Flags.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.validator.util;

  18. import java.io.Serializable;

  19. /**
  20.  * Represents a collection of 64 boolean (on/off) flags.  Individual flags
  21.  * are represented by powers of 2.  For example,<br>
  22.  * Flag 1 = 1<br>
  23.  * Flag 2 = 2<br>
  24.  * Flag 3 = 4<br>
  25.  * Flag 4 = 8<br><br>
  26.  * or using shift operator to make numbering easier:<br>
  27.  * Flag 1 = 1 &lt;&lt; 0<br>
  28.  * Flag 2 = 1 &lt;&lt; 1<br>
  29.  * Flag 3 = 1 &lt;&lt; 2<br>
  30.  * Flag 4 = 1 &lt;&lt; 3<br>
  31.  *
  32.  * <p>
  33.  * There cannot be a flag with a value of 3 because that represents Flag 1
  34.  * and Flag 2 both being on/true.
  35.  * </p>
  36.  */
  37. public class Flags implements Serializable, Cloneable {

  38.     private static final long serialVersionUID = 8481587558770237995L;

  39.     /**
  40.      * Represents the current flag state.
  41.      */
  42.     private long flags;

  43.     /**
  44.      * Create a new Flags object.
  45.      */
  46.     public Flags() {
  47.     }

  48.     /**
  49.      * Initialize a new Flags object with the given flags.
  50.      *
  51.      * @param flags collection of boolean flags to represent.
  52.      */
  53.     public Flags(final long flags) {
  54.         this.flags = flags;
  55.     }

  56.     /**
  57.      * Turn off all flags.  This is a synonym for <code>turnOffAll()</code>.
  58.      * @since 1.1.1
  59.      */
  60.     public void clear() {
  61.         this.flags = 0;
  62.     }

  63.     /**
  64.      * Clone this Flags object.
  65.      *
  66.      * @return a copy of this object.
  67.      * @see Object#clone()
  68.      */
  69.     @Override
  70.     public Object clone() {
  71.         try {
  72.             return super.clone();
  73.         } catch (final CloneNotSupportedException e) {
  74.             throw new UnsupportedOperationException("Couldn't clone Flags object.", e);
  75.         }
  76.     }

  77.     /**
  78.      * Tests if two Flags objects are in the same state.
  79.      * @param obj object being tested
  80.      * @see Object#equals(Object)
  81.      *
  82.      * @return whether the objects are equal.
  83.      */
  84.     @Override
  85.     public boolean equals(final Object obj) {
  86.         if (this == obj) {
  87.             return true;
  88.         }
  89.         if (!(obj instanceof Flags)) {
  90.             return false;
  91.         }
  92.         final Flags other = (Flags) obj;
  93.         return flags == other.flags;
  94.     }

  95.     /**
  96.      * Returns the current flags.
  97.      *
  98.      * @return collection of boolean flags represented.
  99.      */
  100.     public long getFlags() {
  101.         return this.flags;
  102.     }

  103.     /**
  104.      * The hash code is based on the current state of the flags.
  105.      * @see Object#hashCode()
  106.      *
  107.      * @return the hash code for this object.
  108.      */
  109.     @Override
  110.     public int hashCode() {
  111.         return (int) this.flags;
  112.     }

  113.     /**
  114.      * Tests whether the given flag is off.  If the flag is not a power of 2
  115.      * (ie. 3) this tests whether the combination of flags is off.
  116.      *
  117.      * @param flag Flag value to check.
  118.      *
  119.      * @return whether the specified flag value is off.
  120.      */
  121.     public boolean isOff(final long flag) {
  122.         return (this.flags & flag) == 0;
  123.     }

  124.     /**
  125.      * Tests whether the given flag is on.  If the flag is not a power of 2
  126.      * (ie. 3) this tests whether the combination of flags is on.
  127.      *
  128.      * @param flag Flag value to check.
  129.      *
  130.      * @return whether the specified flag value is on.
  131.      */
  132.     public boolean isOn(final long flag) {
  133.         return (this.flags & flag) == flag;
  134.     }

  135.     /**
  136.      * Returns a 64 length String with the first flag on the right and the
  137.      * 64th flag on the left.  A 1 indicates the flag is on, a 0 means it's
  138.      * off.
  139.      *
  140.      * @return string representation of this object.
  141.      */
  142.     @Override
  143.     public String toString() {
  144.         final StringBuilder bin = new StringBuilder(Long.toBinaryString(this.flags));
  145.         for (int i = 64 - bin.length(); i > 0; i--) { // CHECKSTYLE IGNORE MagicNumber
  146.             bin.insert(0, "0");
  147.         }
  148.         return bin.toString();
  149.     }

  150.     /**
  151.      * Turns off the given flag.  If the flag is not a power of 2 (ie. 3) this
  152.      * turns off multiple flags.
  153.      *
  154.      * @param flag Flag value to turn off.
  155.      */
  156.     public void turnOff(final long flag) {
  157.         this.flags &= ~flag;
  158.     }

  159.     /**
  160.      * Turn off all flags.
  161.      */
  162.     public void turnOffAll() {
  163.         this.flags = 0;
  164.     }

  165.     /**
  166.      * Turns on the given flag.  If the flag is not a power of 2 (ie. 3) this
  167.      * turns on multiple flags.
  168.      *
  169.      * @param flag Flag value to turn on.
  170.      */
  171.     public void turnOn(final long flag) {
  172.         this.flags |= flag;
  173.     }

  174.     /**
  175.      * Turn on all 64 flags.
  176.      */
  177.     public void turnOnAll() {
  178.         this.flags = 0xFFFFFFFFFFFFFFFFL;
  179.     }

  180. }