Package org.drasyl.util
Class Worm<T>
java.lang.Object
org.drasyl.util.Worm<T>
- Type Parameters:
T
- the type of the worm object
This class wraps an object of type
T
and serves as a write once read many (worm) memory.-
Method Summary
Modifier and TypeMethodDescriptionboolean
get()
If a value is present, returns the value, otherwise throwsNoSuchElementException
.getOrCompute
(Supplier<T> supplier) If no value has been set yet, it will be set to the return ofsupplier
.If no value has been set yet, it will be set tovalue
.int
hashCode()
boolean
isEmpty()
If a value is not present, returnstrue
, otherwisefalse
.boolean
If a value is present, returnstrue
, otherwisefalse
.static <T> Worm<T>
of()
Creates a newWorm
object with not set value.static <T> Worm<T>
of
(T value) Creates a newWorm
object with value set tovalue
.void
Sets the value tovalue
.toString()
boolean
Sets the value tovalue
if no value is present yet.
-
Method Details
-
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
Sets the value tovalue
.- Parameters:
value
- the new value- Throws:
IllegalStateException
- if value is already present
-
trySet
Sets the value tovalue
if no value is present yet.- Parameters:
value
- the new value- Returns:
- returns
true
, if value has been set tovalue
-
getOrCompute
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
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:
true
if a value is present, otherwisefalse
-
isEmpty
public boolean isEmpty()If a value is not present, returnstrue
, otherwisefalse
.- Returns:
true
if a value is not present, otherwisefalse
-
hashCode
public int hashCode() -
equals
-
toString
-
of
Creates a newWorm
object with not set value.- Type Parameters:
T
- the type of theWorm
object- Returns:
- the
Worm
object
-
of
Creates a newWorm
object with value set tovalue
.- Type Parameters:
T
- the type of theWorm
object- Returns:
- the
Worm
object
-