Class LazyRef<T>

  • All Implemented Interfaces:
    Callable<T>, Supplier<T>, Fn0<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.
      String toString()
      Useful for debugging, but not referentially transparent (sometimes returns LazyRef(*not-computed-yet*), sometimes shows the value that was computed).
      • Methods inherited from interface org.organicdesign.fp.function.Fn0

        apply, call, get
    • 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.
        Specified by:
        applyEx in interface Fn0<T>
        Returns:
        the same value every time it is called.
      • toString

        public 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 class Object
        Returns:
        a string describing this LazyRef and showing whether or not its value has been computed yet.