1 /*
2  * Copyright 2018-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.data.jpa.domain;
17
18 import java.io.Serializable;
19
20 import javax.persistence.criteria.CriteriaBuilder;
21 import javax.persistence.criteria.CriteriaQuery;
22 import javax.persistence.criteria.Predicate;
23 import javax.persistence.criteria.Root;
24
25 import org.springframework.lang.Nullable;
26
27 /**
28  * Helper class to support specification compositions.
29  *
30  * @author Sebastian Staudt
31  * @author Oliver Gierke
32  * @author Jens Schauder
33  * @see Specification
34  * @since 2.2
35  */

36 class SpecificationComposition {
37
38     interface Combiner extends Serializable {
39         Predicate combine(CriteriaBuilder builder, @Nullable Predicate lhs, @Nullable Predicate rhs);
40     }
41
42     static <T> Specification<T> composed(@Nullable Specification<T> lhs, @Nullable Specification<T> rhs,
43             Combiner combiner) {
44
45         return (root, query, builder) -> {
46
47             Predicate otherPredicate = toPredicate(lhs, root, query, builder);
48             Predicate thisPredicate = toPredicate(rhs, root, query, builder);
49
50             if (thisPredicate == null) {
51                 return otherPredicate;
52             }
53
54             return otherPredicate == null ? thisPredicate : combiner.combine(builder, thisPredicate, otherPredicate);
55         };
56     }
57
58     @Nullable
59     private static <T> Predicate toPredicate(@Nullable Specification<T> specification, Root<T> root, CriteriaQuery<?> query,
60             CriteriaBuilder builder) {
61         return specification == null ? null : specification.toPredicate(root, query, builder);
62     }
63 }
64