Module toolbox.lazy

Class Lazy<T>

java.lang.Object
org.spokbjorn.lazy.Lazy<T>
Type Parameters:
T - the type of value to be lazily initialized
All Implemented Interfaces:
Serializable, Comparable<T>, Supplier<T>

public class Lazy<T> extends Object implements Supplier<T>, Comparable<T>, Serializable
A generic class for creating a lazy-initialized value that can be computed on-demand.
Since:
1.0.0
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    boolean
     
    get()
    Gets the lazily initialized value, computing it if necessary.
    int
     
    static <T> @NotNull Lazy<T>
    lazy(@NotNull Supplier<T> supplier)
    Returns a new Lazy instance that computes its value on-demand using the provided Supplier.
    <R> Lazy<R>
    map(Function<? super T,? extends R> function)
    Returns a new Lazy instance that holds a lazily evaluated result of applying the given mapping function to the value held by this instance.
    static <T> @NotNull Lazy<T>
    of(@NotNull Supplier<T> supplier)
    Returns a new Lazy instance that computes its value on-demand using the provided Supplier.
    boolean
    same(@Nullable Object other)
    Compares this get() instance to another object to determine if they are the same value.
    boolean
    same(@Nullable Lazy<T> other)
    Compares this Lazy instance to another instance to determine if they hold the same lazily initialized value.
     

    Methods inherited from class java.lang.Object

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

    • of

      @Contract(value="_ -> new", pure=true) @NotNull public static <T> @NotNull Lazy<T> of(@NotNull @NotNull Supplier<T> supplier)
      Returns a new Lazy instance that computes its value on-demand using the provided Supplier.

      This method is a factory method for creating new instances of the Lazy class. It takes a non-null Supplier argument that will be used to compute the value when the get() method is called for the first time. The returned instance of Lazy is guaranteed to be non-null.

      Type Parameters:
      T - the type of value to be lazily initialized
      Parameters:
      supplier - the supplier used to compute the value on-demand; must not be null
      Returns:
      a new Lazy instance that uses the provided supplier to compute its value
      Throws:
      NullPointerException - if the supplied supplier is null
      Since:
      1.0.0
    • lazy

      @Contract("_ -> new") @NotNull public static <T> @NotNull Lazy<T> lazy(@NotNull @NotNull Supplier<T> supplier)
      Returns a new Lazy instance that computes its value on-demand using the provided Supplier.

      This method is a convenience wrapper for the of(Supplier) method, which creates a new Lazy instance using the supplied Supplier.

      Type Parameters:
      T - the type of value to be lazily initialized
      Parameters:
      supplier - the supplier used to compute the value on-demand; must not be null
      Returns:
      a new Lazy instance that uses the provided supplier to compute its value
      Throws:
      NullPointerException - if the supplied supplier is null
      Since:
      1.0.0
    • get

      public T get()
      Gets the lazily initialized value, computing it if necessary.

      If the value has not yet been initialized, this method will call the Supplier provided in the constructor to compute and cache the value. Subsequent calls to this method will return the cached value without calling the supplier again.

      Specified by:
      get in interface Supplier<T>
      Returns:
      the lazily initialized value
      Since:
      1.0.0
    • map

      public <R> Lazy<R> map(Function<? super T,? extends R> function)
      Returns a new Lazy instance that holds a lazily evaluated result of applying the given mapping function to the value held by this instance.

      The mapping function is applied lazily when the value is first accessed through the new instance. If the mapping function is expensive to compute, this can provide significant performance benefits compared to computing the mapping eagerly and then creating a new Lazy instance.

      The type of the resulting Lazy instance is determined by the return type of the mapping function. The input type of the mapping function must be a super type of the type held by this instance.

      Type Parameters:
      R - the type of the resulting Lazy instance
      Parameters:
      function - the mapping function to apply to the value held by this instance; must not be null
      Returns:
      a new Lazy instance that holds the lazily evaluated result of applying the given mapping function to the value held by this instance
      Throws:
      NullPointerException - if the given mapping function is null
      Since:
      1.0.0
    • same

      public boolean same(@Nullable @Nullable Lazy<T> other)
      Compares this Lazy instance to another instance to determine if they hold the same lazily initialized value.

      This method returns true if the provided Lazy instance is not null and if its lazily initialized value is equal to the value held by this instance. The comparison is performed using the Objects.equals(Object, Object) method.

      Parameters:
      other - the other Lazy instance to compare to; may be null
      Returns:
      true if the two instances hold the same lazily initialized value, false otherwise
      Since:
      1.0.0
    • same

      public boolean same(@Nullable @Nullable Object other)
      Compares this get() instance to another object to determine if they are the same value.

      This method returns true if the provided object is not null and if it is an instance of the value held by this instance. The comparison is performed using the Objects.equals(Object, Object) method.

      Parameters:
      other - the object to compare to; may be null
      Returns:
      true if the object holds the same value, false otherwise
      Since:
      1.0.0
    • compareTo

      public int compareTo(@NotNull T o)
      Specified by:
      compareTo in interface Comparable<T>
    • 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