Class Functions
- java.lang.Object
-
- org.apache.commons.lang3.function.Functions
-
public final class Functions extends java.lang.ObjectFactory forFunction.- Since:
- 3.14.0
-
-
Constructor Summary
Constructors Modifier Constructor Description privateFunctions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T,R>
Rapply(java.util.function.Function<T,R> function, T object)Applies theFunctionon the object if the function is notnull.static <T,R>
RapplyNonNull(T value, java.util.function.Function<? super T,? extends R> mapper)Applies a value to a function if the value isn'tnull, otherwise the method returnsnull.static <T,U,R>
RapplyNonNull(T value1, java.util.function.Function<? super T,? extends U> mapper1, java.util.function.Function<? super U,? extends R> mapper2)Applies values to a chain of functions, where anullcan short-circuit each step.static <T,U,V,R>
RapplyNonNull(T value1, java.util.function.Function<? super T,? extends U> mapper1, java.util.function.Function<? super U,? extends V> mapper2, java.util.function.Function<? super V,? extends R> mapper3)Applies values to a chain of functions, where anullcan short-circuit each step.static <T,R>
java.util.function.Function<T,R>function(java.util.function.Function<T,R> function)Starts a fluent chain likefunction(foo::bar).andThen(...).andThen(...).apply(...);
-
-
-
Method Detail
-
apply
public static <T,R> R apply(java.util.function.Function<T,R> function, T object)Applies theFunctionon the object if the function is notnull. Otherwise, does nothing and returnsnull.- Type Parameters:
T- the type of the argument the function applies.R- the type of the result the function returns.- Parameters:
function- the function to apply.object- the object to apply the function.- Returns:
- the value the function returns if the function is not
null;nullotherwise. - Since:
- 3.15.0
-
applyNonNull
public static <T,R> R applyNonNull(T value, java.util.function.Function<? super T,? extends R> mapper)Applies a value to a function if the value isn'tnull, otherwise the method returnsnull. If the value isn'tnullthen return the result of the applying function.Functions.applyNonNull("a", String::toUpperCase) = "A" Functions.applyNonNull(null, String::toUpperCase) = null Functions.applyNonNull("a", s -> null) = nullUseful when working with expressions that may return
nullas it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to usingOptionalthat is shorter and has less allocation.- Type 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.- Parameters:
value- The value to apply the function to, may benull.mapper- The function to apply, must not benull.- Returns:
- The result of the function (which may be
null) ornullif the input value isnull. - Since:
- 3.19.0
- See Also:
applyNonNull(Object, Function, Function),applyNonNull(Object, Function, Function, Function)
-
applyNonNull
public static <T,U,R> R applyNonNull(T value1, java.util.function.Function<? super T,? extends U> mapper1, java.util.function.Function<? super U,? extends R> mapper2)Applies values to a chain of functions, where anullcan short-circuit each step. A function is only applied if the previous value is notnull, otherwise this method returnsnull.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) = nullUseful when working with expressions that may return
nullas it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to usingOptionalthat is shorter and has less allocation.- Type 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.- Parameters:
value1- The value to apply the functions to, may benull.mapper1- The first function to apply, must not benull.mapper2- The second function to apply, must not benull.- Returns:
- The result of the final function (which may be
null) ornullif the input value or any intermediate value isnull. - Since:
- 3.19.0
- See Also:
applyNonNull(Object, Function),applyNonNull(Object, Function, Function, Function)
-
applyNonNull
public static <T,U,V,R> R applyNonNull(T value1, java.util.function.Function<? super T,? extends U> mapper1, java.util.function.Function<? super U,? extends V> mapper2, java.util.function.Function<? super V,? extends R> mapper3)Applies values to a chain of functions, where anullcan short-circuit each step. A function is only applied if the previous value is notnull, otherwise this method returnsnull.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) = nullUseful when working with expressions that may return
nullas it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to usingOptionalthat is shorter and has less allocation.- Type 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.- Parameters:
value1- The value to apply the first function, may benull.mapper1- The first function to apply, must not benull.mapper2- The second function to apply, must not benull.mapper3- The third function to apply, must not benull.- Returns:
- The result of the final function (which may be
null) ornullif the input value or any intermediate value isnull. - Since:
- 3.19.0
- See Also:
applyNonNull(Object, Function),applyNonNull(Object, Function, Function)
-
function
public static <T,R> java.util.function.Function<T,R> function(java.util.function.Function<T,R> function)
Starts a fluent chain likefunction(foo::bar).andThen(...).andThen(...).apply(...);- Type Parameters:
T- Input type.R- Return type.- Parameters:
function- the argument to return.- Returns:
- the argument
-
-