Package org.organicdesign.fp.function
Class LazyRef<T>
- java.lang.Object
-
- org.organicdesign.fp.function.LazyRef<T>
-
public class LazyRef<T> extends Object implements Fn0<T>
Lazily initialize a value (and free the initialization resources) on the first call to get(). Subsequent calls to get() cheaply return the previously initialized value. This class is thread-safe if the producer function and the value it produces are free from side effects.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description T
applyEx()
The first call to this method calls the initialization function, caches the result, and hands the initialization function reference to the garbage collector so that initialization resources can be freed.static <T> @NotNull LazyRef<T>
of(@NotNull Fn0<T> producer)
Construct a LazyRef from the given initialization function.@NotNull String
toString()
Useful for debugging, but not referentially transparent (sometimes returns LazyRef(*not-computed-yet*), sometimes shows the value that was computed).
-
-
-
Method Detail
-
of
@NotNull public static <T> @NotNull LazyRef<T> of(@NotNull @NotNull Fn0<T> producer)
Construct a LazyRef from the given initialization function.- Parameters:
producer
- a zero-argument function that produces the desired value when called.- Returns:
- a LazyRef with the given producer.
-
applyEx
public T applyEx()
The first call to this method calls the initialization function, caches the result, and hands the initialization function reference to the garbage collector so that initialization resources can be freed. Subsequent calls return the precomputed value.
-
toString
@NotNull public @NotNull String toString()
Useful for debugging, but not referentially transparent (sometimes returns LazyRef(*not-computed-yet*), sometimes shows the value that was computed).
-
-