Class Memoized<T>

  • Type Parameters:
    T - is the type of the value
    All Implemented Interfaces:
    jakarta.inject.Provider<T>, java.util.function.Supplier<T>

    public final class Memoized<T>
    extends java.lang.Object
    implements jakarta.inject.Provider<T>, java.util.function.Supplier<T>
    Provides simple implementation of the lazily computed value which gets evaluated once.

    Can be used as both Provider and Supplier due to their contract equality making this class useful for multiple scenarios.

    NOTE: due to the lazy nature of this value class it does not provide Object.equals(Object) and Object.hashCode() implementations and MUST NOT be used in Set or as a key in Map.

    See Also:
    Memoization on Wikipedia
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T get()
      Double-checked locking with Lock instead of synchronized to facilitate its usage on newer JVM versions affected by thread pinning problem.
      void ifPresent​(java.util.function.Consumer<T> action)
      If a value is evaluated, performs the given action with the value, otherwise does nothing.
      void ifPresentOrElse​(java.util.function.Consumer<? super T> action, java.lang.Runnable emptyAction)
      If a value is evaluated, performs the given action with the value, otherwise performs the given empty-based action.
      boolean isEmpty()
      If a value is not evaluated, returns true, otherwise false.
      boolean isPresent()
      If a value is evaluated, returns true, otherwise false.
      static <T> Memoized<T> memoizedProvider​(jakarta.inject.Provider<T> provider)
      Static factory method for Memoized class.
      static <T> Memoized<T> memoizedSupplier​(java.util.function.Supplier<T> supplier)
      Static factory method for Memoized class.
      java.util.stream.Stream<T> stream()
      If a value is evaluated, returns a sequential Stream containing only that value, otherwise returns an empty Stream.
      java.lang.String toString()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • memoizedProvider

        public static <T> Memoized<T> memoizedProvider​(jakarta.inject.Provider<T> provider)
        Static factory method for Memoized class.
        Type Parameters:
        T - is the type of the value
        Parameters:
        provider - to invoke to compute the value
        Returns:
        new Memoized instance
      • memoizedSupplier

        public static <T> Memoized<T> memoizedSupplier​(java.util.function.Supplier<T> supplier)
        Static factory method for Memoized class.
        Type Parameters:
        T - is the type of the value
        Parameters:
        supplier - to invoke to compute the value
        Returns:
        new Memoized instance
        Throws:
        java.lang.IllegalArgumentException - if Supplier is null
      • get

        public T get()
        Double-checked locking with Lock instead of synchronized to facilitate its usage on newer JVM versions affected by thread pinning problem.

        Despite AtomicReference, we use explicit locking to solve the problem of avoiding excessive Provider calls which would have occurred if we would use AtomicReference.compareAndSet(Object, Object).

        Specified by:
        get in interface jakarta.inject.Provider<T>
        Specified by:
        get in interface java.util.function.Supplier<T>
        Returns:
        computed value
        Throws:
        java.lang.IllegalArgumentException - if Provider returns null
        See Also:
        JEP to fix thread pinning problem
      • isEmpty

        public boolean isEmpty()
        If a value is not evaluated, returns true, otherwise false.
        Returns:
        true if a value is not evaluated, otherwise false
      • isPresent

        public boolean isPresent()
        If a value is evaluated, returns true, otherwise false.
        Returns:
        true if a value is evaluated, otherwise false
      • ifPresent

        public void ifPresent​(java.util.function.Consumer<T> action)
        If a value is evaluated, performs the given action with the value, otherwise does nothing.
        Parameters:
        action - the action to be performed if a value is evaluated
        Throws:
        java.lang.NullPointerException - if value is evaluated and the given action is null
      • ifPresentOrElse

        public void ifPresentOrElse​(java.util.function.Consumer<? super T> action,
                                    java.lang.Runnable emptyAction)
        If a value is evaluated, performs the given action with the value, otherwise performs the given empty-based action.
        Parameters:
        action - the action to be performed if a value is evaluated
        emptyAction - the empty-based action to be performed if no value is evaluated
        Throws:
        java.lang.NullPointerException - if a value is evaluated and the given action is null, or no value is evaluated and the given empty-based action is null.
      • stream

        public java.util.stream.Stream<T> stream()
        If a value is evaluated, returns a sequential Stream containing only that value, otherwise returns an empty Stream.
        Returns:
        the memoized value as a Stream
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object