- Type Parameters:
T- the type of value to be lazily initialized
- All Implemented Interfaces:
Serializable,Comparable<T>,Supplier<T>
- Since:
- 1.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionintbooleanget()Gets the lazily initialized value, computing it if necessary.inthashCode()static <T> @NotNull Lazy<T><R> Lazy<R>Returns a newLazyinstance that holds a lazily evaluated result of applying the given mapping function to the value held by this instance.static <T> @NotNull Lazy<T>booleanCompares thisget()instance to another object to determine if they are the same value.booleanCompares thisLazyinstance to another instance to determine if they hold the same lazily initialized value.toString()
-
Method Details
-
of
@Contract(value="_ -> new", pure=true) @NotNull public static <T> @NotNull Lazy<T> of(@NotNull @NotNull Supplier<T> supplier) Returns a newLazyinstance that computes its value on-demand using the providedSupplier.This method is a factory method for creating new instances of the
Lazyclass. It takes a non-nullSupplierargument that will be used to compute the value when theget()method is called for the first time. The returned instance ofLazyis 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
Lazyinstance 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 newLazyinstance that computes its value on-demand using the providedSupplier.This method is a convenience wrapper for the
of(Supplier)method, which creates a newLazyinstance using the suppliedSupplier.- 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
Lazyinstance that uses the provided supplier to compute its value - Throws:
NullPointerException- if the supplied supplier is null- Since:
- 1.0.0
-
get
Gets the lazily initialized value, computing it if necessary.If the value has not yet been initialized, this method will call the
Supplierprovided in the constructor to compute and cache the value. Subsequent calls to this method will return the cached value without calling the supplier again. -
map
Returns a newLazyinstance 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
Lazyinstance.The type of the resulting
Lazyinstance 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 resultingLazyinstance- Parameters:
function- the mapping function to apply to the value held by this instance; must not be null- Returns:
- a new
Lazyinstance 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
Compares thisLazyinstance to another instance to determine if they hold the same lazily initialized value.This method returns true if the provided
Lazyinstance is not null and if its lazily initialized value is equal to the value held by this instance. The comparison is performed using theObjects.equals(Object, Object)method.- Parameters:
other- the otherLazyinstance 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
Compares thisget()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
- Specified by:
compareToin interfaceComparable<T>
-
equals
-
hashCode
public int hashCode() -
toString
-