Class Worm<T>

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

public class Worm<T> extends Object
This class wraps an object of type T and serves as a write once read many (worm) memory.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    get()
    If a value is present, returns the value, otherwise throws NoSuchElementException.
    getOrCompute(Supplier<T> supplier)
    If no value has been set yet, it will be set to the return of supplier.
    getOrSet(T value)
    If no value has been set yet, it will be set to value.
    int
     
    boolean
    If a value is not present, returns true, otherwise false.
    boolean
    If a value is present, returns true, otherwise false.
    static <T> Worm<T>
    of()
    Creates a new Worm object with not set value.
    static <T> Worm<T>
    of(T value)
    Creates a new Worm object with value set to value.
    void
    set(T value)
    Sets the value to value.
     
    boolean
    trySet(T value)
    Sets the value to value if no value is present yet.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • get

      public T get()
      If a value is present, returns the value, otherwise throws NoSuchElementException.
      Returns:
      the value described by this Worm
      Throws:
      NoSuchElementException - if no value is present
    • set

      public void set(T value)
      Sets the value to value.
      Parameters:
      value - the new value
      Throws:
      IllegalStateException - if value is already present
    • trySet

      public boolean trySet(T value)
      Sets the value to value if no value is present yet.
      Parameters:
      value - the new value
      Returns:
      returns true, if value has been set to value
    • getOrCompute

      public T getOrCompute(Supplier<T> supplier)
      If no value has been set yet, it will be set to the return of supplier. Otherwise the existing value is returned.
      Parameters:
      supplier - is used to compute the value
      Returns:
      the value of this Worm
    • getOrSet

      public T getOrSet(T value)
      If no value has been set yet, it will be set to value. Otherwise the existing value is returned.
      Parameters:
      value - is used to set the value
      Returns:
      the value of this Worm
    • isPresent

      public boolean isPresent()
      If a value is present, returns true, otherwise false.
      Returns:
      true if a value is present, otherwise false
    • isEmpty

      public boolean isEmpty()
      If a value is not present, returns true, otherwise false.
      Returns:
      true if a value is not present, otherwise false
    • 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
    • of

      public static <T> Worm<T> of()
      Creates a new Worm object with not set value.
      Type Parameters:
      T - the type of the Worm object
      Returns:
      the Worm object
    • of

      public static <T> Worm<T> of(T value)
      Creates a new Worm object with value set to value.
      Type Parameters:
      T - the type of the Worm object
      Returns:
      the Worm object