comparison src/EDU/oswego/cs/dl/util/concurrent/SynchronizedByte.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 /*
2 File: SynchronizedByte.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 byte 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 SynchronizedByte extends SynchronizedVariable implements Comparable, Cloneable {
23
24 protected byte value_;
25
26 /**
27 * Make a new SynchronizedByte with the given initial value,
28 * and using its own internal lock.
29 **/
30 public SynchronizedByte(byte initialValue) {
31 super();
32 value_ = initialValue;
33 }
34
35 /**
36 * Make a new SynchronizedByte with the given initial value,
37 * and using the supplied lock.
38 **/
39 public SynchronizedByte(byte initialValue, Object lock) {
40 super(lock);
41 value_ = initialValue;
42 }
43
44 /**
45 * Return the current value
46 **/
47 public final byte get() { synchronized(lock_) { return value_; } }
48
49 /**
50 * Set to newValue.
51 * @return the old value
52 **/
53
54 public byte set(byte newValue) {
55 synchronized (lock_) {
56 byte old = value_;
57 value_ = newValue;
58 return old;
59 }
60 }
61
62 /**
63 * Set value to newValue only if it is currently assumedValue.
64 * @return true if successful
65 **/
66 public boolean commit(byte assumedValue, byte newValue) {
67 synchronized(lock_) {
68 boolean success = (assumedValue == value_);
69 if (success) value_ = newValue;
70 return success;
71 }
72 }
73
74
75 /**
76 * Atomically swap values with another SynchronizedByte.
77 * Uses identityHashCode to avoid deadlock when
78 * two SynchronizedBytes attempt to simultaneously swap with each other.
79 * (Note: Ordering via identyHashCode is not strictly guaranteed
80 * by the language specification to return unique, orderable
81 * values, but in practice JVMs rely on them being unique.)
82 * @return the new value
83 **/
84
85 public byte swap(SynchronizedByte other) {
86 if (other == this) return get();
87 SynchronizedByte fst = this;
88 SynchronizedByte snd = other;
89 if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
90 fst = other;
91 snd = this;
92 }
93 synchronized(fst.lock_) {
94 synchronized(snd.lock_) {
95 fst.set(snd.set(fst.get()));
96 return get();
97 }
98 }
99 }
100
101 /**
102 * Increment the value.
103 * @return the new value
104 **/
105 public byte increment() {
106 synchronized (lock_) {
107 return ++value_;
108 }
109 }
110
111 /**
112 * Decrement the value.
113 * @return the new value
114 **/
115 public byte decrement() {
116 synchronized (lock_) {
117 return --value_;
118 }
119 }
120
121 /**
122 * Add amount to value (i.e., set value += amount)
123 * @return the new value
124 **/
125 public byte add(byte amount) {
126 synchronized (lock_) {
127 return value_ += amount;
128 }
129 }
130
131 /**
132 * Subtract amount from value (i.e., set value -= amount)
133 * @return the new value
134 **/
135 public byte subtract(byte amount) {
136 synchronized (lock_) {
137 return value_ -= amount;
138 }
139 }
140
141 /**
142 * Multiply value by factor (i.e., set value *= factor)
143 * @return the new value
144 **/
145 public synchronized byte multiply(byte factor) {
146 synchronized (lock_) {
147 return value_ *= factor;
148 }
149 }
150
151 /**
152 * Divide value by factor (i.e., set value /= factor)
153 * @return the new value
154 **/
155 public byte divide(byte factor) {
156 synchronized (lock_) {
157 return value_ /= factor;
158 }
159 }
160
161 /**
162 * Set the value to the negative of its old value
163 * @return the new value
164 **/
165 public byte negate() {
166 synchronized (lock_) {
167 value_ = (byte)(-value_);
168 return value_;
169 }
170 }
171
172 /**
173 * Set the value to its complement
174 * @return the new value
175 **/
176 public byte complement() {
177 synchronized (lock_) {
178 value_ = (byte)~value_;
179 return value_;
180 }
181 }
182
183 /**
184 * Set value to value &amp; b.
185 * @return the new value
186 **/
187 public byte and(byte b) {
188 synchronized (lock_) {
189 value_ = (byte)(value_ & b);
190 return value_;
191 }
192 }
193
194 /**
195 * Set value to value | b.
196 * @return the new value
197 **/
198 public byte or(byte b) {
199 synchronized (lock_) {
200 value_ = (byte)(value_ | b);
201 return value_;
202 }
203 }
204
205
206 /**
207 * Set value to value ^ b.
208 * @return the new value
209 **/
210 public byte xor(byte b) {
211 synchronized (lock_) {
212 value_ = (byte)(value_ ^ b);
213 return value_;
214 }
215 }
216
217 public int compareTo(byte other) {
218 byte val = get();
219 return (val < other)? -1 : (val == other)? 0 : 1;
220 }
221
222 public int compareTo(SynchronizedByte other) {
223 return compareTo(other.get());
224 }
225
226 public int compareTo(Object other) {
227 return compareTo((SynchronizedByte)other);
228 }
229
230 public boolean equals(Object other) {
231 if (other != null &&
232 other instanceof SynchronizedByte)
233 return get() == ((SynchronizedByte)other).get();
234 else
235 return false;
236 }
237
238 public int hashCode() {
239 return (int)(get());
240 }
241
242 public String toString() { return Byte.toString(get()); }
243
244 }
245