Class Functions

java.lang.Object
com.globalmentor.io.function.Functions

public class Functions extends Object
Utility higher-order functions.
Author:
Garret Wilson
  • Constructor Details

    • Functions

      public Functions()
  • Method Details

    • countingConsumer

      public static <T> Consumer<T> countingConsumer(@Nonnull BiConsumer<T,Long> biConsumer)
      Creates a consumer that counts the number of times it has been invoked and passes that count to the provided consumer. Use the function to replace this:
      
       fooStream.forEach(foo -> doSomething());
       
      with this:
      
       fooStream.forEach(countingConsumer((foo, count) -> doSomething());
       
      API Note:
      The returned consumer uses one-based "counts" and not zero-based "indexes" because an index implies some sort of order, and also makes it awkward to keep track of an initial value when the consumer may never have been invoked. A "count" starts off with zero invocations, and in a parallel context the concept of an "index" would not necessarily be accurate, as consumers may be called in an arbitrary order.
      Implementation Specification:
      This implementation delegates to countingConsumer(AtomicLong, BiConsumer) with a new counter initially set to zero.
      Implementation Note:
      The returned consumer is thread-safe; each invocation will provide a unique count, although with concurrent invocations the counts will not necessarily be in order.
      Type Parameters:
      T - The type of input argument.
      Parameters:
      biConsumer - The consumer accepting the input argument along with the count.
      Returns:
      A consumer that keeps track of the count of invocations and passes that to the enclosed consumer.
    • countingConsumer

      public static <T> Consumer<T> countingConsumer(@Nonnull AtomicLong counter, @Nonnull BiConsumer<T,Long> biConsumer)
      Creates a consumer that counts the number of times it has been invoked and passes that count to the provided consumer.
      API Note:
      This version allows an existing counter to be used, e.g. to retrieve the count after or during stream traversal; or to continue counting in other contexts (such as nested loops). See countingConsumer(BiConsumer) for usage example., The returned consumer uses one-based "counts" and not zero-based "indexes" because an index implies some sort of order, and also makes it awkward to keep track of an initial value when the consumer may never have been invoked. A "count" starts off with zero invocations, and in a parallel context the concept of an "index" would not necessarily be accurate, as consumers may be called in an arbitrary order.
      Implementation Note:
      The returned consumer is thread-safe; each invocation will provide a unique count, although with concurrent invocations the counts will not necessarily be in order.
      Type Parameters:
      T - The type of input argument.
      Parameters:
      counter - An existing counter to use.
      biConsumer - The consumer accepting the input argument along with the count.
      Returns:
      A consumer that keeps track of the count of invocations and passes that to the enclosed consumer.
    • toBiFunctionT

      public static <T, U, R> BiFunction<T,U,R> toBiFunctionT(@Nonnull Function<? super T,? extends R> function)
      Converts a function to a bifunction by passing the first bifunction argument t to the function and ignoring the second.
      Type Parameters:
      T - The type of the first argument to the bifunction.
      U - The type of the second argument to the bifunction.
      R - The type of the result of the function.
      Parameters:
      function - The function to convert to a bifunction.
      Returns:
      A bifunction that delegates to the given function, passing its first argument.
    • toBiFunctionU

      public static <T, U, R> BiFunction<T,U,R> toBiFunctionU(@Nonnull Function<? super U,? extends R> function)
      Converts a function to a bifunction by passing the second bifunction argument u to the function and ignoring the first.
      Type Parameters:
      T - The type of the first argument to the bifunction.
      U - The type of the second argument to the bifunction.
      R - The type of the result of the function.
      Parameters:
      function - The function to convert to a bifunction.
      Returns:
      A bifunction that delegates to the given function, passing its second argument.