001: /*
002: File: SynchronizedBoolean.java
003:
004: Originally written by Doug Lea and released into the public domain.
005: This may be used for any purposes whatsoever without acknowledgment.
006: Thanks for the assistance and support of Sun Microsystems Labs,
007: and everyone contributing, testing, and using this code.
008:
009: History:
010: Date Who What
011: 19Jun1998 dl Create public version
012: */
013:
014: package EDU.oswego.cs.dl.util.concurrent;
015:
016: /**
017: * A class useful for offloading synch for boolean instance variables.
018: *
019: * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
020: **/
021:
022: public class SynchronizedBoolean extends SynchronizedVariable implements
023: Comparable, Cloneable {
024: protected boolean value_;
025:
026: /**
027: * Make a new SynchronizedBoolean with the given initial value,
028: * and using its own internal lock.
029: **/
030: public SynchronizedBoolean(boolean initialValue) {
031: super ();
032: value_ = initialValue;
033: }
034:
035: /**
036: * Make a new SynchronizedBoolean with the given initial value,
037: * and using the supplied lock.
038: **/
039: public SynchronizedBoolean(boolean initialValue, Object lock) {
040: super (lock);
041: value_ = initialValue;
042: }
043:
044: /**
045: * Return the current value
046: **/
047: public final boolean get() {
048: synchronized (lock_) {
049: return value_;
050: }
051: }
052:
053: /**
054: * Set to newValue.
055: * @return the old value
056: **/
057:
058: public boolean set(boolean newValue) {
059: synchronized (lock_) {
060: boolean old = value_;
061: value_ = newValue;
062: return old;
063: }
064: }
065:
066: /**
067: * Set value to newValue only if it is currently assumedValue.
068: * @return true if successful
069: **/
070: public boolean commit(boolean assumedValue, boolean newValue) {
071: synchronized (lock_) {
072: boolean success = (assumedValue == value_);
073: if (success)
074: value_ = newValue;
075: return success;
076: }
077: }
078:
079: /**
080: * Atomically swap values with another SynchronizedBoolean.
081: * Uses identityHashCode to avoid deadlock when
082: * two SynchronizedBooleans attempt to simultaneously swap with each other.
083: * (Note: Ordering via identyHashCode is not strictly guaranteed
084: * by the language specification to return unique, orderable
085: * values, but in practice JVMs rely on them being unique.)
086: * @return the new value
087: **/
088:
089: public boolean swap(SynchronizedBoolean other) {
090: if (other == this )
091: return get();
092: SynchronizedBoolean fst = this ;
093: SynchronizedBoolean snd = other;
094: if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
095: fst = other;
096: snd = this ;
097: }
098: synchronized (fst.lock_) {
099: synchronized (snd.lock_) {
100: fst.set(snd.set(fst.get()));
101: return get();
102: }
103: }
104: }
105:
106: /**
107: * Set the value to its complement
108: * @return the new value
109: **/
110: public boolean complement() {
111: synchronized (lock_) {
112: value_ = !value_;
113: return value_;
114: }
115: }
116:
117: /**
118: * Set value to value & b.
119: * @return the new value
120: **/
121: public boolean and(boolean b) {
122: synchronized (lock_) {
123: value_ = value_ & b;
124: return value_;
125: }
126: }
127:
128: /**
129: * Set value to value | b.
130: * @return the new value
131: **/
132: public boolean or(boolean b) {
133: synchronized (lock_) {
134: value_ = value_ | b;
135: return value_;
136: }
137: }
138:
139: /**
140: * Set value to value ^ b.
141: * @return the new value
142: **/
143: public boolean xor(boolean b) {
144: synchronized (lock_) {
145: value_ = value_ ^ b;
146: return value_;
147: }
148: }
149:
150: public int compareTo(boolean other) {
151: boolean val = get();
152: return (val == other) ? 0 : (val) ? 1 : -1;
153: }
154:
155: public int compareTo(SynchronizedBoolean other) {
156: return compareTo(other.get());
157: }
158:
159: public int compareTo(Object other) {
160: return compareTo((SynchronizedBoolean) other);
161: }
162:
163: public boolean equals(Object other) {
164: if (other != null && other instanceof SynchronizedBoolean)
165: return get() == ((SynchronizedBoolean) other).get();
166: else
167: return false;
168: }
169:
170: public int hashCode() {
171: boolean b = get();
172: return (b) ? 3412688 : 8319343; // entirely arbitrary
173: }
174:
175: public String toString() {
176: return String.valueOf(get());
177: }
178:
179: }
|