Class ConcurrentReference<T>

java.lang.Object
org.drasyl.util.ConcurrentReference<T>
Type Parameters:
T - the type of the ConcurrentReference object

public class ConcurrentReference<T> extends 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 Details

    • of

      public static <T> ConcurrentReference<T> of()
      Creates a new empty write many read many memory.
      Type Parameters:
      T - the type of the ConcurrentReference 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 of initialValue.
      Type Parameters:
      T - the type of the ConcurrentReference 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 of supplier if value is Objects.isNull(Object). Otherwise, nothing happens. Blocks only if the if value is Objects.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 not null
    • 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 of unaryFunction if value fulfills the condition. Otherwise, nothing happens. Blocks only if condition is fulfilled and another thread tries to read or write concurrently the #value.
      Parameters:
      condition - the condition to test
      unaryFunction - the unary function to apply
      Returns:
      the value after applying the unaryFunction or the old value if condition not fulfilled
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object