Package com.globalmentor.io.function
Class Functions
java.lang.Object
com.globalmentor.io.function.Functions
Utility higher-order functions.
- Author:
- Garret Wilson
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Consumer<T>
countingConsumer
(AtomicLong counter, 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.static <T> Consumer<T>
countingConsumer
(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.static <T,
U, R> BiFunction<T, U, R> toBiFunctionT
(Function<? super T, ? extends R> function) Converts a function to a bifunction by passing the first bifunction argumentt
to the function and ignoring the second.static <T,
U, R> BiFunction<T, U, R> toBiFunctionU
(Function<? super U, ? extends R> function) Converts a function to a bifunction by passing the second bifunction argumentu
to the function and ignoring the first.
-
Constructor Details
-
Functions
public Functions()
-
-
Method Details
-
countingConsumer
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:
with this:fooStream.forEach(foo -> doSomething());
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, BiFunction<T,R> U, toBiFunctionTR> (@Nonnull Function<? super T, ? extends R> function) Converts a function to a bifunction by passing the first bifunction argumentt
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, BiFunction<T,R> U, toBiFunctionUR> (@Nonnull Function<? super U, ? extends R> function) Converts a function to a bifunction by passing the second bifunction argumentu
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.
-