Class FunctionUtils
java.lang.Object
com.thanlinardos.spring_enterprise_library.objects.utils.FunctionUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,O, R> Function <T, R> apply(BiFunction<T, O, R> function, O object) Method that can be used to apply with a single method argument to a bi-function.static <T,O, S> Consumer <T> chainAllFromOther(Collection<O> others, Function<O, S> func, BiConsumer<S, T> consumer) Creates a null-safe consumer that applies a function to a set of another objects, then a consumer to the result.static <T,O, S> Consumer <T> chainAllFromSelf(Collection<O> others, Function<T, S> func, BiConsumer<S, O> consumer) Creates a null-safe consumer that applies a function to the element, then a consumer to input object.static <T,O, S> Consumer <T> chainFromOther(O other, Function<O, S> func, BiConsumer<S, T> consumer) Creates a null-safe consumer that applies a function to another object, then a consumer to the result.static <T,O, S> Consumer <T> chainFromSelf(O other, Function<T, S> func, BiConsumer<S, O> consumer) Creates a null-safe consumer that applies a function to the element, then a consumer to input object.static <T1,T2, R>
Function<T1, R> Method that chains two methods and return the combined result.composeAllNullSafe(Function<T1, Set<T2>> firstGetter, Function<T2, Set<R>> finalGetter) Method that chains two methods in a null safe wrapper and return the combined result.static <T1,T2, R>
Function<T1, R> composeNullSafe(Function<T1, T2> firstGetter, Function<T2, R> finalGetter) Method that chains two methods in a null safe wrapper and return the combined result.static <T1,T2, T3, R>
Function<T1, R> composeNullSafe(Function<T1, T2> firstGetter, Function<T2, T3> secondGetter, Function<T3, R> finalGetter) Method that chains three methods in a null safe wrapper and return the combined result.composeWithSetNullSafe(Function<T1, Set<T2>> firstGetter, Function<T2, R> finalGetter) Method that chains two methods in a null safe wrapper and return the combined result.static ErrorCodenegateBoolean(Function<T, Boolean> func) Negates the result of input function.static <U extends BasicIdJpa,R>
RrethrowWithEntityId(U entity, Function<U, R> function) Helper method to provide ids of entity input parameters in the resulting exception if the given function throws an exceptionstream(Function<T, Collection<R>> function) streamOptional(Function<T, Optional<R>> function)
-
Constructor Details
-
FunctionUtils
public FunctionUtils()
-
-
Method Details
-
negateBoolean
-
apply
Method that can be used to apply with a single method argument to a bi-function.- Type Parameters:
T- type of the starting elementO- type of the object to be appliedR- type of result- Parameters:
function- the function to call with the inputobject- the input- Returns:
- a function that can be used to apply the input to the bi-function
-
compose
Method that chains two methods and return the combined result.- Type Parameters:
T1- type of the first methodT2- type of the final methodR- type the final method returns- Parameters:
func1- the first function of the chainfunc2- the final function of the chain- Returns:
- the method returns the result of the final function
-
composeNullSafe
public static <T1,T2, Function<T1,R> R> composeNullSafe(Function<T1, T2> firstGetter, Function<T2, R> finalGetter) Method that chains two methods in a null safe wrapper and return the combined result.- Type Parameters:
T1- type of the first methodT2- type of the final methodR- type the final method returns- Parameters:
firstGetter- the first function of the chainfinalGetter- the final function of the chain- Returns:
- the method returns the result of the final function
-
composeWithSetNullSafe
public static <T1,T2, Function<T1,R> Set<R>> composeWithSetNullSafe(Function<T1, Set<T2>> firstGetter, Function<T2, R> finalGetter) Method that chains two methods in a null safe wrapper and return the combined result.- Type Parameters:
T1- type set of the first methodT2- type of the final methodR- type the final method returns- Parameters:
firstGetter- the first function of the chainfinalGetter- the final function of the chain- Returns:
- the method returns the result of the final function
-
composeAllNullSafe
public static <T1,T2, Function<T1,R> Set<R>> composeAllNullSafe(Function<T1, Set<T2>> firstGetter, Function<T2, Set<R>> finalGetter) Method that chains two methods in a null safe wrapper and return the combined result.- Type Parameters:
T1- type set of the first methodT2- type set of the final methodR- type the final method returns- Parameters:
firstGetter- the first function of the chainfinalGetter- the final function of the chain- Returns:
- the method returns the result of the final function
-
composeNullSafe
public static <T1,T2, Function<T1,T3, R> R> composeNullSafe(Function<T1, T2> firstGetter, Function<T2, T3> secondGetter, Function<T3, R> finalGetter) Method that chains three methods in a null safe wrapper and return the combined result.- Type Parameters:
T1- type of the first methodT2- type of the second methodT3- type of the final methodR- type the final method returns- Parameters:
firstGetter- the first function of the chainsecondGetter- the second function of the chainfinalGetter- the final function of the chain- Returns:
- the method returns the result of the final function
-
chainFromOther
public static <T,O, Consumer<T> chainFromOtherS> (@Nullable O other, Function<O, S> func, BiConsumer<S, T> consumer) Creates a null-safe consumer that applies a function to another object, then a consumer to the result. This is useful, e.g., when needing to consume elements of a (possible composed) getter.- Type Parameters:
T- type of the consumed elementO- type of the "starting" objectS- type of intermediate element used byconsumer- Parameters:
other- object to start chain fromfunc- function to apply tootherconsumer- bi-consumer that takes the result offuncand the applied element- Returns:
- single-element consumer for an object of type
T
-
chainAllFromOther
public static <T,O, Consumer<T> chainAllFromOtherS> (@Nullable Collection<O> others, Function<O, S> func, BiConsumer<S, T> consumer) Creates a null-safe consumer that applies a function to a set of another objects, then a consumer to the result. This is useful, e.g., when needing to consume elements of a (possible composed) getter.- Type Parameters:
T- type of the consumed elementO- type of the "starting" objectS- type of intermediate element used byconsumer- Parameters:
others- objects to start chain fromfunc- function to apply tootherconsumer- bi-consumer that takes the result offuncand the applied element- Returns:
- single-element consumer for an object of type
T
-
chainFromSelf
public static <T,O, Consumer<T> chainFromSelfS> (@Nullable O other, Function<T, S> func, BiConsumer<S, O> consumer) Creates a null-safe consumer that applies a function to the element, then a consumer to input object. This is useful, e.g., when needing to consume elements of a (possible composed) getter.- Type Parameters:
T- type of the consumed elementO- type of the other objectS- type of intermediate element used byconsumer- Parameters:
other- object to consume in the endfunc- function to apply to the elementconsumer- bi-consumer that takes the result offuncandother- Returns:
- single-element consumer for an object of type
T
-
chainAllFromSelf
public static <T,O, Consumer<T> chainAllFromSelfS> (@Nullable Collection<O> others, Function<T, S> func, BiConsumer<S, O> consumer) Creates a null-safe consumer that applies a function to the element, then a consumer to input object. This is useful, e.g., when needing to consume elements of a (possible composed) getter.- Type Parameters:
T- type of the consumed elementO- type of the other objectS- type of intermediate element used byconsumer- Parameters:
others- objects to consume in the endfunc- function to apply to the elementconsumer- bi-consumer that takes the result offuncand another- Returns:
- single-element consumer for an object of type
T
-
stream
Returns aFunctionthat applies theCollection.stream()method on the result of the inputFunction. This can be used to shorten stream chains that first does a map and then a flatMap withCollection.stream().- Type Parameters:
T- type of the input elementR- type of the elements in the collection returned by the inputFunction- Parameters:
function- the inputFunction- Returns:
- a
Functionthat applies theCollection.stream()method on the result of the inputFunction
-
streamOptional
Returns aFunctionthat applies theOptional.stream()method on the result of the inputFunction. This can be used to shorten stream chains that first does a map and then a flatMap withOptional.stream().- Type Parameters:
T- type of the input elementR- type of the element in the optional returned by the inputFunction- Parameters:
function- the inputFunction- Returns:
- a
Functionthat applies theOptional.stream()method on the result of the inputFunction
-
rethrowWithEntityId
Helper method to provide ids of entity input parameters in the resulting exception if the given function throws an exception- Type Parameters:
U- class of the given entityR- class of returned byfunction- Parameters:
entity- a given entity extendingBasicIdJpafunction- a function taking the givenentity- Returns:
- the result of applying the function to the given entity, rethrowing any exception with an exception mentioning the entity id
-
getCoreExceptionErrorCodeOrUnexpectedSystemError
- Parameters:
e- a given exception- Returns:
- the
ErrorCodef it is aCoreExceptionotherwiseErrorCode.UNEXPECTED_ERROR.
-