Class FunctionUtils
Collection, Map, arrays, and single objects.
Methods include allMatch, noneMatch, anyMatch, and reduce,
allowing evaluation of predicates and reduction operations on diverse data structures.
This class is not meant to be instantiated. Attempting to do so will throw
a NaftahBugError.
- Author:
- Chakib Daii
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleanReturnstrueif all elements contained in the given input satisfy the supplied predicate.static <T> booleanReturnstrueif at least one element in the input matches the given predicate.static <T> voidexecute(NaftahObject naftahObject, Class<?> rawClass, Consumer<T> executionConsumer) Executes a consumer on the underlying Java value of aNaftahObjectafter validating its runtime type.static <T,R> R execute(NaftahObject naftahObject, Class<?> rawClass, ThrowingFunction<T, R> executionFunction) Executes a function on the underlying Java value of aNaftahObjectafter validating its runtime type.static <T> booleanReturnstrueif no elements in the input match the given predicate.static Objectreduce(Object input, BinaryOperator<Object> combiner) Reduces the given input to a single value using the specified combiner.
-
Constructor Details
-
FunctionUtils
private FunctionUtils()Private constructor to prevent instantiation. Always throws aNaftahBugErrorwhen called.
-
-
Method Details
-
allMatch
Returnstrueif all elements contained in the given input satisfy the supplied predicate.The input may represent a composite or a scalar value:
NTuple— evaluated element by elementCollection— evaluated over its elementsMap— evaluated over its entries- Array — evaluated over its elements
- Any other object — evaluated as a single value
This method is intended for use in recursive or dynamically-typed processing where the concrete structure of the input is not known at compile time.
- Type Parameters:
T- the type of values tested by the predicate- Parameters:
input- the input object to evaluate; must not benullpredicate- the predicate applied to each evaluated value- Returns:
trueif every evaluated value satisfies the predicate;falseotherwise- Throws:
NaftahBugError- ifinputisnull
-
noneMatch
Returnstrueif no elements in the input match the given predicate.This method is logically equivalent to negating
allMatch(Object, Predicate)with the inverted predicate.- Type Parameters:
T- the type of elements to be tested by the predicate- Parameters:
input- the input object to evaluate; may benullpredicate- the predicate to test each element- Returns:
trueif no elements match the predicate;falseotherwise
-
anyMatch
Returnstrueif at least one element in the input matches the given predicate.This method is logically equivalent to negating
noneMatch(Object, Predicate).- Type Parameters:
T- the type of elements to be tested by the predicate- Parameters:
input- the input object to evaluate; may benullpredicate- the predicate to test each element- Returns:
trueif any element matches the predicate;falseotherwise
-
reduce
Reduces the given input to a single value using the specified combiner.The input may represent either a composite structure or a scalar value:
NTuple— reduced by combining its elementsCollection— reduced by combining its elements- Array — reduced by combining its elements
Map— reduced by combining its values- Any other object — returned as-is
If the input is
null, this method returnsnullwithout invoking the combiner.- Parameters:
input- the input object to reduce; may benullcombiner- the binary operator used to combine values- Returns:
- the reduced value, or
nullifinputisnull
-
execute
public static <T,R> R execute(NaftahObject naftahObject, Class<?> rawClass, ThrowingFunction<T, R> executionFunction) Executes a function on the underlying Java value of aNaftahObjectafter validating its runtime type.The provided
NaftahObjectis unwrapped usingNaftahObject.get(boolean)withtrue. The unwrapped value must be non-nulland assignable torawClass. If these conditions are met, the value is cast toTand passed to the suppliedexecutionFunction.If the
NaftahObjectisnull, unwraps tonull, or does not match the expected runtime type, aNaftahBugErroris thrown viaExceptionUtils.newIllegalArgumentException(Object).- Type Parameters:
T- the expected runtime type of the unwrapped valueR- the return type of the function- Parameters:
naftahObject- theNaftahObjectto unwrap and validaterawClass- the expected raw runtime class of the unwrapped valueexecutionFunction- a function to execute on the validated value- Returns:
- the result of
executionFunction.apply(T) - Throws:
NaftahBugError- if the object isnull, cannot be unwrapped, or is not assignable torawClass
-
execute
public static <T> void execute(NaftahObject naftahObject, Class<?> rawClass, Consumer<T> executionConsumer) Executes a consumer on the underlying Java value of aNaftahObjectafter validating its runtime type.The provided
NaftahObjectis unwrapped usingNaftahObject.get(boolean)withtrue. The unwrapped value must be non-nulland assignable torawClass. If these conditions are met, the value is cast toTand passed to the suppliedexecutionConsumer.If the
NaftahObjectisnull, unwraps tonull, or does not match the expected runtime type, aNaftahBugErroris thrown viaExceptionUtils.newIllegalArgumentException(Object).- Type Parameters:
T- the expected runtime type of the unwrapped value- Parameters:
naftahObject- theNaftahObjectto unwrap and validaterawClass- the expected raw runtime class of the unwrapped valueexecutionConsumer- a consumer to execute on the validated value- Throws:
NaftahBugError- if the object isnull, cannot be unwrapped, or is not assignable torawClass
-