1 13 14 package org.dbunit.util.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() { synchronized(lock_) { return value_; } } 48 49 53 54 public int set(int newValue) { 55 synchronized (lock_) { 56 int old = value_; 57 value_ = newValue; 58 return old; 59 } 60 } 61 62 66 public boolean commit(int assumedValue, int newValue) { 67 synchronized(lock_) { 68 boolean success = (assumedValue == value_); 69 if (success) value_ = newValue; 70 return success; 71 } 72 } 73 74 83 84 public int swap(SynchronizedInt other) { 85 if (other == this) return get(); 86 SynchronizedInt fst = this; 87 SynchronizedInt snd = other; 88 if (System.identityHashCode(fst) > System.identityHashCode(snd)) { 89 fst = other; 90 snd = this; 91 } 92 synchronized(fst.lock_) { 93 synchronized(snd.lock_) { 94 fst.set(snd.set(fst.get())); 95 return get(); 96 } 97 } 98 } 99 100 104 public int increment() { 105 synchronized (lock_) { 106 return ++value_; 107 } 108 } 109 110 114 public int decrement() { 115 synchronized (lock_) { 116 return --value_; 117 } 118 } 119 120 124 public int add(int amount) { 125 synchronized (lock_) { 126 return value_ += amount; 127 } 128 } 129 130 134 public int subtract(int amount) { 135 synchronized (lock_) { 136 return value_ -= amount; 137 } 138 } 139 140 144 public synchronized int multiply(int factor) { 145 synchronized (lock_) { 146 return value_ *= factor; 147 } 148 } 149 150 154 public int divide(int factor) { 155 synchronized (lock_) { 156 return value_ /= factor; 157 } 158 } 159 160 164 public int negate() { 165 synchronized (lock_) { 166 value_ = -value_; 167 return value_; 168 } 169 } 170 171 175 public int complement() { 176 synchronized (lock_) { 177 value_ = ~value_; 178 return value_; 179 } 180 } 181 182 186 public int and(int b) { 187 synchronized (lock_) { 188 value_ = value_ & b; 189 return value_; 190 } 191 } 192 193 197 public int or(int b) { 198 synchronized (lock_) { 199 value_ = value_ | b; 200 return value_; 201 } 202 } 203 204 205 209 public int xor(int b) { 210 synchronized (lock_) { 211 value_ = value_ ^ b; 212 return value_; 213 } 214 } 215 216 public int compareTo(int other) { 217 int val = get(); 218 return (val < other)? -1 : (val == other)? 0 : 1; 219 } 220 221 public int compareTo(SynchronizedInt other) { 222 return compareTo(other.get()); 223 } 224 225 public int compareTo(Object other) { 226 return compareTo((SynchronizedInt)other); 227 } 228 229 public boolean equals(Object other) { 230 if (other != null && 231 other instanceof SynchronizedInt) 232 return get() == ((SynchronizedInt)other).get(); 233 else 234 return false; 235 } 236 237 public int hashCode() { return get(); } 238 239 public String toString() { return String.valueOf(get()); } 240 241 } 242 243 | Popular Tags |