Class Functions


  • @Internal
    @ReturnValuesAreNonnullByDefault
    public abstract class Functions
    extends Object
    • Method Detail

      • voidFn

        public static <T> Function<T,​Void> voidFn​(Consumer<T> effect)
        Convenience wrapper to turn a Consumer<T> into a Function<T, Void>. It is common to need to do this when working with lambdas, and unfortunately Java doesn't know how to work this out for itself.
        Type Parameters:
        T - the inferred input parameter type for the original Consumer
        Parameters:
        effect - the consumer to be reimagined as a Void-returning function
        Returns:
        the resulting function that returns Void
      • mapToList

        public static <T,​U> List<U> mapToList​(T[] source,
                                                    Function<? super T,​? extends U> mapper)
        Given an array, map each element from the array to a new value and return the resulting values as a list.
        Type Parameters:
        T - the inferred input type
        U - the inferred output type
        Parameters:
        source - the array of input values
        mapper - the mapping function to apply to each input value; must not return null
        Returns:
        an (unmodifiable) list of the resulting values
      • mapToList

        public static <T,​U> List<U> mapToList​(Iterable<T> source,
                                                    Function<? super T,​? extends U> mapper)
        Given an iterable collection, map each element from the collection to a new value and return the resulting values as a list.
        Type Parameters:
        T - the inferred input type
        U - the inferred output type
        Parameters:
        source - the collection of input values
        mapper - the mapping function to apply to each input value; must not return null
        Returns:
        an (unmodifiable) list of the resulting values
      • mapToList

        public static <T,​U> List<U> mapToList​(Stream<T> source,
                                                    Function<? super T,​? extends U> mapper)
        Given a stream, map each element from it to a new value and return the resulting values as a list.
        Type Parameters:
        T - the inferred input type
        U - the inferred output type
        Parameters:
        source - the stream of input values
        mapper - the mapping function to apply to each input value; must not return null
        Returns:
        an (unmodifiable) list of the resulting values
      • iterateJoined

        public static <T> boolean iterateJoined​(Iterable<T> source,
                                                Consumer<? super T> effect,
                                                Runnable joiner)
        Iterates the source collection, calling effect for each element with calls to the joiner between them. If the source collection is empty, there is no effect.

        For example, if source is List.of("foo", "bar", "baz"), then the sequence of calls will be:

        
             effect.accept("foo");
             joiner.run();
             effect.accept("bar");
             joiner.run();
             effect.accept("baz");
         
        Type Parameters:
        T - the inferred element type supplied by source
        Parameters:
        source - supplies the elements to iterate over
        effect - what to do with each element from the collection
        joiner - what to do between any two elements from the collection
        Returns:
        true if the source collection supplied at least one element; false if it was empty