Package org.drasyl.util
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 typeTand serves as a write once read many (worm) memory.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object o)Tget()If a value is present, returns the value, otherwise throwsNoSuchElementException.TgetOrCompute(Supplier<T> supplier)If no value has been set yet, it will be set to the return ofsupplier.TgetOrSet(T value)If no value has been set yet, it will be set tovalue.inthashCode()booleanisEmpty()If a value is not present, returnstrue, otherwisefalse.booleanisPresent()If a value is present, returnstrue, otherwisefalse.static <T> Worm<T>of()Creates a newWormobject with not set value.static <T> Worm<T>of(T value)Creates a newWormobject with value set tovalue.voidset(T value)Sets the value tovalue,StringtoString()
-
-
-
Method Detail
-
get
public T get()
If a value is present, returns the value, otherwise throwsNoSuchElementException.- Returns:
- the value described by this
Worm - Throws:
NoSuchElementException- if no value is present
-
set
public void set(T value)
Sets the value tovalue,- Parameters:
value- the new value- Throws:
IllegalStateException- if value is already present
-
getOrCompute
public T getOrCompute(Supplier<T> supplier)
If no value has been set yet, it will be set to the return ofsupplier. 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 tovalue. 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, returnstrue, otherwisefalse.- Returns:
trueif a value is present, otherwisefalse
-
isEmpty
public boolean isEmpty()
If a value is not present, returnstrue, otherwisefalse.- Returns:
trueif a value is not present, otherwisefalse
-
of
public static <T> Worm<T> of()
Creates a newWormobject with not set value.- Type Parameters:
T- the type of theWormobject- Returns:
- the
Wormobject
-
of
public static <T> Worm<T> of(T value)
Creates a newWormobject with value set tovalue.- Type Parameters:
T- the type of theWormobject- Returns:
- the
Wormobject
-
-