Package org.opensearch.common
Class SetOnce<T>
java.lang.Object
org.opensearch.common.SetOnce<T>
- All Implemented Interfaces:
Cloneable
A convenient class which offers a semi-immutable object wrapper implementation which allows one
to set the value of an object exactly once, and retrieve it many times. If
set(Object)
is called more than once, SetOnce.AlreadySetException is thrown and the operation will fail.
This is borrowed from lucene's experimental API. It is not reused to eliminate the dependency
on lucene core for such a simple (standalone) utility class that may change beyond OpenSearch needs.- Opensearch.api:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThrown whenset(Object)is called more than once. -
Constructor Summary
ConstructorsConstructorDescriptionSetOnce()A default constructor which does not set the internal object, and allows setting it by callingset(Object).Creates a new instance with the internal object set to the given object. -
Method Summary
-
Constructor Details
-
SetOnce
public SetOnce()A default constructor which does not set the internal object, and allows setting it by callingset(Object). -
SetOnce
Creates a new instance with the internal object set to the given object. Note that any calls toset(Object)afterwards will result inSetOnce.AlreadySetException- Throws:
SetOnce.AlreadySetException- if called more than once- See Also:
-
-
Method Details
-
set
Sets the given object. If the object has already been set, an exception is thrown. -
trySet
Sets the given object if none was set before.- Returns:
- true if object was set successfully, false otherwise
-
get
Returns the object set byset(Object).
-