View Javadoc
1   /*
2     File: SynchronizedInt.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 org.dbunit.util.concurrent;
15  
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  
19  /**
20   * A class useful for offloading synch for int instance variables.
21   *
22   * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
23   * 
24   * @author Doug Lea
25   * @author Last changed by: $Author$
26   * @version $Revision$ $Date$
27   * @since ? (pre 2.1)
28   */
29  public class SynchronizedInt extends SynchronizedVariable implements Comparable, Cloneable {
30  
31      /**
32       * Logger for this class
33       */
34      private static final Logger logger = LoggerFactory.getLogger(SynchronizedInt.class);
35  
36    protected int value_;
37  
38    /** 
39     * Make a new SynchronizedInt with the given initial value,
40     * and using its own internal lock.
41     **/
42    public SynchronizedInt(int initialValue) { 
43      super(); 
44      value_ = initialValue; 
45    }
46  
47    /** 
48     * Make a new SynchronizedInt with the given initial value,
49     * and using the supplied lock.
50     **/
51    public SynchronizedInt(int initialValue, Object lock) { 
52      super(lock); 
53      value_ = initialValue; 
54    }
55  
56    /** 
57     * Return the current value 
58     **/
59    public final int get() {
60   synchronized(lock_) { return value_; } }
61  
62    /** 
63     * Set to newValue.
64     * @return the old value 
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     * Set value to newValue only if it is currently assumedValue.
79     * @return true if successful
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     * Atomically swap values with another SynchronizedInt.
93     * Uses identityHashCode to avoid deadlock when
94     * two SynchronizedInts attempt to simultaneously swap with each other.
95     * (Note: Ordering via identyHashCode is not strictly guaranteed
96     * by the language specification to return unique, orderable
97     * values, but in practice JVMs rely on them being unique.)
98     * @return the new value 
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    * Increment the value.
121    * @return the new value 
122    **/
123   public int increment() {
124     synchronized (lock_) {
125       return ++value_; 
126     }
127   }
128 
129   /** 
130    * Decrement the value.
131    * @return the new value 
132    **/
133   public int decrement() {
134     synchronized (lock_) {
135       return --value_; 
136     }
137   }
138 
139   /** 
140    * Add amount to value (i.e., set value += amount)
141    * @return the new value 
142    **/
143   public int add(int amount) {
144     synchronized (lock_) {
145       return value_ += amount; 
146     }
147   }
148 
149   /** 
150    * Subtract amount from value (i.e., set value -= amount)
151    * @return the new value 
152    **/
153   public int subtract(int amount) {
154     synchronized (lock_) {
155       return value_ -= amount; 
156     }
157   }
158 
159   /** 
160    * Multiply value by factor (i.e., set value *= factor)
161    * @return the new value 
162    **/
163   public synchronized int multiply(int factor) {
164     synchronized (lock_) {
165       return value_ *= factor; 
166     }
167   }
168 
169   /** 
170    * Divide value by factor (i.e., set value /= factor)
171    * @return the new value 
172    **/
173   public int divide(int factor) {
174     synchronized (lock_) {
175       return value_ /= factor; 
176     }
177   }
178 
179   /** 
180    * Set the value to the negative of its old value
181    * @return the new value 
182    **/
183   public int negate() {
184     synchronized (lock_) {
185       value_ = -value_;
186       return value_;
187     }
188   }
189 
190   /** 
191    * Set the value to its complement
192    * @return the new value 
193    **/
194   public  int complement() {
195     synchronized (lock_) {
196       value_ = ~value_;
197       return value_;
198     }
199   }
200 
201   /** 
202    * Set value to value &amp; b.
203    * @return the new value 
204    **/
205   public  int and(int b) {
206     synchronized (lock_) {
207       value_ = value_ & b;
208       return value_;
209     }
210   }
211 
212   /** 
213    * Set value to value | b.
214    * @return the new value 
215    **/
216   public  int or(int b) {
217     synchronized (lock_) {
218       value_ = value_ | b;
219       return value_;
220     }
221   }
222 
223 
224   /** 
225    * Set value to value ^ b.
226    * @return the new value 
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