Package org.drasyl.util
Class ConcurrentReference<T>
java.lang.Object
org.drasyl.util.ConcurrentReference<T>
- Type Parameters:
T
- the type of theConcurrentReference
object
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 ofsupplier
ifvalue
isObjects.isNull(Object)
.computeOnCondition
(Predicate<T> condition, UnaryOperator<T> unaryFunction) Sets the write many read many memory to the return value ofunaryFunction
ifvalue
fulfills thecondition
.boolean
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
.toString()
-
Method Details
-
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
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
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
- Returns:
- returns the internal optional that holds the current value
-
computeOnCondition
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
-
hashCode
public int hashCode() -
equals
-
toString
-