Class PredicateSpecificationEvaluator<T>

java.lang.Object
com.reallifedeveloper.tools.test.database.inmemory.PredicateSpecificationEvaluator<T>
Type Parameters:
T - the type of entity for which the specification

public final class PredicateSpecificationEvaluator<T> extends Object
An evaluator of org.springframework.data.jpa.domain.PredicateSpecification instances.

This implementation is maintained in a single, large, file. This is because, a) the functionatlity has a clear focus with a small public API, and b) the code was written for the main part by ChatGPT, and I want to make it clear what code was generated by AI.

Author:
ChatGPT, RealLifeDeveloper
See Also:
  • Constructor Details

    • PredicateSpecificationEvaluator

      public PredicateSpecificationEvaluator()
  • Method Details

    • matches

      public boolean matches(org.springframework.data.jpa.domain.PredicateSpecification<T> specification, T entity)
      Checks if the given entity matches the given specification.
      Parameters:
      specification - the PredicateSpecification to use
      entity - the entity to check
      Returns:
      true if specification matches entity, false otherwise
    • filter

      public List<T> filter(org.springframework.data.jpa.domain.PredicateSpecification<T> specification, Collection<T> entities)
      Filters a collection of entities based on if they match the given specification or not.
      Parameters:
      specification - the PredicateSpecification to use
      entities - the collection of entities to filter
      Returns:
      a list of the entities from the entities collection that match specification
    • registerFunction

      public <R> PredicateSpecificationEvaluator<T> registerFunction(String name, Class<R> resultType, List<Class<?>> parameterTypes, PredicateSpecificationEvaluator.EvaluationFunction function)
      Registers a function that can be used by CriteriaBuilder.function.

      An example of registering a function:

      
       evaluator.registerFunction("concat_with_separator", String.class, arguments -> {
           String separator = (String) arguments.get(0);
           String left = (String) arguments.get(1);
           String right = (String) arguments.get(2);
           if (separator == null || left == null || right == null) {
               return null;
           }
           return left + separator + right;
       });
       
      Type Parameters:
      R - the return type of the function to register
      Parameters:
      name - the name of the function to register
      resultType - the class representing the return type of the function to register
      parameterTypes - a list of class objects representing the type of the parameters
      function - the function to register
      Returns:
      the PredicationSpecificationEvaluator itself, in order to support nested (fluent) calls
    • registerFunction

      public <A, R> PredicateSpecificationEvaluator<T> registerFunction(String name, Class<R> resultType, Class<A> argumentType, Function<A,R> function)
      Registers a single-parameter function thet can be used by CriteriaBuilder.function.

      This is a convenience method so that you can register functions with a single parameter like this:

      
       evaluator.registerFunction("lower", String.class, String.class, value -> value == null ? null : value.toLowerCase(Locale.ROOT));
       
      Type Parameters:
      A - the type of the only function parameter
      R - the return type of the function to register
      Parameters:
      name - the name of the function to register
      resultType - the class representing the return type of the function to register
      argumentType - the class representing the type of the only function parameter
      function - the function to register
      Returns:
      the PredicationSpecificationEvaluator itself, in order to support nested (fluent) calls