KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > EDU > oswego > cs > dl > util > concurrent > SynchronizedShort


1 /*
2   File: SynchronizedShort.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   15Apr2003 dl Removed redundant "synchronized" for multiply()
13 */

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

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

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

40   public SynchronizedShort(short initialValue, Object JavaDoc lock) {
41     super(lock);
42     value_ = initialValue;
43   }
44
45   /**
46    * Return the current value
47    **/

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

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

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

85
86   public short swap(SynchronizedShort other) {
87     if (other == this) return get();
88     SynchronizedShort fst = this;
89     SynchronizedShort snd = other;
90     if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
91       fst = other;
92       snd = this;
93     }
94     synchronized(fst.lock_) {
95       synchronized(snd.lock_) {
96         fst.set(snd.set(fst.get()));
97         return get();
98       }
99     }
100   }
101
102
103   /**
104    * Increment the value.
105    * @return the new value
106    **/

107   public short increment() {
108     synchronized (lock_) {
109       return ++value_;
110     }
111   }
112
113   /**
114    * Decrement the value.
115    * @return the new value
116    **/

117   public short decrement() {
118     synchronized (lock_) {
119       return --value_;
120     }
121   }
122
123   /**
124    * Add amount to value (i.e., set value += amount)
125    * @return the new value
126    **/

127   public short add(short amount) {
128     synchronized (lock_) {
129       return value_ += amount;
130     }
131   }
132
133   /**
134    * Subtract amount from value (i.e., set value -= amount)
135    * @return the new value
136    **/

137   public short subtract(short amount) {
138     synchronized (lock_) {
139       return value_ -= amount;
140     }
141   }
142
143   /**
144    * Multiply value by factor (i.e., set value *= factor)
145    * @return the new value
146    **/

147   public short multiply(short factor) {
148     synchronized (lock_) {
149       return value_ *= factor;
150     }
151   }
152
153   /**
154    * Divide value by factor (i.e., set value /= factor)
155    * @return the new value
156    **/

157   public short divide(short factor) {
158     synchronized (lock_) {
159       return value_ /= factor;
160     }
161   }
162
163   /**
164    * Set the value to the negative of its old value
165    * @return the new value
166    **/

167   public short negate() {
168     synchronized (lock_) {
169       value_ = (short)(-value_);
170       return value_;
171     }
172   }
173
174   /**
175    * Set the value to its complement
176    * @return the new value
177    **/

178   public short complement() {
179     synchronized (lock_) {
180       value_ = (short)(~value_);
181       return value_;
182     }
183   }
184
185   /**
186    * Set value to value &amp; b.
187    * @return the new value
188    **/

189   public short and(short b) {
190     synchronized (lock_) {
191       value_ = (short)(value_ & b);
192       return value_;
193     }
194   }
195
196   /**
197    * Set value to value | b.
198    * @return the new value
199    **/

200   public short or(short b) {
201     synchronized (lock_) {
202       value_ = (short)(value_ | b);
203       return value_;
204     }
205   }
206
207
208   /**
209    * Set value to value ^ b.
210    * @return the new value
211    **/

212   public short xor(short b) {
213     synchronized (lock_) {
214       value_ = (short)(value_ ^ b);
215       return value_;
216     }
217   }
218
219   public int compareTo(short other) {
220     short val = get();
221     return (val < other)? -1 : (val == other)? 0 : 1;
222   }
223
224   public int compareTo(SynchronizedShort other) {
225     return compareTo(other.get());
226   }
227
228   public int compareTo(Object JavaDoc other) {
229     return compareTo((SynchronizedShort)other);
230   }
231
232   public boolean equals(Object JavaDoc other) {
233     if (other != null &&
234         other instanceof SynchronizedShort)
235       return get() == ((SynchronizedShort)other).get();
236     else
237       return false;
238   }
239
240   public int hashCode() {
241     return (int)(get());
242   }
243
244   public String JavaDoc toString() { return String.valueOf(get()); }
245
246 }
247
248
Popular Tags