Top Description Constructors Methods
org.apache.commons.lang3.function

public final Class Functions

extends Object
Class Inheritance
Imports
java.util.Objects, .Optional, java.util.function.Function

Factory for Function.
Since
3.14.0

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <
the type of the argument the function applies.
T
,
the type of the result the function returns.
R
>
R

Returns:

the value the function returns if the function is not null; null otherwise.
apply
(final Function<T, R>
the function to apply.
function
,
final T
the object to apply the function.
object
)

Applies the Function on the object if the function is not null.

public static <
The type of the input of this method and the function.
T
,
The type of the result of the function and this method.
R
>
R

Returns:

The result of the function (which may be null) or null if the input value is null.
applyNonNull
(final T
The value to apply the function to, may be null.
value
,
final Function<? super T, ? extends R>
The function to apply, must not be null.
mapper
)

Applies a value to a function if the value isn't null, otherwise the method returns null.

public static <
The type of the input of this method and the first function.
T
,
The type of the result of the first function and the input to the second function.
U
,
The type of the result of the second function and this method.
R
>
R

Returns:

The result of the final function (which may be null) or null if the input value or any intermediate value is null.
applyNonNull
(final T
The value to apply the functions to, may be null.
value1
,
final Function<? super T, ? extends U>
The first function to apply, must not be null.
mapper1
,
final Function<? super U, ? extends R>
The second function to apply, must not be null.
mapper2
)

Applies values to a chain of functions, where a null can short-circuit each step.

public static <
The type of the input of this method and the first function.
T
,
The type of the result of the first function and the input to the second function.
U
,
The type of the result of the second function and the input to the third function.
V
,
The type of the result of the third function and this method.
R
>
R

Returns:

The result of the final function (which may be null) or null if the input value or any intermediate value is null.
applyNonNull
(final T
The value to apply the first function, may be null.
value1
,
final Function<? super T, ? extends U>
The first function to apply, must not be null.
mapper1
,
final Function<? super U, ? extends V>
The second function to apply, must not be null.
mapper2
,
final Function<? super V, ? extends R>
The third function to apply, must not be null.
mapper3
)

Applies values to a chain of functions, where a null can short-circuit each step.

public static <
Input type.
T
,
Return type.
R
>
Function<T, R>

Returns:

the argument
function
(final Function<T, R>
the argument to return.
function
)

Starts a fluent chain like function(foo::bar).andThen(...).andThen(...).apply(...);

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Functionsback to summary
private Functions()

Method Detail

applyback to summary
public static <T, R> R apply(final Function<T, R> function, final T object)

Applies the Function on the object if the function is not null. Otherwise, does nothing and returns null.

Parameters
<T>
the type of the argument the function applies.
<R>
the type of the result the function returns.
function:Function<T, R>

the function to apply.

object:T

the object to apply the function.

Returns:R

the value the function returns if the function is not null; null otherwise.

Since
3.15.0
applyNonNullback to summary
public static <T, R> R applyNonNull(final T value, final Function<? super T, ? extends R> mapper)

Applies a value to a function if the value isn't null, otherwise the method returns null. If the value isn't null then return the result of the applying function.

Functions.applyNonNull("a", String::toUpperCase)  = "A"
Functions.applyNonNull(null, String::toUpperCase) = null
Functions.applyNonNull("a", s -> null)            = null

Useful when working with expressions that may return null as it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to using Optional that is shorter and has less allocation.

Parameters
<T>
The type of the input of this method and the function.
<R>
The type of the result of the function and this method.
value:T

The value to apply the function to, may be null.

mapper:Function<? super T, ? extends R>

The function to apply, must not be null.

Returns:R

The result of the function (which may be null) or null if the input value is null.

Since
3.19.0
See Also
applyNonNull(Object, Function, Function), applyNonNull(Object, Function, Function, Function)
applyNonNullback to summary
public static <T, U, R> R applyNonNull(final T value1, final Function<? super T, ? extends U> mapper1, final Function<? super U, ? extends R> mapper2)

Applies values to a chain of functions, where a null can short-circuit each step. A function is only applied if the previous value is not null, otherwise this method returns null.

Functions.applyNonNull(" a ", String::toUpperCase, String::trim) = "A"
Functions.applyNonNull(null, String::toUpperCase, String::trim)  = null
Functions.applyNonNull(" a ", s -> null, String::trim)           = null
Functions.applyNonNull(" a ", String::toUpperCase, s -> null)    = null

Useful when working with expressions that may return null as it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to using Optional that is shorter and has less allocation.

Parameters
<T>
The type of the input of this method and the first function.
<U>
The type of the result of the first function and the input to the second function.
<R>
The type of the result of the second function and this method.
value1:T

The value to apply the functions to, may be null.

mapper1:Function<? super T, ? extends U>

The first function to apply, must not be null.

mapper2:Function<? super U, ? extends R>

The second function to apply, must not be null.

Returns:R

The result of the final function (which may be null) or null if the input value or any intermediate value is null.

Since
3.19.0
See Also
applyNonNull(Object, Function), applyNonNull(Object, Function, Function, Function)
applyNonNullback to summary
public static <T, U, V, R> R applyNonNull(final T value1, final Function<? super T, ? extends U> mapper1, final Function<? super U, ? extends V> mapper2, final Function<? super V, ? extends R> mapper3)

Applies values to a chain of functions, where a null can short-circuit each step. A function is only applied if the previous value is not null, otherwise this method returns null.

Functions.applyNonNull(" abc ", String::toUpperCase, String::trim, StringUtils::reverse) = "CBA"
Functions.applyNonNull(null, String::toUpperCase, String::trim, StringUtils::reverse)    = null
Functions.applyNonNull(" abc ", s -> null, String::trim, StringUtils::reverse)           = null
Functions.applyNonNull(" abc ", String::toUpperCase, s -> null, StringUtils::reverse)    = null
Functions.applyNonNull(" abc ", String::toUpperCase, String::trim, s -> null)            = null

Useful when working with expressions that may return null as it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to using Optional that is shorter and has less allocation.

Parameters
<T>
The type of the input of this method and the first function.
<U>
The type of the result of the first function and the input to the second function.
<V>
The type of the result of the second function and the input to the third function.
<R>
The type of the result of the third function and this method.
value1:T

The value to apply the first function, may be null.

mapper1:Function<? super T, ? extends U>

The first function to apply, must not be null.

mapper2:Function<? super U, ? extends V>

The second function to apply, must not be null.

mapper3:Function<? super V, ? extends R>

The third function to apply, must not be null.

Returns:R

The result of the final function (which may be null) or null if the input value or any intermediate value is null.

Since
3.19.0
See Also
applyNonNull(Object, Function), applyNonNull(Object, Function, Function)
functionback to summary
public static <T, R> Function<T, R> function(final Function<T, R> function)

Starts a fluent chain like function(foo::bar).andThen(...).andThen(...).apply(...);

Parameters
<T>
Input type.
<R>
Return type.
function:Function<T, R>

the argument to return.

Returns:Function<T, R>

the argument