Package org.organicdesign.fp.function
Class LazyRef<T>
- java.lang.Object
-
- org.organicdesign.fp.function.LazyRef<T>
-
- All Implemented Interfaces:
java.util.concurrent.Callable<T>
,java.util.function.Supplier<T>
,Fn0<T>
public class LazyRef<T> extends java.lang.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> LazyRef<T>
of(Fn0<T> producer)
Construct a LazyRef from the given initialization function.java.lang.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
public static <T> LazyRef<T> of(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
public java.lang.String toString()
Useful for debugging, but not referentially transparent (sometimes returns LazyRef(*not-computed-yet*), sometimes shows the value that was computed).- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string describing this LazyRef and showing whether or not its value has been computed yet.
-
-