1 13 14 package org.logicalcobwebs.concurrent; 15 16 21 22 public class SynchronizedInt extends SynchronizedVariable implements Comparable , Cloneable { 23 24 protected int value_; 25 26 30 public SynchronizedInt(int initialValue) { 31 super(); 32 value_ = initialValue; 33 } 34 35 39 public SynchronizedInt(int initialValue, Object lock) { 40 super(lock); 41 value_ = initialValue; 42 } 43 44 47 public final int get() { 48 synchronized (lock_) { 49 return value_; 50 } 51 } 52 53 57 58 public int set(int newValue) { 59 synchronized (lock_) { 60 int old = value_; 61 value_ = newValue; 62 return old; 63 } 64 } 65 66 70 public boolean commit(int assumedValue, int newValue) { 71 synchronized (lock_) { 72 boolean success = (assumedValue == value_); 73 if (success) value_ = newValue; 74 return success; 75 } 76 } 77 78 87 88 public int swap(SynchronizedInt other) { 89 if (other == this) return get(); 90 SynchronizedInt fst = this; 91 SynchronizedInt snd = other; 92 if (System.identityHashCode(fst) > System.identityHashCode(snd)) { 93 fst = other; 94 snd = this; 95 } 96 synchronized (fst.lock_) { 97 synchronized (snd.lock_) { 98 fst.set(snd.set(fst.get())); 99 return get(); 100 } 101 } 102 } 103 104 108 public int increment() { 109 synchronized (lock_) { 110 return ++value_; 111 } 112 } 113 114 118 public int decrement() { 119 synchronized (lock_) { 120 return --value_; 121 } 122 } 123 124 128 public int add(int amount) { 129 synchronized (lock_) { 130 return value_ += amount; 131 } 132 } 133 134 138 public int subtract(int amount) { 139 synchronized (lock_) { 140 return value_ -= amount; 141 } 142 } 143 144 148 public synchronized int multiply(int factor) { 149 synchronized (lock_) { 150 return value_ *= factor; 151 } 152 } 153 154 158 public int divide(int factor) { 159 synchronized (lock_) { 160 return value_ /= factor; 161 } 162 } 163 164 168 public int negate() { 169 synchronized (lock_) { 170 value_ = -value_; 171 return value_; 172 } 173 } 174 175 179 public int complement() { 180 synchronized (lock_) { 181 value_ = ~value_; 182 return value_; 183 } 184 } 185 186 190 public int and(int b) { 191 synchronized (lock_) { 192 value_ = value_ & b; 193 return value_; 194 } 195 } 196 197 201 public int or(int b) { 202 synchronized (lock_) { 203 value_ = value_ | b; 204 return value_; 205 } 206 } 207 208 209 213 public int xor(int b) { 214 synchronized (lock_) { 215 value_ = value_ ^ b; 216 return value_; 217 } 218 } 219 220 public int compareTo(int other) { 221 int val = get(); 222 return (val < other) ? -1 : (val == other) ? 0 : 1; 223 } 224 225 public int compareTo(SynchronizedInt other) { 226 return compareTo(other.get()); 227 } 228 229 public int compareTo(Object other) { 230 return compareTo((SynchronizedInt) other); 231 } 232 233 public boolean equals(Object other) { 234 if (other != null && 235 other instanceof SynchronizedInt) 236 return get() == ((SynchronizedInt) other).get(); 237 else 238 return false; 239 } 240 241 public int hashCode() { 242 return get(); 243 } 244 245 public String toString() { 246 return String.valueOf(get()); 247 } 248 249 } 250 251 | Popular Tags |