1. /*
  2. * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/util/Flags.java,v 1.8 2004/02/21 17:10:30 rleland Exp $
  3. * $Revision: 1.8 $
  4. * $Date: 2004/02/21 17:10:30 $
  5. *
  6. * ====================================================================
  7. * Copyright 2003-2004 The Apache Software Foundation
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. package org.apache.commons.validator.util;
  22. import java.io.Serializable;
  23. /**
  24. * Represents a collection of 64 boolean (on/off) flags. Individual flags
  25. * are represented by powers of 2. For example,<br/>
  26. * Flag 1 = 1<br/>
  27. * Flag 2 = 2<br/>
  28. * Flag 3 = 4<br/>
  29. * Flag 4 = 8<br/><br/>
  30. * or using shift operator to make numbering easier:<br/>
  31. * Flag 1 = 1 << 0<br/>
  32. * Flag 2 = 1 << 1<br/>
  33. * Flag 3 = 1 << 2<br/>
  34. * Flag 4 = 1 << 3<br/>
  35. *
  36. * <p>
  37. * There cannot be a flag with a value of 3 because that represents Flag 1
  38. * and Flag 2 both being on/true.
  39. * </p>
  40. */
  41. public class Flags implements Serializable {
  42. /**
  43. * Represents the current flag state.
  44. */
  45. private long flags = 0;
  46. /**
  47. * Create a new Flags object.
  48. */
  49. public Flags() {
  50. super();
  51. }
  52. /**
  53. * Initialize a new Flags object with the given flags.
  54. */
  55. public Flags(long flags) {
  56. super();
  57. this.flags = flags;
  58. }
  59. /**
  60. * Returns the current flags.
  61. */
  62. public long getFlags() {
  63. return this.flags;
  64. }
  65. /**
  66. * Tests whether the given flag is on. If the flag is not a power of 2
  67. * (ie. 3) this tests whether the combination of flags is on.
  68. */
  69. public boolean isOn(long flag) {
  70. return (this.flags & flag) > 0;
  71. }
  72. /**
  73. * Tests whether the given flag is off. If the flag is not a power of 2
  74. * (ie. 3) this tests whether the combination of flags is off.
  75. */
  76. public boolean isOff(long flag) {
  77. return (this.flags & flag) == 0;
  78. }
  79. /**
  80. * Turns on the given flag. If the flag is not a power of 2 (ie. 3) this
  81. * turns on multiple flags.
  82. */
  83. public void turnOn(long flag) {
  84. this.flags |= flag;
  85. }
  86. /**
  87. * Turns off the given flag. If the flag is not a power of 2 (ie. 3) this
  88. * turns off multiple flags.
  89. */
  90. public void turnOff(long flag) {
  91. this.flags &= ~flag;
  92. }
  93. /**
  94. * Turn off all flags.
  95. */
  96. public void turnOffAll() {
  97. this.flags = 0;
  98. }
  99. /**
  100. * Turn off all flags. This is a synonym for <code>turnOffAll()</code>.
  101. * @since Validator 1.1.1
  102. */
  103. public void clear() {
  104. this.flags = 0;
  105. }
  106. /**
  107. * Turn on all 64 flags.
  108. */
  109. public void turnOnAll() {
  110. this.flags = Long.MAX_VALUE;
  111. }
  112. /**
  113. * Clone this Flags object.
  114. * @see java.lang.Object#clone()
  115. */
  116. public Object clone() {
  117. try {
  118. return super.clone();
  119. } catch(CloneNotSupportedException e) {
  120. throw new RuntimeException("Couldn't clone Flags object.");
  121. }
  122. }
  123. /**
  124. * Tests if two Flags objects are in the same state.
  125. * @param obj object being tested
  126. * @see java.lang.Object#equals(java.lang.Object)
  127. */
  128. public boolean equals(Object obj) {
  129. if (!(obj instanceof Flags)) {
  130. return false;
  131. }
  132. if (obj == this) {
  133. return true;
  134. }
  135. Flags f = (Flags) obj;
  136. return this.flags == f.flags;
  137. }
  138. /**
  139. * The hash code is based on the current state of the flags.
  140. * @see java.lang.Object#hashCode()
  141. */
  142. public int hashCode() {
  143. return (int) this.flags;
  144. }
  145. /**
  146. * Returns a 64 length String with the first flag on the right and the
  147. * 64th flag on the left. A 1 indicates the flag is on, a 0 means it's
  148. * off.
  149. */
  150. public String toString() {
  151. StringBuffer bin = new StringBuffer(Long.toBinaryString(this.flags));
  152. for (int i = 64 - bin.length(); i > 0; i--) {
  153. bin.insert(0, "0");
  154. }
  155. return bin.toString();
  156. }
  157. }