com.yost.util
Class AtomicReferenceWithWait<T>

java.lang.Object
  extended by com.yost.util.AtomicReferenceWithWait<T>

public class AtomicReferenceWithWait<T>
extends Object

An object reference that can be updated atomically, and which allows clients to wait for the value to change to suit their needs. Unlike AtomicReference,

See the java.util.concurrent.atomic package specification.


Constructor Summary
AtomicReferenceWithWait()
          Create a new AtomicReferenceWithWait with null initial value.
AtomicReferenceWithWait(T initialValue)
          Create a new AtomicReferenceWithWait with the given initial value.
 
Method Summary
 boolean compareEqualsAndSet(T expect, T update)
          Atomically set the value to the given updated value if the current value equals the expected value.
 boolean compareIsAndSet(T expect, T update)
          Atomically set the value to the given updated value if the current value == the expected value.
 boolean compareIsNotAndSet(T expect, T update)
          Atomically set the value to the given updated value if the current value != the expected value.
 boolean compareNotEqualsAndSet(T expect, T update)
          Atomically set the value to the given updated value if the current value !equals the expected value.
 T get()
          Get the current value.
 T getAndSet(T newValue)
          Set to the given value and return the old value.
 Object getLockObject()
          Get the monitor lock object.
 void set(T newValue)
          Set to the given value.
 void setLockObject(Object newValue)
          Set the monitor lock object.
 String toString()
          Returns the String representation of the current value.
 T waitUntilValueEquals(T valueWanted)
          Wait until value equals the desired value, then return the current value.
 T waitUntilValueEqualsThenSet(T valueWanted, T newValue)
          Wait until value equals the desired object, then set to newValue and return the old value.
 boolean waitUntilValueIs(T objectReferenceWanted)
          Wait until value == the desired object, then return true if no waiting was necessary.
 T waitUntilValueIsNot(T objectReferenceNotWanted)
          Wait until value != the desired object, then return the current value.
 T waitUntilValueIsNotThenSet(T objectReferenceWanted, T newValue)
          Wait until value != the desired object, then set to newValue and return the old value.
 boolean waitUntilValueIsThenSet(T objectReferenceWanted, T newValue)
          Wait until value == the desired object, then set to newValue and return true if no waiting was necessary.
 T waitUntilValueNotEquals(T valueNotWanted)
          Wait until value !equals the desired value, then return the current value.
 T waitUntilValueNotEqualsThenSet(T valueWanted, T newValue)
          Wait until value not equals the desired object, then set to newValue and return the old value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AtomicReferenceWithWait

public AtomicReferenceWithWait(T initialValue)
Create a new AtomicReferenceWithWait with the given initial value.

Parameters:
initialValue - the initial value

AtomicReferenceWithWait

public AtomicReferenceWithWait()
Create a new AtomicReferenceWithWait with null initial value.

Method Detail

get

public T get()
Get the current value.

Returns:
the current value

set

public void set(T newValue)
Set to the given value.

Parameters:
newValue - the new value

compareIsAndSet

public final boolean compareIsAndSet(T expect,
                                     T update)
Atomically set the value to the given updated value if the current value == the expected value.

Parameters:
expect - the expected value
update - the new value
Returns:
true if successful. False return indicates that the current value was not equal to the expected value.

compareEqualsAndSet

public final boolean compareEqualsAndSet(T expect,
                                         T update)
Atomically set the value to the given updated value if the current value equals the expected value.

Parameters:
expect - the expected value
update - the new value
Returns:
true if successful. False return indicates that the current value was not equal to the expected value.

compareIsNotAndSet

public final boolean compareIsNotAndSet(T expect,
                                        T update)
Atomically set the value to the given updated value if the current value != the expected value.

Parameters:
expect - the expected value
update - the new value
Returns:
true if successful. False return indicates that the current value was not equal to the expected value.

compareNotEqualsAndSet

public final boolean compareNotEqualsAndSet(T expect,
                                            T update)
Atomically set the value to the given updated value if the current value !equals the expected value.

Parameters:
expect - the expected value
update - the new value
Returns:
true if successful. False return indicates that the current value was not equal to the expected value.

getAndSet

public final T getAndSet(T newValue)
Set to the given value and return the old value.

Parameters:
newValue - the new value
Returns:
the previous value

waitUntilValueIs

public final boolean waitUntilValueIs(T objectReferenceWanted)
                               throws InterruptedException
Wait until value == the desired object, then return true if no waiting was necessary.

Parameters:
objectReferenceWanted - the desired object
Returns:
true if no waiting necessary
Throws:
InterruptedException

waitUntilValueEquals

public final T waitUntilValueEquals(T valueWanted)
                             throws InterruptedException
Wait until value equals the desired value, then return the current value.

Parameters:
valueWanted - the current value
Returns:
the current value
Throws:
InterruptedException

waitUntilValueIsNot

public final T waitUntilValueIsNot(T objectReferenceNotWanted)
                            throws InterruptedException
Wait until value != the desired object, then return the current value.

Parameters:
objectReferenceNotWanted - the undesired object
Returns:
the current value
Throws:
InterruptedException

waitUntilValueNotEquals

public T waitUntilValueNotEquals(T valueNotWanted)
                          throws InterruptedException
Wait until value !equals the desired value, then return the current value.

Parameters:
valueNotWanted - the undesired value
Returns:
the current value
Throws:
InterruptedException

waitUntilValueIsThenSet

public final boolean waitUntilValueIsThenSet(T objectReferenceWanted,
                                             T newValue)
                                      throws InterruptedException
Wait until value == the desired object, then set to newValue and return true if no waiting was necessary.

Parameters:
objectReferenceWanted - the desired object
newValue - the new value
Returns:
true if no waiting necessary
Throws:
InterruptedException

waitUntilValueEqualsThenSet

public final T waitUntilValueEqualsThenSet(T valueWanted,
                                           T newValue)
                                    throws InterruptedException
Wait until value equals the desired object, then set to newValue and return the old value.

Parameters:
valueWanted - the desired object
newValue - the new value
Returns:
the old value
Throws:
InterruptedException

waitUntilValueIsNotThenSet

public final T waitUntilValueIsNotThenSet(T objectReferenceWanted,
                                          T newValue)
                                   throws InterruptedException
Wait until value != the desired object, then set to newValue and return the old value.

Parameters:
objectReferenceWanted - the desired object
newValue - the new value
Returns:
the old value
Throws:
InterruptedException

waitUntilValueNotEqualsThenSet

public final T waitUntilValueNotEqualsThenSet(T valueWanted,
                                              T newValue)
                                       throws InterruptedException
Wait until value not equals the desired object, then set to newValue and return the old value.

Parameters:
valueWanted - the desired object
newValue - the new value
Returns:
the old value
Throws:
InterruptedException

getLockObject

public Object getLockObject()
Get the monitor lock object.

Returns:
the monitor lock object

setLockObject

public void setLockObject(Object newValue)
Set the monitor lock object.

Parameters:
newValue - the new monitor lock object

toString

public String toString()
Returns the String representation of the current value.

Overrides:
toString in class Object
Returns:
the String representation of the current value