1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.dbunit.util.concurrent;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19
20
21
22
23
24
25
26
27
28
29 public class SynchronizedInt extends SynchronizedVariable implements Comparable, Cloneable {
30
31
32
33
34 private static final Logger logger = LoggerFactory.getLogger(SynchronizedInt.class);
35
36 protected int value_;
37
38
39
40
41
42 public SynchronizedInt(int initialValue) {
43 super();
44 value_ = initialValue;
45 }
46
47
48
49
50
51 public SynchronizedInt(int initialValue, Object lock) {
52 super(lock);
53 value_ = initialValue;
54 }
55
56
57
58
59 public final int get() {
60 synchronized(lock_) { return value_; } }
61
62
63
64
65
66
67 public int set(int newValue) {
68 logger.debug("set(newValue={}) - start", String.valueOf(newValue));
69
70 synchronized (lock_) {
71 int old = value_;
72 value_ = newValue;
73 return old;
74 }
75 }
76
77
78
79
80
81 public boolean commit(int assumedValue, int newValue) {
82 logger.debug("commit(assumedValue={}, newValue={}) - start", String.valueOf(assumedValue), String.valueOf(newValue));
83
84 synchronized(lock_) {
85 boolean success = (assumedValue == value_);
86 if (success) value_ = newValue;
87 return success;
88 }
89 }
90
91
92
93
94
95
96
97
98
99
100
101 public int swap(SynchronizedInt other) {
102 logger.debug("swap(other={}) - start", other);
103
104 if (other == this) return get();
105 SynchronizedInt fst = this;
106 SynchronizedInt snd = other;
107 if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
108 fst = other;
109 snd = this;
110 }
111 synchronized(fst.lock_) {
112 synchronized(snd.lock_) {
113 fst.set(snd.set(fst.get()));
114 return get();
115 }
116 }
117 }
118
119
120
121
122
123 public int increment() {
124 synchronized (lock_) {
125 return ++value_;
126 }
127 }
128
129
130
131
132
133 public int decrement() {
134 synchronized (lock_) {
135 return --value_;
136 }
137 }
138
139
140
141
142
143 public int add(int amount) {
144 synchronized (lock_) {
145 return value_ += amount;
146 }
147 }
148
149
150
151
152
153 public int subtract(int amount) {
154 synchronized (lock_) {
155 return value_ -= amount;
156 }
157 }
158
159
160
161
162
163 public synchronized int multiply(int factor) {
164 synchronized (lock_) {
165 return value_ *= factor;
166 }
167 }
168
169
170
171
172
173 public int divide(int factor) {
174 synchronized (lock_) {
175 return value_ /= factor;
176 }
177 }
178
179
180
181
182
183 public int negate() {
184 synchronized (lock_) {
185 value_ = -value_;
186 return value_;
187 }
188 }
189
190
191
192
193
194 public int complement() {
195 synchronized (lock_) {
196 value_ = ~value_;
197 return value_;
198 }
199 }
200
201
202
203
204
205 public int and(int b) {
206 synchronized (lock_) {
207 value_ = value_ & b;
208 return value_;
209 }
210 }
211
212
213
214
215
216 public int or(int b) {
217 synchronized (lock_) {
218 value_ = value_ | b;
219 return value_;
220 }
221 }
222
223
224
225
226
227
228 public int xor(int b) {
229 synchronized (lock_) {
230 value_ = value_ ^ b;
231 return value_;
232 }
233 }
234
235 public int compareTo(int other) {
236 logger.debug("compareTo(other={}) - start", String.valueOf(other));
237 int val = get();
238 return (val < other)? -1 : (val == other)? 0 : 1;
239 }
240
241 public int compareTo(SynchronizedInt other) {
242 logger.debug("compareTo(other={}) - start", other);
243 return compareTo(other.get());
244 }
245
246 public int compareTo(Object other) {
247 logger.debug("compareTo(other={}) - start", other);
248 return compareTo((SynchronizedInt)other);
249 }
250
251 public boolean equals(Object other) {
252 logger.debug("equals(other={}) - start", other);
253 if (other != null &&
254 other instanceof SynchronizedInt)
255 return get() == ((SynchronizedInt)other).get();
256 else
257 return false;
258 }
259
260 public int hashCode() {
261 return get();
262 }
263
264 public String toString() {
265 return String.valueOf(get());
266 }
267
268 }
269