Class LazyValue<T>

java.lang.Object
nl.pim16aap2.util.LazyValue<T>
Type Parameters:
T - The type of the object to initialize lazily.

@ThreadSafe public final class LazyValue<T> extends Object
Represents a lazily initialized object.

Initialization is thread-safe.

  • Constructor Details

    • LazyValue

      public LazyValue(Supplier<T> supplier)
      Parameters:
      supplier - The supplier to use to create the instance of the object when needed.

      Note that the return value may not be null!

  • Method Details

    • get

      public T get()
      Gets the lazily initialized object. If it does not exist yet, a new instance will be created using supplier.
      Returns:
      The lazily initialized object.
    • reset

      @Nullable public T reset()
      Resets the lazily initialized value (if it has been initialized yet).
      Returns:
      The previously-initialized value or null if it was not initialized yet.
    • equals

      public boolean equals(Object o)
      Note that calling this method will result in get() being called for both objects if the other object is also a LazyValue.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Note that calling this method will result in get() being called.
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object