Class LazyInitialization<T>

java.lang.Object
net.luis.utils.util.LazyInitialization<T>
Type Parameters:
T - The type of the object to initialize

public class LazyInitialization<T> extends Object
A class representing a lazy initialization of an object.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Consumer<T>
    The action to perform when the object is initialized.
    private boolean
    Whether the object has been initialized or not.
    private final org.apache.commons.lang3.mutable.MutableObject<T>
    The object to be initialized.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    Constructs a new lazy initialization with no value and no initialization action.
     
    LazyInitialization(@NotNull Consumer<T> action)
    Constructs a new lazy initialization with the given initialization action.
    private
    LazyInitialization(@NotNull org.apache.commons.lang3.mutable.MutableObject<T> mutable, @NotNull Consumer<T> action, boolean initialised)
    Constructs a new lazy initialization with the given mutable object and initialization action.
     
    Constructs an already initialized lazy initialization with the given value.
     
    LazyInitialization(T value, @NotNull Consumer<T> action)
    Constructs an already initialized lazy initialization with the given value and initialization action.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    get()
    Gets the value of the object if it has been initialized.
    int
     
    boolean
    Checks whether the object has been initialized or not.
    void
    set(T value)
    Sets the value of the object if it has not been initialized yet.
    Performs the action set in the constructor if the object is initialized.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • object

      private final org.apache.commons.lang3.mutable.MutableObject<T> object
      The object to be initialized.
    • action

      private final Consumer<T> action
      The action to perform when the object is initialized.
    • initialised

      private boolean initialised
      Whether the object has been initialized or not.
  • Constructor Details

    • LazyInitialization

      public LazyInitialization()
      Constructs a new lazy initialization with no value and no initialization action.
    • LazyInitialization

      public LazyInitialization(@Nullable T value)
      Constructs an already initialized lazy initialization with the given value.
      Parameters:
      value - The value to initialize the object with
    • LazyInitialization

      public LazyInitialization(@NotNull @NotNull Consumer<T> action)
      Constructs a new lazy initialization with the given initialization action.
      Parameters:
      action - The action to perform when the object is initialized
      Throws:
      NullPointerException - If the action is null
    • LazyInitialization

      public LazyInitialization(@Nullable T value, @NotNull @NotNull Consumer<T> action)
      Constructs an already initialized lazy initialization with the given value and initialization action.
      Parameters:
      value - The value to initialize the object with
      action - The action to perform when the object is initialized
      Throws:
      NullPointerException - If the action is null
    • LazyInitialization

      private LazyInitialization(@NotNull @NotNull org.apache.commons.lang3.mutable.MutableObject<T> mutable, @NotNull @NotNull Consumer<T> action, boolean initialised)
      Constructs a new lazy initialization with the given mutable object and initialization action.
      Parameters:
      mutable - The internal mutable object to hold the value
      action - The action to perform when the object is initialized
      initialised - Whether the object has been initialized or not
      Throws:
      NullPointerException - If the mutable or action is null
      See Also:
  • Method Details

    • get

      @Nullable public T get()
      Gets the value of the object if it has been initialized.
      Returns:
      The value of the object
      Throws:
      NotInitializedException - If the object has not been initialized yet
    • set

      public void set(@Nullable T value)
      Sets the value of the object if it has not been initialized yet.
      Performs the action set in the constructor if the object is initialized.
      Parameters:
      value - The value to initialize the object with
      Throws:
      AlreadyInitializedException - If the object has already been initialized
    • isInstantiated

      public boolean isInstantiated()
      Checks whether the object has been initialized or not.
      Returns:
      True if the object has been initialized, false otherwise
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object