Code - Class EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean


1 /*
2   File: SynchronizedBoolean.java
3
4   Originally written by Doug Lea and released into the public domain.
5   This may be used for any purposes whatsoever without acknowledgment.
6   Thanks for the assistance and support of Sun Microsystems Labs,
7   and everyone contributing, testing, and using this code.
8
9   History:
10   Date Who What
11   19Jun1998 dl Create public version
12 */

13
14 package EDU.oswego.cs.dl.util.concurrent;
15
16 /**
17  * A class useful for offloading synch for boolean instance variables.
18  *
19  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
20  **/

21
22 public class SynchronizedBoolean extends SynchronizedVariable implements Comparable, Cloneable {
23   protected boolean value_;
24
25   /**
26    * Make a new SynchronizedBoolean with the given initial value,
27    * and using its own internal lock.
28    **/

29   public SynchronizedBoolean(boolean initialValue) {
30     super();
31     value_ = initialValue;
32   }
33
34   /**
35    * Make a new SynchronizedBoolean with the given initial value,
36    * and using the supplied lock.
37    **/

38   public SynchronizedBoolean(boolean initialValue, Object lock) {
39     super(lock);
40     value_ = initialValue;
41   }
42
43   /**
44    * Return the current value
45    **/

46   public final boolean get() { synchronized(lock_) { return value_; } }
47
48   /**
49    * Set to newValue.
50    * @return the old value
51    **/

52
53   public boolean set(boolean newValue) {
54     synchronized (lock_) {
55       boolean old = value_;
56       value_ = newValue;
57       return old;
58     }
59   }
60
61   /**
62    * Set value to newValue only if it is currently assumedValue.
63    * @return true if successful
64    **/

65   public boolean commit(boolean assumedValue, boolean newValue) {
66     synchronized(lock_) {
67       boolean success = (assumedValue == value_);
68       if (success) value_ = newValue;
69       return success;
70     }
71   }
72
73   /**
74    * Atomically swap values with another SynchronizedBoolean.
75    * Uses identityHashCode to avoid deadlock when
76    * two SynchronizedBooleans attempt to simultaneously swap with each other.
77    * (Note: Ordering via identyHashCode is not strictly guaranteed
78    * by the language specification to return unique, orderable
79    * values, but in practice JVMs rely on them being unique.)
80    * @return the new value
81    **/

82
83   public boolean swap(SynchronizedBoolean other) {
84     if (other == this) return get();
85     SynchronizedBoolean fst = this;
86     SynchronizedBoolean snd = other;
87     if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
88       fst = other;
89       snd = this;
90     }
91     synchronized(fst.lock_) {
92       synchronized(snd.lock_) {
93         fst.set(snd.set(fst.get()));
94         return get();
95       }
96     }
97   }
98
99   /**
100    * Set the value to its complement
101    * @return the new value
102    **/

103   public boolean complement() {
104     synchronized (lock_) {
105       value_ = !value_;
106       return value_;
107     }
108   }
109
110   /**
111    * Set value to value &amp; b.
112    * @return the new value
113    **/

114   public boolean and(boolean b) {
115     synchronized (lock_) {
116       value_ = value_ & b;
117       return value_;
118     }
119   }
120
121   /**
122    * Set value to value | b.
123    * @return the new value
124    **/

125   public boolean or(boolean b) {
126     synchronized (lock_) {
127       value_ = value_ | b;
128       return value_;
129     }
130   }
131
132
133   /**
134    * Set value to value ^ b.
135    * @return the new value
136    **/

137   public boolean xor(boolean b) {
138     synchronized (lock_) {
139       value_ = value_ ^ b;
140       return value_;
141     }
142   }
143
144
145   public int compareTo(boolean other) {
146     boolean val = get();
147     return (val == other)? 0 : (val)? 1 : -1;
148   }
149
150   public int compareTo(SynchronizedBoolean other) {
151     return compareTo(other.get());
152   }
153
154   public int compareTo(Object other) {
155     return compareTo((SynchronizedBoolean)other);
156   }
157   
158
159   public boolean equals(Object other) {
160     if (other != null &&
161         other instanceof SynchronizedBoolean)
162       return get() == ((SynchronizedBoolean)other).get();
163     else
164       return false;
165   }
166
167   public int hashCode() {
168     boolean b = get();
169     return (b)? 3412688 : 8319343; // entirely arbitrary
170
}
171
172   public String toString() { return String.valueOf(get()); }
173
174 }
175
176

Java API By Example, From Geeks To Geeks. | Conditions of Use | About Us © 2002 - 2005, KickJava.com, or its affiliates