Class FunctionUtils

java.lang.Object
org.daiitech.naftah.builtin.utils.FunctionUtils

public final class FunctionUtils extends Object
Utility class providing functional-style operations on various input types, including 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 Details

    • FunctionUtils

      private FunctionUtils()
      Private constructor to prevent instantiation. Always throws a NaftahBugError when called.
  • Method Details

    • allMatch

      public static <T> boolean allMatch(Object input, Predicate<T> predicate)
      Returns true if 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 element
      • Collection — evaluated over its elements
      • Map — 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 be null
      predicate - the predicate applied to each evaluated value
      Returns:
      true if every evaluated value satisfies the predicate; false otherwise
      Throws:
      NaftahBugError - if input is null
    • noneMatch

      public static <T> boolean noneMatch(Object input, Predicate<T> predicate)
      Returns true if 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 be null
      predicate - the predicate to test each element
      Returns:
      true if no elements match the predicate; false otherwise
    • anyMatch

      public static <T> boolean anyMatch(Object input, Predicate<T> predicate)
      Returns true if 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 be null
      predicate - the predicate to test each element
      Returns:
      true if any element matches the predicate; false otherwise
    • reduce

      public static Object reduce(Object input, BinaryOperator<Object> combiner)
      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 elements
      • Collection — 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 returns null without invoking the combiner.

      Parameters:
      input - the input object to reduce; may be null
      combiner - the binary operator used to combine values
      Returns:
      the reduced value, or null if input is null
    • execute

      public static <T, R> R execute(NaftahObject naftahObject, Class<?> rawClass, ThrowingFunction<T,R> executionFunction)
      Executes a function on the underlying Java value of a NaftahObject after validating its runtime type.

      The provided NaftahObject is unwrapped using NaftahObject.get(boolean) with true. The unwrapped value must be non-null and assignable to rawClass. If these conditions are met, the value is cast to T and passed to the supplied executionFunction.

      If the NaftahObject is null, unwraps to null, or does not match the expected runtime type, a NaftahBugError is thrown via ExceptionUtils.newIllegalArgumentException(Object).

      Type Parameters:
      T - the expected runtime type of the unwrapped value
      R - the return type of the function
      Parameters:
      naftahObject - the NaftahObject to unwrap and validate
      rawClass - the expected raw runtime class of the unwrapped value
      executionFunction - a function to execute on the validated value
      Returns:
      the result of executionFunction.apply(T)
      Throws:
      NaftahBugError - if the object is null, cannot be unwrapped, or is not assignable to rawClass
    • execute

      public static <T> void execute(NaftahObject naftahObject, Class<?> rawClass, Consumer<T> executionConsumer)
      Executes a consumer on the underlying Java value of a NaftahObject after validating its runtime type.

      The provided NaftahObject is unwrapped using NaftahObject.get(boolean) with true. The unwrapped value must be non-null and assignable to rawClass. If these conditions are met, the value is cast to T and passed to the supplied executionConsumer.

      If the NaftahObject is null, unwraps to null, or does not match the expected runtime type, a NaftahBugError is thrown via ExceptionUtils.newIllegalArgumentException(Object).

      Type Parameters:
      T - the expected runtime type of the unwrapped value
      Parameters:
      naftahObject - the NaftahObject to unwrap and validate
      rawClass - the expected raw runtime class of the unwrapped value
      executionConsumer - a consumer to execute on the validated value
      Throws:
      NaftahBugError - if the object is null, cannot be unwrapped, or is not assignable to rawClass