Interface Memoize<S>

Type Parameters:
S - Type of the value returned.
All Superinterfaces:
Supplier<S>
All Known Subinterfaces:
CloseableMemoize<S>
All Known Implementing Classes:
CloseableMemoizingSupplier, MemoizingSupplier, ReferenceMemoizingSupplier, RefreshingMemoizingSupplier

public interface Memoize<S> extends Supplier<S>
Memoizing supplier.

This type extends Supplier and adds a peek() method as well as some monadic methods.

  • Method Summary

    Modifier and Type
    Method
    Description
    default Memoize<S>
    accept(Consumer<? super S> consumer)
    Call the consumer with the value of this memoizing supplier.
    default Memoize<S>
    filter(Predicate<? super S> predicate)
    Filter this memoizing supplier to a new memoizing supplier.
    default <R> Memoize<R>
    flatMap(Function<? super S,? extends Supplier<? extends R>> mapper)
    Flat map this memoizing supplier to a new memoizing supplier.
    get()
    Get the memoized value.
    default Memoize<S>
    ifPresent(Consumer<? super S> consumer)
    If a value is memoized, call the consumer with the value of this memoizing supplier.
    boolean
    If a value is memoized, return true.
    default <R> Memoize<R>
    map(Function<? super S,? extends R> mapper)
    Map this memoizing supplier to a new memoizing supplier.
    Peek the memoized value, if any.
    static <T> Memoize<T>
    predicateSupplier(Supplier<? extends T> supplier, Predicate<? super T> predicate)
    Creates a supplier which memoizes the first value returned by the specified supplier which is accepted by the specified predicate.
    static <T> Memoize<T>
    referenceSupplier(Supplier<? extends T> supplier, Function<? super T,? extends Reference<? extends T>> reference)
    Creates a supplier which memoizes a reference object holding the value returned by the specified supplier.
    static <T> Memoize<T>
    refreshingSupplier(Supplier<? extends T> supplier, long time_to_live, TimeUnit unit)
    Creates a supplier which memoizes, for the specified time-to-live, the value returned by the specified supplier.
    static <T, R> Memoize<R>
    supplier(Function<? super T,? extends R> function, T argument)
    Creates a supplier which memoizes the value returned by the specified function applied to the specified argument.
    static <T> Memoize<T>
    supplier(Supplier<? extends T> supplier)
    Creates a supplier which memoizes the value returned by the specified supplier.
  • Method Details

    • supplier

      static <T> Memoize<T> supplier(Supplier<? extends T> supplier)
      Creates a supplier which memoizes the value returned by the specified supplier.

      When the returned supplier is called to get a value, it will call the specified supplier at most once to obtain a value.

      Type Parameters:
      T - Type of the value returned by the supplier.
      Parameters:
      supplier - The source supplier. Must not be null.
      Returns:
      A memoizing supplier wrapping the specified supplier.
    • supplier

      static <T, R> Memoize<R> supplier(Function<? super T,? extends R> function, T argument)
      Creates a supplier which memoizes the value returned by the specified function applied to the specified argument.

      When the returned supplier is called to get a value, it will call the specified function applied to the specified argument at most once to obtain a value.

      Type Parameters:
      T - Type of the value returned by the supplier.
      Parameters:
      function - The source function. Must not be null.
      argument - The argument to the source function.
      Returns:
      A memoizing supplier wrapping the specified function and argument.
    • refreshingSupplier

      static <T> Memoize<T> refreshingSupplier(Supplier<? extends T> supplier, long time_to_live, TimeUnit unit)
      Creates a supplier which memoizes, for the specified time-to-live, the value returned by the specified supplier.

      When the returned supplier is called to get a value, it will call the specified supplier to obtain a new value if any prior obtained value is older than the specified time-to-live.

      Type Parameters:
      T - Type of the value returned by the supplier.
      Parameters:
      supplier - The source supplier. Must not be null.
      time_to_live - The time-to-live for a value. Negative values are treated as zero.
      unit - The time unit of the time-to-live value. Must not be null.
      Returns:
      A memoizing supplier wrapping the specified supplier.
    • referenceSupplier

      static <T> Memoize<T> referenceSupplier(Supplier<? extends T> supplier, Function<? super T,? extends Reference<? extends T>> reference)
      Creates a supplier which memoizes a reference object holding the value returned by the specified supplier.

      When the returned supplier is called to get a value, if the reference object holding any previously obtained value is cleared, then the specified supplier is called to obtain a new value and the specified reference function is called to wrap the new value in a reference object.

      Type Parameters:
      T - Type of the value returned by the supplier.
      Parameters:
      supplier - The source supplier. Must not be null. The supplier should not return a null value since a cleared reference also returns null.
      reference - A function which is called to wrap an object created by the specified supplier in a reference object. This allows the caller to control the reference type and whether a reference queue is used. The function must not return null.
      Returns:
      A memoizing supplier wrapping the specified supplier.
    • predicateSupplier

      static <T> Memoize<T> predicateSupplier(Supplier<? extends T> supplier, Predicate<? super T> predicate)
      Creates a supplier which memoizes the first value returned by the specified supplier which is accepted by the specified predicate.

      When the returned supplier is called to get a value, it will call the specified supplier to obtain a value and then call the specified predicate to ask if the value is acceptable. If the value is not accepted by the predicate, the value is not memoized before it is returned. If the value is accepted by the predicate, the value is memoized before it is returned and the specified supplier and specified predicate will no longer be called.

      Type Parameters:
      T - Type of the value returned by the supplier.
      Parameters:
      supplier - The source supplier. Must not be null.
      predicate - The accepting predicate. Must not be null.
      Returns:
      A memoizing supplier wrapping the specified supplier and specified predicate.
      Since:
      1.1
    • get

      S get()
      Get the memoized value.
      Specified by:
      get in interface Supplier<S>
      Returns:
      The memoized value.
    • peek

      S peek()
      Peek the memoized value, if any.

      This method will not result in a call to the source supplier.

      Returns:
      The memoized value if a value is memoized; otherwise null.
    • isPresent

      boolean isPresent()
      If a value is memoized, return true. Otherwise return false.

      This method will not result in a call to the source supplier.

      Returns:
      true if a value is memoized; otherwise false.
    • map

      default <R> Memoize<R> map(Function<? super S,? extends R> mapper)
      Map this memoizing supplier to a new memoizing supplier.
      Type Parameters:
      R - Type of the value returned by the new memoizing supplier.
      Parameters:
      mapper - The function to map the value of this memoizing supplier. Must not be null.
      Returns:
      A new memoizing supplier which memoizes the value returned by the specified function.
    • flatMap

      default <R> Memoize<R> flatMap(Function<? super S,? extends Supplier<? extends R>> mapper)
      Flat map this memoizing supplier to a new memoizing supplier.
      Type Parameters:
      R - Type of the value returned by the new memoizing supplier.
      Parameters:
      mapper - The function to flat map the value of this memoizing supplier to a supplier. Must not be null. The returned supplier must not be null.
      Returns:
      A new memoizing supplier which memoizes the value returned by the supplier returned by the specified function.
    • filter

      default Memoize<S> filter(Predicate<? super S> predicate)
      Filter this memoizing supplier to a new memoizing supplier.
      Parameters:
      predicate - The predicate to test the value of this memoizing supplier. Must not be null.
      Returns:
      A new memoizing supplier which memoizes the value returned by this supplier if the predicate accepts the value or null otherwise.
    • accept

      default Memoize<S> accept(Consumer<? super S> consumer)
      Call the consumer with the value of this memoizing supplier.
      Parameters:
      consumer - The consumer to accept the value of this memoizing supplier. Must not be null.
      Returns:
      This memoizing supplier.
    • ifPresent

      default Memoize<S> ifPresent(Consumer<? super S> consumer)
      If a value is memoized, call the consumer with the value of this memoizing supplier. Otherwise do nothing.
      Parameters:
      consumer - The consumer to accept the value of this memoizing supplier if a value is memoized. Must not be null if a value is memoized.
      Returns:
      This memoizing supplier.