Package org.apache.lucene.util

# Class SetOnce<T>

java.lang.Object
org.apache.lucene.util.SetOnce<T>

All Implemented Interfaces:
  `Cloneable`

public final class SetOnce<T> extends Object implements 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.

WARNING: This API is experimental and might change in incompatible ways in the next release.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static final class`
  `SetOnce.AlreadySetException`
  Thrown when `set(Object)` is called more than once.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SetOnce()`
  A default constructor which does not set the internal object, and allows setting it by calling `set(Object)`.
  `SetOnce(T obj)`
  Creates a new instance with the internal object set to the given object.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `final T`
  `get()`
  Returns the object set by `set(Object)`.
  `final void`
  `set(T obj)`
  Sets the given object.
  `final boolean`
  `trySet(T obj)`
  Sets the given object if none was set before.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### SetOnce

    public SetOnce()
    A default constructor which does not set the internal object, and allows setting it by calling `set(Object)`.

  - ### SetOnce

    public SetOnce(T obj)
    Creates a new instance with the internal object set to the given object. Note that any calls to `set(Object)` afterwards will result in `SetOnce.AlreadySetException`
    Throws:
      `SetOnce.AlreadySetException` - if called more than once
    See Also:
      - `set(Object)`

- ## Method Details

  - ### set

    public final void set(T obj)
    Sets the given object. If the object has already been set, an exception is thrown.

  - ### trySet

    public final boolean trySet(T obj)
    Sets the given object if none was set before.
    Returns:
      true if object was set successfully, false otherwise

  - ### get

    public final T get()
    Returns the object set by `set(Object)`.