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
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA function that can be registered using theregisterFunction(String, Class, List, EvaluationFunction)method. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfilter(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.booleanChecks if the given entity matches the given specification.<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 byCriteriaBuilder.function.registerFunction(String name, Class<R> resultType, List<Class<?>> parameterTypes, PredicateSpecificationEvaluator.EvaluationFunction function) Registers a function that can be used byCriteriaBuilder.function.
-
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- thePredicateSpecificationto useentity- the entity to check- Returns:
trueifspecificationmatchesentity,falseotherwise
-
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- thePredicateSpecificationto useentities- the collection of entities to filter- Returns:
- a list of the entities from the
entitiescollection that matchspecification
-
registerFunction
public <R> PredicateSpecificationEvaluator<T> registerFunction(String name, Class<R> resultType, List<Class<?>> parameterTypes, PredicateSpecificationEvaluator.EvaluationFunction function) Registers a function that can be used byCriteriaBuilder.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 registerresultType- the class representing the return type of the function to registerparameterTypes- a list of class objects representing the type of the parametersfunction- the function to register- Returns:
- the
PredicationSpecificationEvaluatoritself, 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 byCriteriaBuilder.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 parameterR- the return type of the function to register- Parameters:
name- the name of the function to registerresultType- the class representing the return type of the function to registerargumentType- the class representing the type of the only function parameterfunction- the function to register- Returns:
- the
PredicationSpecificationEvaluatoritself, in order to support nested (fluent) calls
-