SynchronizedInt.java

  1. /*
  2.   File: SynchronizedInt.java

  3.   Originally written by Doug Lea and released into the public domain.
  4.   This may be used for any purposes whatsoever without acknowledgment.
  5.   Thanks for the assistance and support of Sun Microsystems Labs,
  6.   and everyone contributing, testing, and using this code.

  7.   History:
  8.   Date       Who                What
  9.   19Jun1998  dl               Create public version
  10. */

  11. package org.dbunit.util.concurrent;

  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;

  14. /**
  15.  * A class useful for offloading synch for int instance variables.
  16.  *
  17.  * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
  18.  *
  19.  * @author Doug Lea
  20.  * @author Last changed by: $Author$
  21.  * @version $Revision$ $Date$
  22.  * @since ? (pre 2.1)
  23.  */
  24. public class SynchronizedInt extends SynchronizedVariable implements Comparable, Cloneable {

  25.     /**
  26.      * Logger for this class
  27.      */
  28.     private static final Logger logger = LoggerFactory.getLogger(SynchronizedInt.class);

  29.   protected int value_;

  30.   /**
  31.    * Make a new SynchronizedInt with the given initial value,
  32.    * and using its own internal lock.
  33.    **/
  34.   public SynchronizedInt(int initialValue) {
  35.     super();
  36.     value_ = initialValue;
  37.   }

  38.   /**
  39.    * Make a new SynchronizedInt with the given initial value,
  40.    * and using the supplied lock.
  41.    **/
  42.   public SynchronizedInt(int initialValue, Object lock) {
  43.     super(lock);
  44.     value_ = initialValue;
  45.   }

  46.   /**
  47.    * Return the current value
  48.    **/
  49.   public final int get() {
  50.  synchronized(lock_) { return value_; } }

  51.   /**
  52.    * Set to newValue.
  53.    * @return the old value
  54.    **/

  55.   public int set(int newValue) {
  56.         logger.debug("set(newValue={}) - start", String.valueOf(newValue));
  57.  
  58.     synchronized (lock_) {
  59.       int old = value_;
  60.       value_ = newValue;
  61.       return old;
  62.     }
  63.   }

  64.   /**
  65.    * Set value to newValue only if it is currently assumedValue.
  66.    * @return true if successful
  67.    **/
  68.   public boolean commit(int assumedValue, int newValue) {
  69.         logger.debug("commit(assumedValue={}, newValue={}) - start", String.valueOf(assumedValue), String.valueOf(newValue));

  70.     synchronized(lock_) {
  71.       boolean success = (assumedValue == value_);
  72.       if (success) value_ = newValue;
  73.       return success;
  74.     }
  75.   }

  76.   /**
  77.    * Atomically swap values with another SynchronizedInt.
  78.    * Uses identityHashCode to avoid deadlock when
  79.    * two SynchronizedInts attempt to simultaneously swap with each other.
  80.    * (Note: Ordering via identyHashCode is not strictly guaranteed
  81.    * by the language specification to return unique, orderable
  82.    * values, but in practice JVMs rely on them being unique.)
  83.    * @return the new value
  84.    **/

  85.   public int swap(SynchronizedInt other) {
  86.         logger.debug("swap(other={}) - start", other);

  87.     if (other == this) return get();
  88.     SynchronizedInt fst = this;
  89.     SynchronizedInt snd = other;
  90.     if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
  91.       fst = other;
  92.       snd = this;
  93.     }
  94.     synchronized(fst.lock_) {
  95.       synchronized(snd.lock_) {
  96.         fst.set(snd.set(fst.get()));
  97.         return get();
  98.       }
  99.     }
  100.   }

  101.   /**
  102.    * Increment the value.
  103.    * @return the new value
  104.    **/
  105.   public int increment() {
  106.     synchronized (lock_) {
  107.       return ++value_;
  108.     }
  109.   }

  110.   /**
  111.    * Decrement the value.
  112.    * @return the new value
  113.    **/
  114.   public int decrement() {
  115.     synchronized (lock_) {
  116.       return --value_;
  117.     }
  118.   }

  119.   /**
  120.    * Add amount to value (i.e., set value += amount)
  121.    * @return the new value
  122.    **/
  123.   public int add(int amount) {
  124.     synchronized (lock_) {
  125.       return value_ += amount;
  126.     }
  127.   }

  128.   /**
  129.    * Subtract amount from value (i.e., set value -= amount)
  130.    * @return the new value
  131.    **/
  132.   public int subtract(int amount) {
  133.     synchronized (lock_) {
  134.       return value_ -= amount;
  135.     }
  136.   }

  137.   /**
  138.    * Multiply value by factor (i.e., set value *= factor)
  139.    * @return the new value
  140.    **/
  141.   public synchronized int multiply(int factor) {
  142.     synchronized (lock_) {
  143.       return value_ *= factor;
  144.     }
  145.   }

  146.   /**
  147.    * Divide value by factor (i.e., set value /= factor)
  148.    * @return the new value
  149.    **/
  150.   public int divide(int factor) {
  151.     synchronized (lock_) {
  152.       return value_ /= factor;
  153.     }
  154.   }

  155.   /**
  156.    * Set the value to the negative of its old value
  157.    * @return the new value
  158.    **/
  159.   public int negate() {
  160.     synchronized (lock_) {
  161.       value_ = -value_;
  162.       return value_;
  163.     }
  164.   }

  165.   /**
  166.    * Set the value to its complement
  167.    * @return the new value
  168.    **/
  169.   public  int complement() {
  170.     synchronized (lock_) {
  171.       value_ = ~value_;
  172.       return value_;
  173.     }
  174.   }

  175.   /**
  176.    * Set value to value &amp; b.
  177.    * @return the new value
  178.    **/
  179.   public  int and(int b) {
  180.     synchronized (lock_) {
  181.       value_ = value_ & b;
  182.       return value_;
  183.     }
  184.   }

  185.   /**
  186.    * Set value to value | b.
  187.    * @return the new value
  188.    **/
  189.   public  int or(int b) {
  190.     synchronized (lock_) {
  191.       value_ = value_ | b;
  192.       return value_;
  193.     }
  194.   }


  195.   /**
  196.    * Set value to value ^ b.
  197.    * @return the new value
  198.    **/
  199.   public  int xor(int b) {
  200.     synchronized (lock_) {
  201.       value_ = value_ ^ b;
  202.       return value_;
  203.     }
  204.   }

  205.   public int compareTo(int other) {
  206.     logger.debug("compareTo(other={}) - start", String.valueOf(other));
  207.     int val = get();
  208.     return (val < other)? -1 : (val == other)? 0 : 1;
  209.   }

  210.   public int compareTo(SynchronizedInt other) {
  211.      logger.debug("compareTo(other={}) - start", other);
  212.     return compareTo(other.get());
  213.   }

  214.   public int compareTo(Object other) {
  215.     logger.debug("compareTo(other={}) - start", other);
  216.     return compareTo((SynchronizedInt)other);
  217.   }

  218.   public boolean equals(Object other) {
  219.     logger.debug("equals(other={}) - start", other);
  220.     if (other != null &&
  221.         other instanceof SynchronizedInt)
  222.       return get() == ((SynchronizedInt)other).get();
  223.     else
  224.       return false;
  225.   }

  226.   public int hashCode() {
  227.     return get();
  228.   }

  229.   public String toString() {
  230.     return String.valueOf(get());
  231.   }

  232. }