comparison src/EDU/oswego/cs/dl/util/concurrent/SynchronizedChar.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: SynchronizedChar.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 char 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 SynchronizedChar extends SynchronizedVariable implements Comparable, Cloneable {
23
24 protected char value_;
25
26 /**
27 * Make a new SynchronizedChar with the given initial value,
28 * and using its own internal lock.
29 **/
30 public SynchronizedChar(char initialValue) {
31 super();
32 value_ = initialValue;
33 }
34
35 /**
36 * Make a new SynchronizedChar with the given initial value,
37 * and using the supplied lock.
38 **/
39 public SynchronizedChar(char initialValue, Object lock) {
40 super(lock);
41 value_ = initialValue;
42 }
43
44 /**
45 * Return the current value
46 **/
47 public final char get() { synchronized(lock_) { return value_; } }
48
49 /**
50 * Set to newValue.
51 * @return the old value
52 **/
53
54 public char set(char newValue) {
55 synchronized (lock_) {
56 char 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(char assumedValue, char 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 SynchronizedChar.
77 * Uses identityHashCode to avoid deadlock when
78 * two SynchronizedChars 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 char swap(SynchronizedChar other) {
86 if (other == this) return get();
87 SynchronizedChar fst = this;
88 SynchronizedChar 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 * Add amount to value (i.e., set value += amount)
103 * @return the new value
104 **/
105 public char add(char amount) {
106 synchronized (lock_) {
107 return value_ += amount;
108 }
109 }
110
111 /**
112 * Subtract amount from value (i.e., set value -= amount)
113 * @return the new value
114 **/
115 public char subtract(char amount) {
116 synchronized (lock_) {
117 return value_ -= amount;
118 }
119 }
120
121 /**
122 * Multiply value by factor (i.e., set value *= factor)
123 * @return the new value
124 **/
125 public synchronized char multiply(char factor) {
126 synchronized (lock_) {
127 return value_ *= factor;
128 }
129 }
130
131 /**
132 * Divide value by factor (i.e., set value /= factor)
133 * @return the new value
134 **/
135 public char divide(char factor) {
136 synchronized (lock_) {
137 return value_ /= factor;
138 }
139 }
140
141 public int compareTo(char other) {
142 char val = get();
143 return (val < other)? -1 : (val == other)? 0 : 1;
144 }
145
146 public int compareTo(SynchronizedChar other) {
147 return compareTo(other.get());
148 }
149
150 public int compareTo(Object other) {
151 return compareTo((SynchronizedChar)other);
152 }
153
154 public boolean equals(Object other) {
155 if (other != null &&
156 other instanceof SynchronizedChar)
157 return get() == ((SynchronizedChar)other).get();
158 else
159 return false;
160 }
161
162 public int hashCode() { // same hash as Char
163 return (int)(get());
164 }
165
166 public String toString() { return String.valueOf(get()); }
167
168 }
169