KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > MuBoolean


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.util;
11
12 import java.io.Serializable JavaDoc;
13
14 /**
15  * A mutable boolean class.
16  *
17  * @version <tt>$Revision: 1.1.28.1 $</tt>
18  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
19  */

20 public class MuBoolean
21    implements Comparable JavaDoc, Cloneable JavaDoc, Serializable JavaDoc, Mutable
22 {
23    static final long serialVersionUID = 8496688045920612417L;
24    /** Boolean value */
25    private boolean value; // = false;
26

27    /**
28     * Construct a new mutable boolean.
29     */

30    public MuBoolean() {}
31
32    /**
33     * Construct a new mutable boolean.
34     *
35     * @param b Boolean value.
36     */

37    public MuBoolean(boolean b) {
38       value = b;
39    }
40
41    /**
42     * Construct a new mutable boolean.
43     *
44     * @param obj Object to convert to a boolean value.
45     */

46    public MuBoolean(Object JavaDoc obj) {
47       setValue(obj);
48    }
49
50    /**
51     * Construct a new mutable boolean.
52     *
53     * @param value String to convert to a boolean value.
54     */

55    public MuBoolean(String JavaDoc value) {
56       set(Boolean.valueOf(value));
57    }
58
59    /**
60     * Return the value of this mutable boolean.
61     *
62     * @return Boolean value.
63     */

64    public boolean booleanValue() {
65       return value;
66    }
67
68    /**
69     * Set the value.
70     *
71     * @param b Boolean value.
72     * @return The previous value.
73     */

74    public boolean set(boolean b) {
75       boolean old = value;
76       value = b;
77       return old;
78    }
79
80    /**
81     * Set the value.
82     *
83     * @param b Boolean value.
84     * @return The previous value.
85     */

86    public boolean set(Boolean JavaDoc b) {
87       boolean old = value;
88       value = b.booleanValue();
89       return old;
90    }
91
92    /**
93     * Set the value.
94     *
95     * @param b Boolean value.
96     * @return The previous value.
97     */

98    public boolean set(MuBoolean b) {
99       boolean old = value;
100       value = b.value;
101       return old;
102    }
103
104    /**
105     * Get the current value.
106     *
107     * @return The current value.
108     */

109    public boolean get() {
110       return value;
111    }
112
113    /**
114     * Set the value to value only if the current value is equal to
115     * the assumed value.
116     *
117     * @param assumed The assumed value.
118     * @param b The new value.
119     * @return True if value was changed.
120     */

121    public boolean commit(boolean assumed, boolean b) {
122       boolean success = (assumed == value);
123       if (success) value = b;
124       return success;
125    }
126
127    /**
128     * Swap values with another mutable boolean.
129     *
130     * @param b Mutable boolean to swap values with.
131     * @return The new value.
132     */

133    public boolean swap(MuBoolean b) {
134       if (b == this) return value;
135
136       boolean temp = value;
137       value = b.value;
138       b.value = temp;
139
140       return value;
141    }
142
143    /**
144     * Set the value to its complement.
145     *
146     * @return The new value.
147     */

148    public boolean complement() {
149       value = !value;
150       return value;
151    }
152
153    /**
154     * <i>AND</i>s the current value with the specified value.
155     *
156     * @param b Value to <i>and</i> with.
157     * @return The new value.
158     */

159    public boolean and(boolean b) {
160       value &= b;
161
162       return value;
163    }
164
165    /**
166     * <i>OR</i>s the current value with the specified value.
167     *
168     * @param b Value to <i>or</i> with.
169     * @return The new value.
170     */

171    public boolean or(boolean b) {
172       value |= b;
173
174       return value;
175    }
176
177    /**
178     * <i>XOR</i>s the current value with the specified value.
179     *
180     * @param b Value to <i>xor</i> with.
181     * @return The new value.
182     */

183    public boolean xor(boolean b) {
184       value ^= b;
185
186       return value;
187    }
188
189    /**
190     * Compares this object with the specified boolean for order.
191     *
192     * @param bool Boolean to compare with.
193     * @return A negative integer, zero, or a positive integer as
194     * this object is less than, equal to, or greater than
195     * the specified object.
196     */

197    public int compareTo(boolean bool) {
198       return (value == bool) ? 0 : (value) ? 1 : -1;
199    }
200
201    /**
202     * Compares this object with the specified object for order.
203     *
204     * @param bool Boolean to compare with.
205     * @return A negative integer, zero, or a positive integer as
206     * this object is less than, equal to, or greater than
207     * the specified object.
208     *
209     * @throws ClassCastException Object is not a MuBoolean.
210     */

211    public int compareTo(Object JavaDoc obj) throws ClassCastException JavaDoc {
212       return compareTo(((MuBoolean)obj).value);
213    }
214
215    /**
216     * Convert to a string.
217     *
218     * @return String value
219     */

220    public String JavaDoc toString() {
221       return String.valueOf(value);
222    }
223
224    /**
225     * Return the hash code for this mutable boolean.
226     *
227     * @return Hash code
228     */

229    public int hashCode() {
230       return HashCode.generate(value);
231    }
232
233    /**
234     * Test the equality of another object.
235     *
236     * @param obj Object to test equality with.
237     * @return True if object is equal to this.
238     */

239    public boolean equals(Object JavaDoc obj) {
240       if (obj == this) return true;
241
242       if (obj != null && obj.getClass() == getClass()) {
243          MuBoolean bool = (MuBoolean)obj;
244          return value == bool.value;
245       }
246
247       return false;
248    }
249
250    /**
251     * Clone this mutable boolean.
252     *
253     * @return Cloned mutable boolean.
254     */

255    public Object JavaDoc clone() {
256       try {
257          return super.clone();
258       }
259       catch (CloneNotSupportedException JavaDoc e) {
260          throw new InternalError JavaDoc();
261       }
262    }
263
264
265    /////////////////////////////////////////////////////////////////////////
266
// Mutable Support //
267
/////////////////////////////////////////////////////////////////////////
268

269    /**
270     * Set the value of this mutable boolean.
271     *
272     * <p>If value is a <code>java.lang.Boolean</code>, then use
273     * <code>Boolean.booleanValue()</code> to determin value, if
274     * the object is non-null then the value is <i>true</i>, else
275     * it is <i>false</i>.
276     *
277     * @param obj Object to convert to a boolean value.
278     */

279    public void setValue(Object JavaDoc obj) {
280       if (obj instanceof MuBoolean) {
281          value = ((MuBoolean)obj).value;
282       }
283       else if (obj instanceof Boolean JavaDoc) {
284          value = ((Boolean JavaDoc)obj).booleanValue();
285       }
286       else if (obj != null) {
287          value = true;
288       }
289       else {
290          value = false;
291       }
292    }
293
294    /**
295     * Get the boolean value of this mutable boolean.
296     *
297     * @return <code>java.lang.Boolean</code> value.
298     */

299    public Object JavaDoc getValue() {
300       return value ? Boolean.TRUE : Boolean.FALSE;
301    }
302 }
303
304
Popular Tags