Package org.drasyl.util
Class ConcurrentReference<T>
- java.lang.Object
-
- org.drasyl.util.ConcurrentReference<T>
-
- Type Parameters:
T
- the type of theConcurrentReference
object
public class ConcurrentReference<T> extends Object
This class wraps an object of typeT
and serves as a write many read many memory. The advantage of this class is that all methods are lock free until a given condition is met for a write/update operation. Therefore this class requires less locks than anAtomicReference
.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description T
computeIfAbsent(Supplier<T> supplier)
Sets the write many read many memory to the return value ofsupplier
ifvalue
isObjects.isNull(Object)
.Optional<T>
computeOnCondition(Predicate<T> condition, UnaryOperator<T> unaryFunction)
Sets the write many read many memory to the return value ofunaryFunction
ifvalue
fulfills thecondition
.boolean
equals(Object o)
Optional<T>
getValue()
int
hashCode()
static <T> ConcurrentReference<T>
of()
Creates a new empty write many read many memory.static <T> ConcurrentReference<T>
of(T initialValue)
Creates a new write many read many memory with the initial value ofinitialValue
.String
toString()
-
-
-
Method Detail
-
of
public static <T> ConcurrentReference<T> of()
Creates a new empty write many read many memory.- Type Parameters:
T
- the type of theConcurrentReference
object- Returns:
- an empty write many read many memory
-
of
public static <T> ConcurrentReference<T> of(T initialValue)
Creates a new write many read many memory with the initial value ofinitialValue
.- Type Parameters:
T
- the type of theConcurrentReference
object- Returns:
- a write many read many memory with the initial value of
initialValue
-
computeIfAbsent
public T computeIfAbsent(Supplier<T> supplier)
Sets the write many read many memory to the return value ofsupplier
ifvalue
isObjects.isNull(Object)
. Otherwise, nothing happens. Blocks only if the ifvalue
isObjects.isNull(Object)
and another thread tries to read or write concurrently the#value
.- Parameters:
supplier
- the value supplier- Returns:
- the value after applying the
supplier
or the old value if notnull
-
getValue
public Optional<T> getValue()
- Returns:
- returns the internal optional that holds the current value
-
computeOnCondition
public Optional<T> computeOnCondition(Predicate<T> condition, UnaryOperator<T> unaryFunction)
Sets the write many read many memory to the return value ofunaryFunction
ifvalue
fulfills thecondition
. Otherwise, nothing happens. Blocks only ifcondition
is fulfilled and another thread tries to read or write concurrently the#value
.- Parameters:
condition
- the condition to testunaryFunction
- the unary function to apply- Returns:
- the value after applying the
unaryFunction
or the old value ifcondition
not fulfilled
-
-