Package no.digipost.concurrent
Class OneTimeAssignment<V>
java.lang.Object
no.digipost.concurrent.OneTimeAssignment<V>
- Type Parameters:
V- The type of the object which is referenced.
- All Implemented Interfaces:
Consumer<V>,Supplier<V>,Assignment<V>,ViewableAsOptional<V>,ViewableAsOptional.Single<V>
A reference which may or may not be assigned a value, with the added constraint
that it can only be assigned once. If it is assigned a value more than one time
it will throw an exception.
The class is thread-safe in the sense that it is not possible for concurrent threads to assign a value to it twice, though relevant cases for concurrent use are probably limited. The motivation is rather to enable fail-fast for erroneous API-usage involving assignments, e.g. builder implementations.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface no.digipost.util.ViewableAsOptional
ViewableAsOptional.Single<V>, ViewableAsOptional.TooManyElements -
Method Summary
Modifier and TypeMethodDescriptionstatic final <V> OneTimeAssignment<V>Create a new non-assigned instance with a default value.static final <V> OneTimeAssignment<V>defaultTo(V defaultValue) Create a new non-assigned instance with a default value.get()booleanisSet()Determine if thisOneTimeAssignmenthas been set, either by usingset(Object)or implicitly by adefault value, triggered with a call toget().static final <V> OneTimeAssignment<V>Create a new non-assigned instance.voidAssigns a value to this reference.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface no.digipost.util.Assignment
accept, chainableWithMethods inherited from interface no.digipost.util.ViewableAsOptional.Single
toOptional
-
Method Details
-
defaultTo
Create a new non-assigned instance with a default value.- Parameters:
defaultValue- The default value to use in case an assignment throughset(Object)is never performed.- See Also:
-
defaultTo
Create a new non-assigned instance with a default value.- Parameters:
defaultValue- The supplier to acquire the default value to use in case an assignment throughset(Object)is never performed.
-
newInstance
Create a new non-assigned instance. -
set
Assigns a value to this reference. This method can only be called once.- Specified by:
setin interfaceAssignment<V>- Parameters:
value- the value to set.- Throws:
OneTimeAssignment.AlreadyAssigned- if this reference is already assigned a value.
-
isSet
public boolean isSet()Determine if thisOneTimeAssignmenthas been set, either by usingset(Object)or implicitly by adefault value, triggered with a call toget().For sake of clarity, an invocation of this method does never implicitly assign any default value. An assignment with no default value requires an invocation of
set(Object)for this method to returntrue.- Returns:
trueif the assignment has a value,falseif it has not been assigned yet.
-
get
- Specified by:
getin interfaceSupplier<V>- Returns:
- the referenced value. If the reference has not yet been set but is
initialized with a default value, the reference is assigned the default value and this is returned.
-