Class CheckerFunction<T,R>

java.lang.Object
util.AbstractChecker<Function<T,R>,CheckerFunction<T,R>>
specialized_checkers.lambda.CheckerFunction<T,R>
Type Parameters:
T - the type of the input to the Function being checked
R - the type of the result returned by the Function
All Implemented Interfaces:
InterfaceChecker<AbstractChecker<Function<T,R>,CheckerFunction<T,R>>,Function<T,R>>

public class CheckerFunction<T,R> extends AbstractChecker<Function<T,R>,CheckerFunction<T,R>>
A specialized checker for Function instances, providing a fluent API for validating function behavior and results. This class allows assertions such as verifying that a function does not throw exceptions, produces expected results, or returns non-null values for given inputs. It also supports optional deep cloning of input objects to ensure immutability during checks.

Example usage:

     CheckerFunction<String, Integer> checker = CheckerFunction.check(String::length);
     checker.producesExpected("abc", 3)
            .applyWithoutException("test")
            .producesNonNull("hello");
 

Deep cloning can be enabled to ensure that the input object is not mutated by the function:

     checker.activateDeepClone();
 
See Also:
  • Constructor Details

    • CheckerFunction

      protected CheckerFunction(Function<T,R> function, String name)
      Constructs a new CheckerFunction with the specified function and name.
      Parameters:
      function - the Function to be used by this checker
      name - the name identifying this checker
  • Method Details

    • check

      public static <T, R> CheckerFunction<T,R> check(Function<T,R> function, String name)
      Creates a new CheckerFunction for the given Function instance with a custom name.
      Type Parameters:
      T - the type of the input to the Function being checked
      R - the type of the result returned by the Function
      Parameters:
      function - the Function instance to be checked
      name - the name to identify this checker instance (useful for error messages)
      Returns:
      a new CheckerFunction for the provided Function
    • check

      public static <T, R> CheckerFunction<T,R> check(Function<T,R> function)
      Creates a new CheckerFunction for the given Function instance with a default name.
      Type Parameters:
      T - the type of the input to the Function being checked
      R - the type of the result returned by the Function
      Parameters:
      function - the Function instance to be checked
      Returns:
      a new CheckerFunction for the provided Function
    • self

      protected CheckerFunction<T,R> self()
      Returns this checker instance (for fluent API usage).
      Specified by:
      self in class AbstractChecker<Function<T,R>,CheckerFunction<T,R>>
      Returns:
      this CheckerFunction instance for further validation and chaining
    • activateDeepClone

      public CheckerFunction<T,R> activateDeepClone()
      Enables deep cloning of input objects before passing them to the Function.

      This is useful to ensure that the original input is not modified by the operation, especially when the function may mutate its input.

      Returns:
      this CheckerFunction instance for further validation and chaining
    • deactivateDeepClone

      public CheckerFunction<T,R> deactivateDeepClone()
      Disables deep cloning of input objects before passing them to the Function.
      Returns:
      this CheckerFunction instance for further validation and chaining
    • getInput

      public T getInput(T input)
      Returns the input object, deep-cloned if deep cloning is activated, otherwise returns the original input.
      Parameters:
      input - the input object to process
      Returns:
      the processed input (deep-cloned or original), depending on the current deep clone setting
    • applyWithoutException

      public CheckerFunction<T,R> applyWithoutException(T input)
      Asserts that applying the Function to the given input does not throw any exception.

      This check is useful to ensure that the function is safe to apply to the provided input and does not result in runtime errors.

      Parameters:
      input - the input object to be processed by the function
      Returns:
      this CheckerFunction instance for further validation and chaining
    • producesExpected

      public CheckerFunction<T,R> producesExpected(T input, R expected)
      Asserts that applying the Function to the given input produces the expected result.

      This check compares the actual result of the function with the expected value using content equality.

      Parameters:
      input - the input object to be processed by the function
      expected - the expected result to compare with the actual result of the function
      Returns:
      this CheckerFunction instance for further validation and chaining
    • producesNonNull

      public CheckerFunction<T,R> producesNonNull(T input)
      Asserts that applying the Function to the given input produces a non-null result.

      This check is useful to ensure that the function always returns a valid (non-null) result for the given input.

      Parameters:
      input - the input object to be processed by the function
      Returns:
      this CheckerFunction instance for further validation and chaining