com.atlassian.util.concurrent.atomic
Class Atomics

java.lang.Object
  extended by com.atlassian.util.concurrent.atomic.Atomics

@ThreadSafe
public final class Atomics
extends java.lang.Object

Utility methods for handling the specifics of correctly using the CAS operations on AtomicReference classes and the like.

All methods are thread safe.

Since:
0.0.12

Method Summary
static boolean getAndSetIf(java.util.concurrent.atomic.AtomicBoolean reference, boolean oldValue, boolean newValue)
          Get the current value of the reference but if it matches the oldValue argument, compare-and-set it to one created by the newValue Supplier.
static long getAndSetIf(java.util.concurrent.atomic.AtomicInteger reference, int oldValue, int newValue)
          Get the current value of the reference but if it matches the oldValue argument, compare-and-set it to one created by the newValue Supplier.
static long getAndSetIf(java.util.concurrent.atomic.AtomicLong reference, long oldValue, long newValue)
          Get the current value of the reference but if it matches the oldValue argument, compare-and-set it the new value.
static
<T> T
getAndSetIf(java.util.concurrent.atomic.AtomicReference<T> reference, T oldValue, com.google.common.base.Supplier<T> newValue)
          Get the current value of the reference but if it matches the oldValue argument, compare-and-set it to one created by the new value supplier.
static
<T> T
getAndSetIf(java.util.concurrent.atomic.AtomicReference<T> reference, T oldValue, T newValue)
          Check the current value of the reference and if it matches the old value argument, compare-and-set it to the new value and return that instead.
static
<T> T
getAndSetIf(java.util.concurrent.atomic.AtomicReferenceArray<T> reference, int index, T oldValue, com.google.common.base.Supplier<T> newValue)
          Get the current value of the array reference but if it matches the oldValue argument, compare-and-set it to one created by the new value supplier.
static
<T> T
getAndSetIf(java.util.concurrent.atomic.AtomicReferenceArray<T> reference, int index, T oldValue, T newValue)
          Get the current value of the array reference but if it matches the oldValue argument, compare-and-set it the new value.
static
<T> T
getAndSetIfNull(java.util.concurrent.atomic.AtomicReference<T> reference, com.google.common.base.Supplier<T> newValue)
          Get the current value of the reference but if it is null, compare-and-set it to one created by the new value Supplier.
static
<T> T
getAndSetIfNull(java.util.concurrent.atomic.AtomicReference<T> reference, T newValue)
          Get the current value of the reference but if it is null, compare-and-set it the new value.
static
<T> T
getAndSetIfNull(java.util.concurrent.atomic.AtomicReferenceArray<T> reference, int index, com.google.common.base.Supplier<T> newValue)
          Get the current value of the array reference but if it is null, compare-and-set it to one created by the new value Supplier.
static
<T> T
getAndSetIfNull(java.util.concurrent.atomic.AtomicReferenceArray<T> reference, int index, T newValue)
          Get the current value of the array reference but if it is null, compare-and-set it the new value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAndSetIf

public static <T> T getAndSetIf(java.util.concurrent.atomic.AtomicReference<T> reference,
                                T oldValue,
                                com.google.common.base.Supplier<T> newValue)
Get the current value of the reference but if it matches the oldValue argument, compare-and-set it to one created by the new value supplier.

Type Parameters:
T - the object type.
Parameters:
reference - the reference to get the value from.
oldValue - to check the current value against (reference equality check only).
newValue - a Supplier for a new value. May be called more than once.
Returns:
the current reference value if it doesn't match oldValue or a newly created value.

getAndSetIf

public static <T> T getAndSetIf(java.util.concurrent.atomic.AtomicReference<T> reference,
                                T oldValue,
                                T newValue)
Check the current value of the reference and if it matches the old value argument, compare-and-set it to the new value and return that instead. If the old value argument does not match, ignore both and return the current value.

Type Parameters:
T - the object type.
Parameters:
reference - the reference to get the value from
oldValue - to check the current value against (reference equality check only)
newValue - the new value to set it to
Returns:
the current reference value if it doesn't match oldValue or a newly created value.

getAndSetIfNull

public static <T> T getAndSetIfNull(java.util.concurrent.atomic.AtomicReference<T> reference,
                                    com.google.common.base.Supplier<T> newValue)
Get the current value of the reference but if it is null, compare-and-set it to one created by the new value Supplier.

Type Parameters:
T - the object type.
Parameters:
reference - the reference to get the value from
newValue - a Supplier for a new value. May be called more than once.
Returns:
the current reference value if it doesn't match oldValue or a newly created value.

