Skip to content

Commit 4c17e29

Browse files
koraktorschauder
authored andcommitted
DATAJPA-1449 - Removal of Specifications.
`Specifications` is deprecated since 2.0. This removes it completely. * Moves static helper code into a private helper class. * Removes references to `Specifications` from `Specification`. Original pull request: #300.
1 parent 580d2a6 commit 4c17e29

File tree

4 files changed

+64
-304
lines changed

4 files changed

+64
-304
lines changed

src/main/java/org/springframework/data/jpa/domain/Specification.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package org.springframework.data.jpa.domain;
1717

18-
import static org.springframework.data.jpa.domain.Specifications.CompositionType.*;
18+
import static org.springframework.data.jpa.domain.SpecificationComposition.*;
19+
import static org.springframework.data.jpa.domain.SpecificationComposition.CompositionType.*;
1920

2021
import java.io.Serializable;
2122

@@ -35,11 +36,12 @@
3536
* @author Sebastian Staudt
3637
* @author Mark Paluch
3738
*/
38-
@SuppressWarnings("deprecation")
3939
public interface Specification<T> extends Serializable {
4040

4141
long serialVersionUID = 1L;
4242

43+
Specification EMPTY_SPEC = (root, query, criteriaBuilder) -> null;
44+
4345
/**
4446
* Negates the given {@link Specification}.
4547
*
@@ -49,7 +51,7 @@ public interface Specification<T> extends Serializable {
4951
* @since 2.0
5052
*/
5153
static <T> Specification<T> not(Specification<T> spec) {
52-
return Specifications.negated(spec);
54+
return negated(spec);
5355
}
5456

5557
/**
@@ -61,7 +63,7 @@ static <T> Specification<T> not(Specification<T> spec) {
6163
* @since 2.0
6264
*/
6365
static <T> Specification<T> where(Specification<T> spec) {
64-
return Specifications.where(spec);
66+
return spec == null ? EMPTY_SPEC : spec;
6567
}
6668

6769
/**
@@ -72,7 +74,7 @@ static <T> Specification<T> where(Specification<T> spec) {
7274
* @since 2.0
7375
*/
7476
default Specification<T> and(Specification<T> other) {
75-
return Specifications.composed(this, other, AND);
77+
return composed(this, other, AND);
7678
}
7779

7880
/**
@@ -83,7 +85,7 @@ default Specification<T> and(Specification<T> other) {
8385
* @since 2.0
8486
*/
8587
default Specification<T> or(Specification<T> other) {
86-
return Specifications.composed(this, other, OR);
88+
return composed(this, other, OR);
8789
}
8890

8991
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.springframework.data.jpa.domain;
2+
3+
import org.springframework.lang.Nullable;
4+
5+
import javax.persistence.criteria.CriteriaBuilder;
6+
import javax.persistence.criteria.Predicate;
7+
8+
/**
9+
* Helper class to support specification compositions
10+
*
11+
* @author Sebastian Staudt
12+
* @see Specification
13+
*/
14+
class SpecificationComposition {
15+
16+
/**
17+
* Enum for the composition types for {@link Predicate}s. Can not be turned into lambdas as we need to be
18+
* serializable.
19+
*
20+
* @author Thomas Darimont
21+
*/
22+
enum CompositionType {
23+
24+
AND {
25+
@Override
26+
public Predicate combine(CriteriaBuilder builder, Predicate lhs, Predicate rhs) {
27+
return builder.and(lhs, rhs);
28+
}
29+
},
30+
31+
OR {
32+
@Override
33+
public Predicate combine(CriteriaBuilder builder, Predicate lhs, Predicate rhs) {
34+
return builder.or(lhs, rhs);
35+
}
36+
};
37+
38+
abstract Predicate combine(CriteriaBuilder builder, Predicate lhs, Predicate rhs);
39+
}
40+
41+
static <T> Specification<T> negated(@Nullable Specification<T> spec) {
42+
return (root, query, builder) -> spec == null ? null : builder.not(spec.toPredicate(root, query, builder));
43+
}
44+
45+
static <T> Specification<T> composed(@Nullable Specification<T> lhs, @Nullable Specification<T> rhs, CompositionType compositionType) {
46+
47+
return (root, query, builder) -> {
48+
49+
Predicate otherPredicate = rhs == null ? null : rhs.toPredicate(root, query, builder);
50+
Predicate thisPredicate = lhs == null ? null : lhs.toPredicate(root, query, builder);
51+
52+
return thisPredicate == null ? otherPredicate
53+
: otherPredicate == null ? thisPredicate : compositionType.combine(builder, thisPredicate, otherPredicate);
54+
};
55+
}
56+
}

src/main/java/org/springframework/data/jpa/domain/Specifications.java

-153
This file was deleted.

0 commit comments

Comments
 (0)