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