Interface PredicateSpecification<T>
- Type Parameters:
T- the type of theFrom targetto which the specification is applied.
- All Superinterfaces:
Serializable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Specification in the sense of Domain Driven Design.
Specifications can be composed into higher order functions from other specifications using
and(PredicateSpecification), or(PredicateSpecification) or factory methods such as
allOf(Iterable) with reduced type interference of the query source type.
PredicateSpecifications are building blocks for composition and do not express their type opinion towards a specific entity source or join source type for improved reuse.
Composition considers whether one or more specifications contribute to the overall predicate by returning a
Predicate or null. Specifications returning null, such as unrestricted(), are
considered to not contribute to the overall predicate, and their result is not considered in the final predicate.
- Since:
- 4.0
- Author:
- Mark Paluch, Peter Aisher
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> PredicateSpecification<T> allOf(Iterable<PredicateSpecification<T>> specifications) Applies an AND operation to all the givenPredicateSpecifications.static <T> PredicateSpecification<T> allOf(PredicateSpecification<T>... specifications) Applies an AND operation to all the givenPredicateSpecifications.default PredicateSpecification<T> and(PredicateSpecification<T> other) ANDs the given PredicateSpecification to the current one.static <T> PredicateSpecification<T> anyOf(Iterable<PredicateSpecification<T>> specifications) Applies an OR operation to all the givenPredicateSpecifications.static <T> PredicateSpecification<T> anyOf(PredicateSpecification<T>... specifications) Applies an OR operation to all the givenPredicateSpecifications.static <T> PredicateSpecification<T> not(PredicateSpecification<T> spec) Negates the givenPredicateSpecification.default PredicateSpecification<T> or(PredicateSpecification<T> other) ORs the given specification to the current one.@Nullable jakarta.persistence.criteria.PredicatetoPredicate(jakarta.persistence.criteria.From<?, T> from, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder) Creates a WHERE clause for a query of the referenced entity in form of aPredicatefor the givenFromandCriteriaBuilder.static <T> PredicateSpecification<T> Simple static factory method to create a specification which does not participate in matching.static <T> PredicateSpecification<T> where(PredicateSpecification<T> spec) Simple static factory method to add some syntactic sugar around a PredicateSpecification.
-
Method Details
-
unrestricted
Simple static factory method to create a specification which does not participate in matching. The specification returned isnull-like, and is elided in all operations.unrestricted().and(other) // consider only `other` unrestricted().or(other) // consider only `other` not(unrestricted()) // equivalent to `unrestricted()`
- Type Parameters:
T- the type of theFromthe resulting PredicateSpecification operates on.- Returns:
- guaranteed to be not null.
-
where
Simple static factory method to add some syntactic sugar around a PredicateSpecification.- Type Parameters:
T- the type of theFromthe resulting PredicateSpecification operates on.- Parameters:
spec- must not be null.- Returns:
- guaranteed to be not null.
- Since:
- 2.0
-
and
@Contract("_ -> new") @CheckReturnValue default PredicateSpecification<T> and(PredicateSpecification<T> other) ANDs the given PredicateSpecification to the current one.- Parameters:
other- the otherPredicateSpecification.- Returns:
- the conjunction of the specifications.
-
or
@Contract("_ -> new") @CheckReturnValue default PredicateSpecification<T> or(PredicateSpecification<T> other) ORs the given specification to the current one.- Parameters:
other- the otherPredicateSpecification.- Returns:
- the disjunction of the specifications.
-
not
Negates the givenPredicateSpecification.- Type Parameters:
T- the type of theFromthe resulting PredicateSpecification operates on.- Parameters:
spec- can be null.- Returns:
- guaranteed to be not null.
-
allOf
@SafeVarargs static <T> PredicateSpecification<T> allOf(PredicateSpecification<T>... specifications) Applies an AND operation to all the givenPredicateSpecifications. Ifspecificationsis empty, the resultingPredicateSpecificationwill beunrestricted()applying to all objects.- Parameters:
specifications- thePredicateSpecifications to compose.- Returns:
- the conjunction of the specifications.
- See Also:
-
allOf
Applies an AND operation to all the givenPredicateSpecifications. Ifspecificationsis empty, the resultingPredicateSpecificationwill beunrestricted()applying to all objects.- Parameters:
specifications- thePredicateSpecifications to compose.- Returns:
- the conjunction of the specifications.
- See Also:
-
anyOf
@SafeVarargs static <T> PredicateSpecification<T> anyOf(PredicateSpecification<T>... specifications) Applies an OR operation to all the givenPredicateSpecifications. Ifspecificationsis empty, the resultingPredicateSpecificationwill beunrestricted()applying to all objects.- Parameters:
specifications- thePredicateSpecifications to compose.- Returns:
- the disjunction of the specifications.
- See Also:
-
anyOf
Applies an OR operation to all the givenPredicateSpecifications. Ifspecificationsis empty, the resultingPredicateSpecificationwill beunrestricted()applying to all objects.- Parameters:
specifications- thePredicateSpecifications to compose.- Returns:
- the disjunction of the specifications.
- See Also:
-
toPredicate
@Nullable jakarta.persistence.criteria.Predicate toPredicate(jakarta.persistence.criteria.From<?, T> from, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder) Creates a WHERE clause for a query of the referenced entity in form of aPredicatefor the givenFromandCriteriaBuilder.- Parameters:
from- must not be null.criteriaBuilder- must not be null.- Returns:
- a
Predicate, may be null.
-