1 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 16 21 22 public class SynchronizedByte extends SynchronizedVariable implements Comparable , Cloneable { 23 24 protected byte value_; 25 26 30 public SynchronizedByte(byte initialValue) { 31 super(); 32 value_ = initialValue; 33 } 34 35 39 public SynchronizedByte(byte initialValue, Object lock) { 40 super(lock); 41 value_ = initialValue; 42 } 43 44 47 public final byte get() { synchronized(lock_) { return value_; } } 48 49 53 54 public byte set(byte newValue) { 55 synchronized (lock_) { 56 byte old = value_; 57 value_ = newValue; 58 return old; 59 } 60 } 61 62 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 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 105 public byte increment() { 106 synchronized (lock_) { 107 return ++value_; 108 } 109 } 110 111 115 public byte decrement() { 116 synchronized (lock_) { 117 return --value_; 118 } 119 } 120 121 125 public byte add(byte amount) { 126 synchronized (lock_) { 127 return value_ += amount; 128 } 129 } 130 131 135 public byte subtract(byte amount) { 136 synchronized (lock_) { 137 return value_ -= amount; 138 } 139 } 140 141 145 public synchronized byte multiply(byte factor) { 146 synchronized (lock_) { 147 return value_ *= factor; 148 } 149 } 150 151 155 public byte divide(byte factor) { 156 synchronized (lock_) { 157 return value_ /= factor; 158 } 159 } 160 161 165 public byte negate() { 166 synchronized (lock_) { 167 value_ = (byte)(-value_); 168 return value_; 169 } 170 } 171 172 176 public byte complement() { 177 synchronized (lock_) { 178 value_ = (byte)~value_; 179 return value_; 180 } 181 } 182 183 187 public byte and(byte b) { 188 synchronized (lock_) { 189 value_ = (byte)(value_ & b); 190 return value_; 191 } 192 } 193 194 198 public byte or(byte b) { 199 synchronized (lock_) { 200 value_ = (byte)(value_ | b); 201 return value_; 202 } 203 } 204 205 206 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 246 | Popular Tags |