Package org.drasyl.util
Class ConcurrentReference<T>
java.lang.Object
org.drasyl.util.ConcurrentReference<T>
- Type Parameters:
T- the type of theConcurrentReferenceobject
This class wraps an object of type
T 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 an
AtomicReference.-
Method Summary
Modifier and TypeMethodDescriptioncomputeIfAbsent(Supplier<T> supplier) Sets the write many read many memory to the return value ofsupplierifvalueisObjects.isNull(Object).computeOnCondition(Predicate<T> condition, UnaryOperator<T> unaryFunction) Sets the write many read many memory to the return value ofunaryFunctionifvaluefulfills thecondition.booleangetValue()inthashCode()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.toString()
-
Method Details
-
of
Creates a new empty write many read many memory.- Type Parameters:
T- the type of theConcurrentReferenceobject- Returns:
- an empty write many read many memory
-
of
Creates a new write many read many memory with the initial value ofinitialValue.- Type Parameters:
T- the type of theConcurrentReferenceobject- Returns:
- a write many read many memory with the initial value of
initialValue
-
computeIfAbsent
Sets the write many read many memory to the return value ofsupplierifvalueisObjects.isNull(Object). Otherwise, nothing happens. Blocks only if the ifvalueisObjects.isNull(Object)and another thread tries to read or write concurrently the#value.- Parameters:
supplier- the value supplier- Returns:
- the value after applying the
supplieror the old value if notnull
-
getValue
- Returns:
- returns the internal optional that holds the current value
-
computeOnCondition
Sets the write many read many memory to the return value ofunaryFunctionifvaluefulfills thecondition. Otherwise, nothing happens. Blocks only ifconditionis 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
unaryFunctionor the old value ifconditionnot fulfilled
-
hashCode
public int hashCode() -
equals
-
toString
-