Skip to content

Commit 28a52e0

Browse files
committed
Add Contract annotations.
Original Pull Request: #3578
1 parent 5fafaa4 commit 28a52e0

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

Diff for: spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/DeleteSpecification.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424
import java.util.Arrays;
2525
import java.util.stream.StreamSupport;
2626

27+
import org.springframework.lang.CheckReturnValue;
28+
import org.springframework.lang.Contract;
2729
import org.springframework.lang.Nullable;
2830
import org.springframework.util.Assert;
2931

3032
/**
3133
* Specification in the sense of Domain Driven Design to handle Criteria Deletes.
3234
*
3335
* @author Mark Paluch
34-
* @since xxx
36+
* @since 4.0
3537
*/
3638
@FunctionalInterface
3739
public interface DeleteSpecification<T> extends Serializable {
@@ -81,6 +83,8 @@ static <T> DeleteSpecification<T> where(PredicateSpecification<T> spec) {
8183
* @param other the other {@link DeleteSpecification}.
8284
* @return the conjunction of the specifications.
8385
*/
86+
@Contract("_ -> new")
87+
@CheckReturnValue
8488
default DeleteSpecification<T> and(DeleteSpecification<T> other) {
8589

8690
Assert.notNull(other, "Other specification must not be null");
@@ -94,6 +98,8 @@ default DeleteSpecification<T> and(DeleteSpecification<T> other) {
9498
* @param other the other {@link PredicateSpecification}.
9599
* @return the conjunction of the specifications.
96100
*/
101+
@Contract("_ -> new")
102+
@CheckReturnValue
97103
default DeleteSpecification<T> and(PredicateSpecification<T> other) {
98104

99105
Assert.notNull(other, "Other specification must not be null");
@@ -107,6 +113,8 @@ default DeleteSpecification<T> and(PredicateSpecification<T> other) {
107113
* @param other the other {@link DeleteSpecification}.
108114
* @return the disjunction of the specifications.
109115
*/
116+
@Contract("_ -> new")
117+
@CheckReturnValue
110118
default DeleteSpecification<T> or(DeleteSpecification<T> other) {
111119

112120
Assert.notNull(other, "Other specification must not be null");
@@ -120,6 +128,8 @@ default DeleteSpecification<T> or(DeleteSpecification<T> other) {
120128
* @param other the other {@link PredicateSpecification}.
121129
* @return the disjunction of the specifications.
122130
*/
131+
@Contract("_ -> new")
132+
@CheckReturnValue
123133
default DeleteSpecification<T> or(PredicateSpecification<T> other) {
124134

125135
Assert.notNull(other, "Other specification must not be null");

Diff for: spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/PredicateSpecification.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@
2323
import java.util.Arrays;
2424
import java.util.stream.StreamSupport;
2525

26+
import org.springframework.lang.CheckReturnValue;
27+
import org.springframework.lang.Contract;
2628
import org.springframework.lang.Nullable;
2729
import org.springframework.util.Assert;
2830

2931
/**
3032
* Specification in the sense of Domain Driven Design.
3133
*
3234
* @author Mark Paluch
33-
* @since xxx
35+
* @since 4.0
3436
*/
3537
public interface PredicateSpecification<T> extends Serializable {
3638

@@ -54,7 +56,7 @@ static <T> PredicateSpecification<T> all() {
5456
*/
5557
static <T> PredicateSpecification<T> where(PredicateSpecification<T> spec) {
5658

57-
Assert.notNull(spec, "DeleteSpecification must not be null");
59+
Assert.notNull(spec, "PredicateSpecification must not be null");
5860

5961
return spec;
6062
}
@@ -65,6 +67,8 @@ static <T> PredicateSpecification<T> where(PredicateSpecification<T> spec) {
6567
* @param other the other {@link PredicateSpecification}.
6668
* @return the conjunction of the specifications.
6769
*/
70+
@Contract("_ -> new")
71+
@CheckReturnValue
6872
default PredicateSpecification<T> and(PredicateSpecification<T> other) {
6973

7074
Assert.notNull(other, "Other specification must not be null");
@@ -78,6 +82,8 @@ default PredicateSpecification<T> and(PredicateSpecification<T> other) {
7882
* @param other the other {@link PredicateSpecification}.
7983
* @return the disjunction of the specifications.
8084
*/
85+
@Contract("_ -> new")
86+
@CheckReturnValue
8187
default PredicateSpecification<T> or(PredicateSpecification<T> other) {
8288

8389
Assert.notNull(other, "Other specification must not be null");

Diff for: spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/Specification.java

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.Arrays;
2626
import java.util.stream.StreamSupport;
2727

28+
import org.springframework.lang.CheckReturnValue;
29+
import org.springframework.lang.Contract;
2830
import org.springframework.lang.Nullable;
2931
import org.springframework.util.Assert;
3032

@@ -75,6 +77,8 @@ static <T> Specification<T> where(PredicateSpecification<T> spec) {
7577
* @return the conjunction of the specifications.
7678
* @since 2.0
7779
*/
80+
@Contract("_ -> new")
81+
@CheckReturnValue
7882
default Specification<T> and(Specification<T> other) {
7983

8084
Assert.notNull(other, "Other specification must not be null");
@@ -89,6 +93,8 @@ default Specification<T> and(Specification<T> other) {
8993
* @return the conjunction of the specifications.
9094
* @since 2.0
9195
*/
96+
@Contract("_ -> new")
97+
@CheckReturnValue
9298
default Specification<T> and(PredicateSpecification<T> other) {
9399

94100
Assert.notNull(other, "Other specification must not be null");
@@ -103,6 +109,8 @@ default Specification<T> and(PredicateSpecification<T> other) {
103109
* @return the disjunction of the specifications
104110
* @since 2.0
105111
*/
112+
@Contract("_ -> new")
113+
@CheckReturnValue
106114
default Specification<T> or(Specification<T> other) {
107115

108116
Assert.notNull(other, "Other specification must not be null");
@@ -117,6 +125,8 @@ default Specification<T> or(Specification<T> other) {
117125
* @return the disjunction of the specifications
118126
* @since 2.0
119127
*/
128+
@Contract("_ -> new")
129+
@CheckReturnValue
120130
default Specification<T> or(PredicateSpecification<T> other) {
121131

122132
Assert.notNull(other, "Other specification must not be null");

Diff for: spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/UpdateSpecification.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@
2424
import java.util.Arrays;
2525
import java.util.stream.StreamSupport;
2626

27+
import org.springframework.lang.CheckReturnValue;
28+
import org.springframework.lang.Contract;
2729
import org.springframework.lang.Nullable;
2830
import org.springframework.util.Assert;
2931

3032
/**
3133
* Specification in the sense of Domain Driven Design to handle Criteria Updates.
3234
*
3335
* @author Mark Paluch
34-
* @since xxx
36+
* @since 4.0
3537
*/
3638
@FunctionalInterface
3739
public interface UpdateSpecification<T> extends Serializable {
@@ -103,6 +105,8 @@ static <T> UpdateSpecification<T> where(PredicateSpecification<T> spec) {
103105
* @param other the other {@link UpdateSpecification}.
104106
* @return the conjunction of the specifications.
105107
*/
108+
@Contract("_ -> new")
109+
@CheckReturnValue
106110
default UpdateSpecification<T> and(UpdateSpecification<T> other) {
107111

108112
Assert.notNull(other, "Other specification must not be null");
@@ -116,6 +120,8 @@ default UpdateSpecification<T> and(UpdateSpecification<T> other) {
116120
* @param other the other {@link PredicateSpecification}.
117121
* @return the conjunction of the specifications.
118122
*/
123+
@Contract("_ -> new")
124+
@CheckReturnValue
119125
default UpdateSpecification<T> and(PredicateSpecification<T> other) {
120126

121127
Assert.notNull(other, "Other specification must not be null");
@@ -129,6 +135,8 @@ default UpdateSpecification<T> and(PredicateSpecification<T> other) {
129135
* @param other the other {@link UpdateSpecification}.
130136
* @return the disjunction of the specifications.
131137
*/
138+
@Contract("_ -> new")
139+
@CheckReturnValue
132140
default UpdateSpecification<T> or(UpdateSpecification<T> other) {
133141

134142
Assert.notNull(other, "Other specification must not be null");
@@ -142,6 +150,8 @@ default UpdateSpecification<T> or(UpdateSpecification<T> other) {
142150
* @param other the other {@link PredicateSpecification}.
143151
* @return the disjunction of the specifications.
144152
*/
153+
@Contract("_ -> new")
154+
@CheckReturnValue
145155
default UpdateSpecification<T> or(PredicateSpecification<T> other) {
146156

147157
Assert.notNull(other, "Other specification must not be null");
@@ -256,6 +266,8 @@ interface UpdateOperation<T> {
256266
* @param other the other {@link UpdateOperation}.
257267
* @return the conjunction of the specifications.
258268
*/
269+
@Contract("_ -> new")
270+
@CheckReturnValue
259271
default UpdateOperation<T> and(UpdateOperation<T> other) {
260272

261273
Assert.notNull(other, "Other UpdateOperation must not be null");
@@ -272,6 +284,8 @@ default UpdateOperation<T> and(UpdateOperation<T> other) {
272284
* @param specification the {@link PredicateSpecification}.
273285
* @return the conjunction of the specifications.
274286
*/
287+
@Contract("_ -> new")
288+
@CheckReturnValue
275289
default UpdateSpecification<T> where(PredicateSpecification<T> specification) {
276290

277291
Assert.notNull(specification, "PredicateSpecification must not be null");
@@ -288,6 +302,8 @@ default UpdateSpecification<T> where(PredicateSpecification<T> specification) {
288302
* @param specification the {@link UpdateSpecification}.
289303
* @return the conjunction of the specifications.
290304
*/
305+
@Contract("_ -> new")
306+
@CheckReturnValue
291307
default UpdateSpecification<T> where(UpdateSpecification<T> specification) {
292308

293309
Assert.notNull(specification, "UpdateSpecification must not be null");
@@ -306,6 +322,7 @@ default UpdateSpecification<T> where(UpdateSpecification<T> specification) {
306322
* @param criteriaBuilder must not be {@literal null}.
307323
*/
308324
void apply(Root<T> root, CriteriaUpdate<T> update, CriteriaBuilder criteriaBuilder);
325+
309326
}
310327

311328
}

Diff for: spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
260260
/*
261261
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
262262
*/
263-
Collection<ID> idCollection = toCollection(ids);
263+
Collection<ID> idCollection = toCollection(ids);
264264
query.setParameter("ids", idCollection);
265265

266266
applyQueryHints(query);
@@ -732,8 +732,7 @@ protected TypedQuery<T> getQuery(Specification<T> spec, Pageable pageable) {
732732
* @param domainClass must not be {@literal null}.
733733
* @param pageable must not be {@literal null}.
734734
*/
735-
protected <S extends T> TypedQuery<S> getQuery(Specification<S> spec, Class<S> domainClass,
736-
Pageable pageable) {
735+
protected <S extends T> TypedQuery<S> getQuery(Specification<S> spec, Class<S> domainClass, Pageable pageable) {
737736

738737
return getQuery(spec, domainClass, pageable.getSort());
739738
}
@@ -1074,7 +1073,7 @@ private static long executeCountQuery(TypedQuery<Long> query) {
10741073
@SuppressWarnings("rawtypes")
10751074
private static final class ByIdsSpecification<T> implements Specification<T> {
10761075

1077-
@Serial private static final long serialVersionUID = 1L;
1076+
@Serial private static final @Serial long serialVersionUID = 1L;
10781077

10791078
private final JpaEntityInformation<T, ?> entityInformation;
10801079

@@ -1085,6 +1084,7 @@ private static final class ByIdsSpecification<T> implements Specification<T> {
10851084
}
10861085

10871086
@Override
1087+
@SuppressWarnings("unchecked")
10881088
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
10891089

10901090
Path<?> path = root.get(entityInformation.getIdAttribute());
@@ -1103,7 +1103,7 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
11031103
*/
11041104
private static class ExampleSpecification<T> implements Specification<T> {
11051105

1106-
@Serial private static final long serialVersionUID = 1L;
1106+
@Serial private static final @Serial long serialVersionUID = 1L;
11071107

11081108
private final Example<T> example;
11091109
private final EscapeCharacter escapeCharacter;

0 commit comments

Comments
 (0)