V - The type of the object which is referenced.public final class OneTimeAssignment<V> extends Object implements ViewableAsOptional<V>
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.
| Modifier and Type | Class and Description |
|---|---|
static class |
OneTimeAssignment.AlreadyAssigned |
ViewableAsOptional.TooManyElements| Modifier and Type | Method and Description |
|---|---|
static <V> OneTimeAssignment<V> |
defaultTo(Supplier<? extends V> defaultValue)
Create a new non-assigned instance with a default value.
|
static <V> OneTimeAssignment<V> |
defaultTo(V defaultValue)
Create a new non-assigned instance with a default value.
|
V |
get() |
static <V> OneTimeAssignment<V> |
newInstance()
Create a new non-assigned instance.
|
void |
set(V value)
Assigns a value to this reference.
|
Optional<V> |
toOptional()
Convert this
OneTimeAssignment to an Optional. |
public static final <V> OneTimeAssignment<V> defaultTo(V defaultValue)
defaultValue - The default value to use in case an assignment
through set(Object) is never performed.defaultTo(Supplier)public static final <V> OneTimeAssignment<V> defaultTo(Supplier<? extends V> defaultValue)
defaultValue - The supplier to acquire the default value to use in case an
assignment through set(Object) is never performed.public static final <V> OneTimeAssignment<V> newInstance()
public void set(V value)
value - the value to set.OneTimeAssignment.AlreadyAssigned - if this reference is already assigned a value.public V get()
initialized with a default value,
the reference is assigned the default value and this is returned.public Optional<V> toOptional()
OneTimeAssignment to an Optional. This
method will never throw an exception. Calling this on an unassigned OneTimeAssignment
with a default value, will effectively assign to the default value before wrapping it in
an Optional.toOptional in interface ViewableAsOptional<V>Optional representation of this object.Copyright © 2016 Digipost. All rights reserved.