getAndSetIfNull

public static <T> T getAndSetIfNull(java.util.concurrent.atomic.AtomicReference<T> reference,
                                    T newValue)
Get the current value of the reference but if it is null, compare-and-set it the new value.

Type Parameters:
T - the object type.
Parameters:
reference - the reference to get the value from
newValue - the new value.
Returns:
the current reference value if it doesn't match oldValue or a newly created value.

getAndSetIf

public static <T> T getAndSetIf(java.util.concurrent.atomic.AtomicReferenceArray<T> reference,
                                int index,
                                T oldValue,
                                com.google.common.base.Supplier<T> newValue)
Get the current value of the array reference but if it matches the oldValue argument, compare-and-set it to one created by the new value supplier.

Type Parameters:
T - the object type.
Parameters:
index - the index to the item
reference - the reference to get the value from
oldValue - to check the current value against (reference equality check only)
newValue - a Supplier for a new value. May be called more than once.
Returns:
the current reference value if it doesn't match oldValue or a newly created value.
Throws:
java.lang.IndexOutOfBoundsException - if the index is less than 0 or equal or greater than the array size

getAndSetIf

public static <T> T getAndSetIf(java.util.concurrent.atomic.AtomicReferenceArray<T> reference,
                                int index,
                                T oldValue,
                                T newValue)
Get the current value of the array reference but if it matches the oldValue argument, compare-and-set it the new value.

Type Parameters:
T - the object type.
Parameters:
index - the index to the item
reference - the reference to get the value from
oldValue - to check the current value against (reference equality check only)
newValue - the new value.
Returns:
the current reference value if it doesn't match oldValue or a newly created value.
Throws:
java.lang.IndexOutOfBoundsException - if the index is less than 0 or equal or greater than the array size

getAndSetIfNull

public static <T> T getAndSetIfNull(java.util.concurrent.atomic.AtomicReferenceArray<T> reference,
                                    int index,
                                    com.google.common.base.Supplier<T> newValue)
Get the current value of the array reference but if it is null, compare-and-set it to one created by the new value Supplier.

Type Parameters:
T - the object type.
Parameters:
index - the index to the item.
reference - the reference to get the value from.
newValue - a Supplier for a new value. May be called more than once.
Returns:
the current reference value if it doesn't match oldValue or a newly created value.
Throws:
java.lang.IndexOutOfBoundsException - if the index is less than 0 or equal or greater than the array size.

getAndSetIfNull

public static <T> T getAndSetIfNull(java.util.concurrent.atomic.AtomicReferenceArray<T> reference,
                                    int index,
                                    T newValue)
Get the current value of the array reference but if it is null, compare-and-set it the new value

Type Parameters:
T - the object type.
Parameters:
index - the index to the item.
reference - the reference to get the value from.
newValue - the new value.
Returns:
the current reference value if it doesn't match oldValue or the new value.
Throws:
java.lang.IndexOutOfBoundsException - if the index is less than 0 or equal or greater than the array size.

getAndSetIf

public static long getAndSetIf(java.util.concurrent.atomic.AtomicLong reference,
                               long oldValue,
                               long newValue)
Get the current value of the reference but if it matches the oldValue argument, compare-and-set it the new value.

Type Parameters:
T - the object type.
Parameters:
reference - the reference to get the value from
oldValue - to check the current value against.
newValue - the new value.
Returns:
the current value if it doesn't match the old value otherwise the new value.

getAndSetIf

public static long getAndSetIf(java.util.concurrent.atomic.AtomicInteger reference,
                               int oldValue,
                               int newValue)
Get the current value of the reference but if it matches the oldValue argument, compare-and-set it to one created by the newValue Supplier.

Type Parameters:
T - the object type.
Parameters:
reference - the reference to get the value from
oldValue - to check the current value against.
newValue - the new value.
Returns:
the current value if it doesn't match the old value otherwise the new value.

getAndSetIf

public static boolean getAndSetIf(java.util.concurrent.atomic.AtomicBoolean reference,
                                  boolean oldValue,
                                  boolean newValue)
Get the current value of the reference but if it matches the oldValue argument, compare-and-set it to one created by the newValue Supplier.

Type Parameters:
T - the object type.
Parameters:
reference - the reference to get the value from
oldValue - to check the current value against.
newValue - the new value.
Returns:
the current value if it doesn't match the old value otherwise the new value.


Copyright © 2011 Atlassian. All Rights Reserved.