/**
 * An encapsulation of a MutableInteger that can be used to
 * demonstrate how synchronization can avoid the problems that arise
 * when using shared mutable state.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class SynchronizedInteger implements MutableInteger
{
    private int    value;
 
    /**
     * Explicit Value Constructor.
     *
     * @param initial   The initial value
     */
    public SynchronizedInteger(int initial)
    {
        set(initial);
    }
    
    /**
     * Get the current value of this MutableInteger.
     *
     * @return  The current value
     */
    public synchronized int get()
    {
        return value;
    }
   
    /**
     * Increment (and return) the value of this MutableInteger.
     *
     * @return  The current value (i.e., after it is incremented)
     */
    public synchronized int incrementAndGet()
    {
        ++value;
        return value;
    }
    
    /**
     * Set the value of this MutableInteger.
     *
     * @param value  The new value
     */
    public synchronized void set(int value)
    {
        this.value = value;
    }
}
