public class Functions
extends java.lang.Object
Constructor and Description |
---|
Functions() |
Modifier and Type | Method and Description |
---|---|
static <T> Predicate<T> |
alwaysFalse()
A Predicate that is always false
|
static <T> Predicate<T> |
alwaysTrue()
A Predicate that is always true
|
static void |
closeFunction(java.lang.Object function)
Close the function.
|
static <T> java.lang.Runnable |
delayedConsume(Consumer<T> consumer,
T value)
Create a
Runnable that calls
consumer.accept(value) when run() is called. |
static <T> Consumer<T> |
discard()
A Consumer that discards all items passed to it.
|
static <T> UnaryOperator<T> |
identity()
Returns the identity function that returns its single argument.
|
static boolean |
isImmutable(java.lang.Object function)
See if the functional logic is immutable.
|
static boolean |
isImmutableClass(java.lang.Class<?> clazz)
See if a function class is immutable.
|
static java.lang.Runnable |
runWithFinal(java.lang.Runnable action,
java.lang.Runnable finalAction)
Wrap a
Runnable with a final action that
is always called when action.run() completes. |
static <T,U,R> BiFunction<T,U,R> |
synchronizedBiFunction(BiFunction<T,U,R> function)
Return a thread-safe version of a
BiFunction function. |
static <T> Consumer<T> |
synchronizedConsumer(Consumer<T> function)
Return a thread-safe version of a
Consumer function. |
static <T,R> Function<T,R> |
synchronizedFunction(Function<T,R> function)
Return a thread-safe version of a
Function function. |
static <T> Supplier<T> |
synchronizedSupplier(Supplier<T> function)
Return a thread-safe version of a
Supplier function. |
static <T> Function<T,java.lang.Integer> |
unpartitioned()
Returns a constant function that returns zero (0).
|
static <T> Function<T,java.lang.Integer> |
zero()
Returns a constant function that returns zero (0).
|
public static <T> UnaryOperator<T> identity()
T
- tuple typepublic static <T> Function<T,java.lang.Integer> zero()
T
- tuple typepublic static <T> Function<T,java.lang.Integer> unpartitioned()
zero()
but is more
readable when applied as a key function.T
- tuple typepublic static void closeFunction(java.lang.Object function) throws java.lang.Exception
function
is an instance of AutoCloseable
then close is called, otherwise no action is taken.function
- Function to be closed.java.lang.Exception
- Error throwing function.public static <T,R> Function<T,R> synchronizedFunction(Function<T,R> function)
Function
function.
If the function is guaranteed to be immutable (stateless)
then the function is returned, as it is thread safe,
otherwise a wrapper is returned that grabs synchronization
on function
when calling Function.apply(Object)
.
function
implements AutoCloseable
then
the function is assumed to be stateful and a thread-safe
version is returned.T
- source tuple typeR
- result tuple typefunction
- Function to return a thread-safe version of.public static <T> Supplier<T> synchronizedSupplier(Supplier<T> function)
Supplier
function.
If the function is guaranteed to be immutable (stateless)
then the function is returned, as it is thread safe,
otherwise a wrapper is returned that grabs synchronization
on function
when calling Supplier.get()
.
function
implements AutoCloseable
then
the function is assumed to be stateful and a thread-safe
version is returned.T
- tuple typefunction
- Function to return a thread-safe version of.public static <T> Consumer<T> synchronizedConsumer(Consumer<T> function)
Consumer
function.
If the function is guaranteed to be immutable (stateless)
then the function is returned, as it is thread safe,
otherwise a wrapper is returned that grabs synchronization
on function
when calling Consumer.accept(Object)
.
function
implements AutoCloseable
then
the function is assumed to be stateful and a thread-safe
version is returned.T
- tuple typefunction
- Function to return a thread-safe version of.public static <T,U,R> BiFunction<T,U,R> synchronizedBiFunction(BiFunction<T,U,R> function)
BiFunction
function.
If the function is guaranteed to be immutable (stateless)
then the function is returned, as it is thread safe,
otherwise a wrapper is returned that grabs synchronization
on function
when calling BiFunction.apply(Object, Object)
.
function
implements AutoCloseable
then
the function is assumed to be stateful and a thread-safe
version is returned.T
- Type of function's first argumentU
- Type of function's second argumentR
- Type of function's third argumentfunction
- Function to return a thread-safe version of.public static boolean isImmutable(java.lang.Object function)
function
- Function to checkpublic static boolean isImmutableClass(java.lang.Class<?> clazz)
clazz
- Class to checkpublic static <T> java.lang.Runnable delayedConsume(Consumer<T> consumer, T value)
Runnable
that calls
consumer.accept(value)
when run()
is called.
This can be used to delay the execution of the consumer
until some time in the future using an executor service.T
- tuple typeconsumer
- Function to be applied to value
.value
- Value to be consumed.Runnable
that invokes consumer.accept(value)
.public static java.lang.Runnable runWithFinal(java.lang.Runnable action, java.lang.Runnable finalAction)
Runnable
with a final action that
is always called when action.run()
completes.action
- Action to be invoked before finalAction
.finalAction
- Action to be invoked after action.run()
is called.Runnable
that invokes action.run()
and then finalAction.run()
public static <T> Consumer<T> discard()
T
- tuple typepublic static <T> Predicate<T> alwaysTrue()
T
- tuple typepublic static <T> Predicate<T> alwaysFalse()
T
- tuple typeCopyright © 2016 The Apache Software Foundation. All Rights Reserved - bbe71fa-20161201-1641