Skip to content

Commit 12d99f4

Browse files
committed
Add Contract annotations.
1 parent b0ffbcd commit 12d99f4

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
@@ -32,6 +32,7 @@
3232
import jakarta.persistence.criteria.Predicate;
3333
import jakarta.persistence.criteria.Root;
3434

35+
import java.io.Serial;
3536
import java.util.ArrayList;
3637
import java.util.Collection;
3738
import java.util.Collections;
@@ -246,7 +247,6 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
246247
/*
247248
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
248249
*/
249-
250250
if (Collection.class.isInstance(ids)) {
251251
query.setParameter("ids", ids);
252252
} else {
@@ -731,8 +731,7 @@ protected TypedQuery<T> getQuery(Specification<T> spec, Pageable pageable) {
731731
* @param domainClass must not be {@literal null}.
732732
* @param pageable must not be {@literal null}.
733733
*/
734-
protected <S extends T> TypedQuery<S> getQuery(Specification<S> spec, Class<S> domainClass,
735-
Pageable pageable) {
734+
protected <S extends T> TypedQuery<S> getQuery(Specification<S> spec, Class<S> domainClass, Pageable pageable) {
736735

737736
return getQuery(spec, domainClass, pageable.getSort());
738737
}
@@ -1019,7 +1018,7 @@ private static long executeCountQuery(TypedQuery<Long> query) {
10191018
@SuppressWarnings("rawtypes")
10201019
private static final class ByIdsSpecification<T> implements Specification<T> {
10211020

1022-
private static final long serialVersionUID = 1L;
1021+
private static final @Serial long serialVersionUID = 1L;
10231022

10241023
private final JpaEntityInformation<T, ?> entityInformation;
10251024

@@ -1030,6 +1029,7 @@ private static final class ByIdsSpecification<T> implements Specification<T> {
10301029
}
10311030

10321031
@Override
1032+
@SuppressWarnings("unchecked")
10331033
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
10341034

10351035
Path<?> path = root.get(entityInformation.getIdAttribute());
@@ -1048,7 +1048,7 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
10481048
*/
10491049
private static class ExampleSpecification<T> implements Specification<T> {
10501050

1051-
private static final long serialVersionUID = 1L;
1051+
private static final @Serial long serialVersionUID = 1L;
10521052

10531053
private final Example<T> example;
10541054
private final EscapeCharacter escapeCharacter;

0 commit comments

Comments
 (0)