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 19 import java.io.Serializable; 20 21 /** 22 * Represents a collection of 64 boolean (on/off) flags. Individual flags 23 * are represented by powers of 2. For example,<br> 24 * Flag 1 = 1<br> 25 * Flag 2 = 2<br> 26 * Flag 3 = 4<br> 27 * Flag 4 = 8<br><br> 28 * or using shift operator to make numbering easier:<br> 29 * Flag 1 = 1 << 0<br> 30 * Flag 2 = 1 << 1<br> 31 * Flag 3 = 1 << 2<br> 32 * Flag 4 = 1 << 3<br> 33 * 34 * <p> 35 * There cannot be a flag with a value of 3 because that represents Flag 1 36 * and Flag 2 both being on/true. 37 * </p> 38 */ 39 public class Flags implements Serializable, Cloneable { 40 41 private static final long serialVersionUID = 8481587558770237995L; 42 43 /** 44 * Represents the current flag state. 45 */ 46 private long flags; 47 48 /** 49 * Create a new Flags object. 50 */ 51 public Flags() { 52 } 53 54 /** 55 * Initialize a new Flags object with the given flags. 56 * 57 * @param flags collection of boolean flags to represent. 58 */ 59 public Flags(final long flags) { 60 this.flags = flags; 61 } 62 63 /** 64 * Turn off all flags. This is a synonym for <code>turnOffAll()</code>. 65 * @since 1.1.1 66 */ 67 public void clear() { 68 this.flags = 0; 69 } 70 71 /** 72 * Clone this Flags object. 73 * 74 * @return a copy of this object. 75 * @see Object#clone() 76 */ 77 @Override 78 public Object clone() { 79 try { 80 return super.clone(); 81 } catch (final CloneNotSupportedException e) { 82 throw new UnsupportedOperationException("Couldn't clone Flags object.", e); 83 } 84 } 85 86 /** 87 * Tests if two Flags objects are in the same state. 88 * @param obj object being tested 89 * @see Object#equals(Object) 90 * 91 * @return whether the objects are equal. 92 */ 93 @Override 94 public boolean equals(final Object obj) { 95 if (this == obj) { 96 return true; 97 } 98 if (!(obj instanceof Flags)) { 99 return false; 100 } 101 final Flags other = (Flags) obj; 102 return flags == other.flags; 103 } 104 105 /** 106 * Returns the current flags. 107 * 108 * @return collection of boolean flags represented. 109 */ 110 public long getFlags() { 111 return this.flags; 112 } 113 114 /** 115 * The hash code is based on the current state of the flags. 116 * @see Object#hashCode() 117 * 118 * @return the hash code for this object. 119 */ 120 @Override 121 public int hashCode() { 122 return (int) this.flags; 123 } 124 125 /** 126 * Tests whether the given flag is off. If the flag is not a power of 2 127 * (ie. 3) this tests whether the combination of flags is off. 128 * 129 * @param flag Flag value to check. 130 * 131 * @return whether the specified flag value is off. 132 */ 133 public boolean isOff(final long flag) { 134 return (this.flags & flag) == 0; 135 } 136 137 /** 138 * Tests whether the given flag is on. If the flag is not a power of 2 139 * (ie. 3) this tests whether the combination of flags is on. 140 * 141 * @param flag Flag value to check. 142 * 143 * @return whether the specified flag value is on. 144 */ 145 public boolean isOn(final long flag) { 146 return (this.flags & flag) == flag; 147 } 148 149 /** 150 * Returns a 64 length String with the first flag on the right and the 151 * 64th flag on the left. A 1 indicates the flag is on, a 0 means it's 152 * off. 153 * 154 * @return string representation of this object. 155 */ 156 @Override 157 public String toString() { 158 final StringBuilder bin = new StringBuilder(Long.toBinaryString(this.flags)); 159 for (int i = 64 - bin.length(); i > 0; i--) { // CHECKSTYLE IGNORE MagicNumber 160 bin.insert(0, "0"); 161 } 162 return bin.toString(); 163 } 164 165 /** 166 * Turns off the given flag. If the flag is not a power of 2 (ie. 3) this 167 * turns off multiple flags. 168 * 169 * @param flag Flag value to turn off. 170 */ 171 public void turnOff(final long flag) { 172 this.flags &= ~flag; 173 } 174 175 /** 176 * Turn off all flags. 177 */ 178 public void turnOffAll() { 179 this.flags = 0; 180 } 181 182 /** 183 * Turns on the given flag. If the flag is not a power of 2 (ie. 3) this 184 * turns on multiple flags. 185 * 186 * @param flag Flag value to turn on. 187 */ 188 public void turnOn(final long flag) { 189 this.flags |= flag; 190 } 191 192 /** 193 * Turn on all 64 flags. 194 */ 195 public void turnOnAll() { 196 this.flags = 0xFFFFFFFFFFFFFFFFL; 197 } 198 199 }