Skip to content

Commit 20ed7fb

Browse files
christophstroblmp911de
authored andcommitted
Add contract annotations.
See #618 Original pull request: #625
1 parent 27e2d9b commit 20ed7fb

File tree

7 files changed

+50
-12
lines changed

7 files changed

+50
-12
lines changed

src/main/java/org/springframework/data/keyvalue/core/IterableConverter.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23+
import org.jspecify.annotations.Nullable;
24+
import org.springframework.lang.Contract;
25+
2326
/**
2427
* Converter capable of transforming a given {@link Iterable} into a collection type.
2528
*
@@ -36,7 +39,12 @@ private IterableConverter() {}
3639
* @param source
3740
* @return {@link Collections#emptyList()} when source is {@literal null}.
3841
*/
39-
public static <T> List<T> toList(Iterable<T> source) {
42+
@Contract("_ -> !null")
43+
public static <T> List<T> toList(@Nullable Iterable<T> source) {
44+
45+
if(source == null) {
46+
return Collections.emptyList();
47+
}
4048

4149
if (source instanceof List) {
4250
return (List<T>) source;

src/main/java/org/springframework/data/keyvalue/core/PredicateQueryEngine.java

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.jspecify.annotations.Nullable;
2626
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
27+
import org.springframework.lang.Contract;
2728

2829
/**
2930
* {@link QueryEngine} implementation specific for executing {@link Predicate} based {@link KeyValueQuery} against
@@ -78,6 +79,7 @@ private List<?> sortAndFilterMatchingRange(Iterable<?> source, @Nullable Predica
7879
return filterMatchingRange(tmp, criteria, offset, rows);
7980
}
8081

82+
@Contract("!null, _, _, _ -> !null")
8183
private static <S> List<S> filterMatchingRange(List<S> source, @Nullable Predicate<?> criteria, long offset, int rows) {
8284

8385
Stream<S> stream = source.stream();

src/main/java/org/springframework/data/keyvalue/core/PropertyPathComparator.java

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.jspecify.annotations.Nullable;
2323
import org.springframework.data.mapping.PropertyPath;
24+
import org.springframework.lang.Contract;
2425

2526
/**
2627
* {@link Comparator} implementation to compare objects based on a {@link PropertyPath}. This comparator obtains the
@@ -81,6 +82,7 @@ public int compare(@Nullable T o1, @Nullable T o2) {
8182
*
8283
* @return
8384
*/
85+
@Contract("-> this")
8486
public PropertyPathComparator<@Nullable T> asc() {
8587
this.asc = true;
8688
return this;
@@ -91,6 +93,7 @@ public int compare(@Nullable T o1, @Nullable T o2) {
9193
*
9294
* @return
9395
*/
96+
@Contract("-> this")
9497
public PropertyPathComparator<@Nullable T> desc() {
9598
this.asc = false;
9699
return this;
@@ -101,6 +104,7 @@ public int compare(@Nullable T o1, @Nullable T o2) {
101104
*
102105
* @return
103106
*/
107+
@Contract("-> this")
104108
public PropertyPathComparator<@Nullable T> nullsFirst() {
105109
this.nullsFirst = true;
106110
return this;
@@ -111,6 +115,7 @@ public int compare(@Nullable T o1, @Nullable T o2) {
111115
*
112116
* @return
113117
*/
118+
@Contract("-> this")
114119
public PropertyPathComparator<@Nullable T> nullsLast() {
115120
this.nullsFirst = false;
116121
return this;

src/main/java/org/springframework/data/keyvalue/core/SpelPropertyComparator.java

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.expression.spel.standard.SpelExpression;
2222
import org.springframework.expression.spel.standard.SpelExpressionParser;
2323
import org.springframework.expression.spel.support.SimpleEvaluationContext;
24+
import org.springframework.lang.Contract;
2425
import org.springframework.util.Assert;
2526

2627
/**
@@ -63,6 +64,7 @@ public SpelPropertyComparator(String path, SpelExpressionParser parser) {
6364
*
6465
* @return
6566
*/
67+
@Contract("-> this")
6668
public SpelPropertyComparator<@Nullable T> asc() {
6769
this.asc = true;
6870
return this;
@@ -73,6 +75,7 @@ public SpelPropertyComparator(String path, SpelExpressionParser parser) {
7375
*
7476
* @return
7577
*/
78+
@Contract("-> this")
7679
public SpelPropertyComparator<@Nullable T> desc() {
7780
this.asc = false;
7881
return this;
@@ -83,6 +86,7 @@ public SpelPropertyComparator(String path, SpelExpressionParser parser) {
8386
*
8487
* @return
8588
*/
89+
@Contract("-> this")
8690
public SpelPropertyComparator<@Nullable T> nullsFirst() {
8791
this.nullsFirst = true;
8892
return this;
@@ -93,6 +97,7 @@ public SpelPropertyComparator(String path, SpelExpressionParser parser) {
9397
*
9498
* @return
9599
*/
100+
@Contract("-> this")
96101
public SpelPropertyComparator<@Nullable T> nullsLast() {
97102
this.nullsFirst = false;
98103
return this;

src/main/java/org/springframework/data/keyvalue/core/query/KeyValueQuery.java

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.jspecify.annotations.Nullable;
1919
import org.springframework.data.domain.Sort;
20+
import org.springframework.lang.Contract;
2021
import org.springframework.util.Assert;
2122

2223
/**
@@ -143,6 +144,7 @@ public void setSort(Sort sort) {
143144
* @param sort must not be {@literal null}.
144145
* @return
145146
*/
147+
@Contract("_ -> this")
146148
public KeyValueQuery<T> orderBy(Sort sort) {
147149

148150
Assert.notNull(sort, "Sort must not be null");
@@ -161,6 +163,7 @@ public KeyValueQuery<T> orderBy(Sort sort) {
161163
* @param offset
162164
* @return
163165
*/
166+
@Contract("_ -> this")
164167
public KeyValueQuery<T> skip(long offset) {
165168

166169
setOffset(offset);
@@ -173,6 +176,7 @@ public KeyValueQuery<T> skip(long offset) {
173176
* @param rows
174177
* @return
175178
*/
179+
@Contract("_ -> this")
176180
public KeyValueQuery<T> limit(int rows) {
177181
setRows(rows);
178182
return this;

src/main/java/org/springframework/data/keyvalue/repository/query/PredicateQueryCreator.java

+23-11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.repository.query.parser.Part;
3636
import org.springframework.data.repository.query.parser.Part.IgnoreCaseType;
3737
import org.springframework.data.repository.query.parser.PartTree;
38+
import org.springframework.lang.Contract;
3839
import org.springframework.util.ObjectUtils;
3940

4041
/**
@@ -124,7 +125,8 @@ public Predicate<Object> isFalse() {
124125
return new ValueComparingPredicate(part.getProperty(), false);
125126
}
126127

127-
public Predicate<Object> isEqualTo(Object value) {
128+
@Contract("_ -> new")
129+
public Predicate<Object> isEqualTo(@Nullable Object value) {
128130
return new ValueComparingPredicate(part.getProperty(), o -> {
129131

130132
if (!ObjectUtils.nullSafeEquals(IgnoreCaseType.NEVER, part.shouldIgnoreCase())) {
@@ -145,22 +147,27 @@ public Predicate<Object> isNotNull() {
145147
return isNull().negate();
146148
}
147149

148-
public Predicate<Object> isLessThan(Object value) {
150+
@Contract("_ -> new")
151+
public Predicate<Object> isLessThan(@Nullable Object value) {
149152
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) < 0);
150153
}
151154

152-
public Predicate<Object> isLessThanEqual(Object value) {
155+
@Contract("_ -> new")
156+
public Predicate<Object> isLessThanEqual(@Nullable Object value) {
153157
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) <= 0);
154158
}
155159

156-
public Predicate<Object> isGreaterThan(Object value) {
160+
@Contract("_ -> new")
161+
public Predicate<Object> isGreaterThan(@Nullable Object value) {
157162
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) > 0);
158163
}
159164

160-
public Predicate<Object> isGreaterThanEqual(Object value) {
165+
@Contract("_ -> new")
166+
public Predicate<Object> isGreaterThanEqual(@Nullable Object value) {
161167
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) >= 0);
162168
}
163169

170+
@Contract("!null -> new")
164171
public Predicate<Object> matches(Pattern pattern) {
165172

166173
return new ValueComparingPredicate(part.getProperty(), o -> {
@@ -172,7 +179,8 @@ public Predicate<Object> matches(Pattern pattern) {
172179
});
173180
}
174181

175-
public Predicate<Object> matches(Object value) {
182+
@Contract("_ -> new")
183+
public Predicate<Object> matches(@Nullable Object value) {
176184
return new ValueComparingPredicate(part.getProperty(), o -> {
177185

178186
if (o == null || value == null) {
@@ -188,10 +196,12 @@ public Predicate<Object> matches(Object value) {
188196
});
189197
}
190198

199+
@Contract("!null -> new")
191200
public Predicate<Object> matches(String regex) {
192201
return matches(Pattern.compile(regex));
193202
}
194203

204+
@Contract("!null -> new")
195205
public Predicate<Object> in(Object value) {
196206
return new ValueComparingPredicate(part.getProperty(), o -> {
197207

@@ -207,11 +217,11 @@ public Predicate<Object> in(Object value) {
207217
return ObjectUtils.containsElement(ObjectUtils.toObjectArray(value), value);
208218
}
209219
return false;
210-
211220
});
212221
}
213222

214-
public Predicate<Object> contains(Object value) {
223+
@Contract("_ -> new")
224+
public Predicate<Object> contains(@Nullable Object value) {
215225

216226
return new ValueComparingPredicate(part.getProperty(), o -> {
217227

@@ -245,6 +255,7 @@ public Predicate<Object> contains(Object value) {
245255
});
246256
}
247257

258+
@Contract("!null -> new")
248259
public Predicate<Object> startsWith(Object value) {
249260
return new ValueComparingPredicate(part.getProperty(), o -> {
250261

@@ -261,6 +272,7 @@ public Predicate<Object> startsWith(Object value) {
261272

262273
}
263274

275+
@Contract("!null -> new")
264276
public Predicate<Object> endsWith(Object value) {
265277

266278
return new ValueComparingPredicate(part.getProperty(), o -> {
@@ -281,13 +293,13 @@ public Predicate<Object> endsWith(Object value) {
281293
static class ValueComparingPredicate implements Predicate<Object> {
282294

283295
private final PropertyPath path;
284-
private final Function<Object, Boolean> check;
296+
private final Function<@Nullable Object, Boolean> check;
285297

286-
public ValueComparingPredicate(PropertyPath path, Object expected) {
298+
public ValueComparingPredicate(PropertyPath path, @Nullable Object expected) {
287299
this(path, (value) -> ObjectUtils.nullSafeEquals(value, expected));
288300
}
289301

290-
public ValueComparingPredicate(PropertyPath path, Function<Object, Boolean> check) {
302+
public ValueComparingPredicate(PropertyPath path, Function<@Nullable Object, Boolean> check) {
291303
this.path = path;
292304
this.check = check;
293305
}

src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueQuerydslUtils.java

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.domain.Sort.Order;
2323
import org.springframework.data.mapping.PropertyPath;
2424
import org.springframework.data.querydsl.QSort;
25+
import org.springframework.lang.Contract;
2526
import org.springframework.util.Assert;
2627

2728
import com.querydsl.core.types.Expression;
@@ -52,6 +53,7 @@ private KeyValueQuerydslUtils() {
5253
* @param builder must not be {@literal null}.
5354
* @return empty {@code OrderSpecifier<?>[]} when sort is {@literal null}.
5455
*/
56+
@Contract("!null, !null -> new")
5557
static OrderSpecifier<?>[] toOrderSpecifier(Sort sort, PathBuilder<?> builder) {
5658

5759
Assert.notNull(sort, "Sort must not be null");

0 commit comments

Comments
 (0)