Class Suppliers


  • public class Suppliers
    extends java.lang.Object
    Provide utility functions for the class of functions that supply a return value when invoked.
    Since:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      Suppliers()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.function.Supplier<java.lang.Boolean> alwaysFalse()
      Supplies false.
      static <A> java.util.function.Supplier<A> alwaysNull()
      Always returns null.
      static java.util.function.Supplier<java.lang.Boolean> alwaysTrue()
      Supplies true.
      static <A,​B>
      java.util.function.Supplier<B>
      ap​(java.util.function.Supplier<A> sa, java.util.function.Supplier<java.util.function.Function<A,​B>> sf)
      Performs function application within a supplier (applicative functor pattern).
      static <A,​B>
      java.util.function.Supplier<B>
      compose​(java.util.function.Function<? super A,​B> transform, java.util.function.Supplier<A> first)
      Create a new Supplier by transforming the result calling the first Supplier
      static <A,​B>
      java.util.function.Supplier<B>
      fromFunction​(java.util.function.Function<? super A,​? extends B> f, A a)
      Constantly applies the input value to the supplied function, and returns the result.
      static <A> java.util.function.Supplier<A> fromOption​(Option<A> option)
      Turns an Option into a supplier, but throws an exception if undefined.
      static <A> java.util.function.Supplier<A> memoize​(java.util.function.Supplier<A> supplier)
      A supplier that memoize the value return by another Supplier, whose Supplier.get() method is guaranteed to be call at most once.
      static <A> java.util.function.Supplier<A> ofInstance​(A a)
      Creates a Supplier of a constant value.
      static <A> java.util.function.Supplier<A> weakMemoize​(java.util.function.Supplier<A> supplier)
      A supplier that weakly memoize the value return by another Supplier , The returned Supplier is thread-safe
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Suppliers

        public Suppliers()
    • Method Detail

      • ofInstance

        public static <A> java.util.function.Supplier<A> ofInstance​(A a)
        Creates a Supplier of a constant value.
        Type Parameters:
        A - the type
        Parameters:
        a - the constant value to supply
        Returns:
        a supplier that always supplies instance.
      • compose

        public static <A,​B> java.util.function.Supplier<B> compose​(java.util.function.Function<? super A,​B> transform,
                                                                         java.util.function.Supplier<A> first)
        Create a new Supplier by transforming the result calling the first Supplier
        Type Parameters:
        A - return type of the Supplier to transform
        B - return type of the new Supplier
        Parameters:
        transform - function to transform the result of a Supplier of A's to B's
        first - a Supplier of A's
        Returns:
        a new Supplier returning B's
      • ap

        public static <A,​B> java.util.function.Supplier<B> ap​(java.util.function.Supplier<A> sa,
                                                                    java.util.function.Supplier<java.util.function.Function<A,​B>> sf)
        Performs function application within a supplier (applicative functor pattern).
        Parameters:
        sa - supplier
        sf - The Supplier function to apply.
        Returns:
        A new Supplier after applying the given Supplier function to the first argument.
        Since:
        4.0
      • alwaysTrue

        public static java.util.function.Supplier<java.lang.Boolean> alwaysTrue()
        Supplies true.
        Returns:
        a supplier that always supplies true.
      • alwaysFalse

        public static java.util.function.Supplier<java.lang.Boolean> alwaysFalse()
        Supplies false.
        Returns:
        a supplier that always supplies false.
      • alwaysNull

        public static <A> java.util.function.Supplier<A> alwaysNull()
        Always returns null. Not a very good idea.
        Type Parameters:
        A - the type
        Returns:
        a supplier that always supplies null.
      • fromOption

        public static <A> java.util.function.Supplier<A> fromOption​(Option<A> option)
        Turns an Option into a supplier, but throws an exception if undefined. Not a very good idea.
        Type Parameters:
        A - the type
        Parameters:
        option - the option to attempt to get values from
        Returns:
        a Supplier that always calls Maybe.get(), which throws an Exception if the option is None
        Since:
        2.0
      • fromFunction

        public static <A,​B> java.util.function.Supplier<B> fromFunction​(java.util.function.Function<? super A,​? extends B> f,
                                                                              A a)
        Constantly applies the input value to the supplied function, and returns the result.
        Type Parameters:
        A - the input type
        B - the result type
        Parameters:
        f - the function
        a - the value
        Returns:
        a Supplier that always calls Function.apply(Object)
        Since:
        2.2
      • memoize

        public static <A> java.util.function.Supplier<A> memoize​(java.util.function.Supplier<A> supplier)
        A supplier that memoize the value return by another Supplier, whose Supplier.get() method is guaranteed to be call at most once. The returned Supplier is thread-safe
        Type Parameters:
        A - the type
        Parameters:
        supplier - the supplier to memoize
        Returns:
        the memoizing supplier
      • weakMemoize

        public static <A> java.util.function.Supplier<A> weakMemoize​(java.util.function.Supplier<A> supplier)
        A supplier that weakly memoize the value return by another Supplier , The returned Supplier is thread-safe
        Type Parameters:
        A - the type
        Parameters:
        supplier - the supplier to memoize
        Returns:
        the weakly memoizing supplier