Skip to content

Commit e2c7be2

Browse files
committed
Move off deprecated NullSafeComparator.
Closes #610
1 parent 2ecedfb commit e2c7be2

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

src/main/java/org/springframework/data/keyvalue/aot/KeyValueRuntimeHints.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
4444
TypeReference.of(KeyValuePartTreeQuery.class)),
4545
hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS));
4646

47-
hints.reflection().registerType(org.springframework.util.comparator.NullSafeComparator.class,
47+
hints.reflection().registerType(TypeReference.of("java.util.Comparators.NaturalOrderComparator"),
48+
builder -> builder.withMethod("compare",
49+
List.of(TypeReference.of(Object.class), TypeReference.of(Object.class)), ExecutableMode.INVOKE));
50+
51+
hints.reflection().registerType(TypeReference.of("java.util.Comparators.NullComparator"),
4852
builder -> builder.withMethod("compare",
4953
List.of(TypeReference.of(Object.class), TypeReference.of(Object.class)), ExecutableMode.INVOKE));
5054
}

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

+22-8
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,35 @@
2020
import java.util.Map;
2121

2222
import org.springframework.data.mapping.PropertyPath;
23-
import org.springframework.data.util.Lazy;
24-
import org.springframework.util.comparator.NullSafeComparator;
23+
import org.springframework.lang.Nullable;
2524

2625
/**
26+
* {@link Comparator} implementation to compare objects based on a {@link PropertyPath}. This comparator obtains the
27+
* value at {@link PropertyPath} from the {@link #compare(Object, Object) given comparison objects} and then performs
28+
* the comparison.
29+
*
2730
* @author Christoph Strobl
31+
* @author Mark Paluch
2832
* @since 3.1.10
2933
*/
3034
public class PropertyPathComparator<T> implements Comparator<T> {
3135

36+
private static final Comparator<?> NULLS_FIRST = Comparator.nullsFirst(Comparator.naturalOrder());
37+
private static final Comparator<?> NULLS_LAST = Comparator.nullsLast(Comparator.naturalOrder());
38+
3239
private final String path;
3340

3441
private boolean asc = true;
3542
private boolean nullsFirst = true;
3643

3744
private final Map<Class<?>, PropertyPath> pathCache = new HashMap<>(2);
38-
private Lazy<Comparator<Object>> comparator = Lazy
39-
.of(() -> new NullSafeComparator(Comparator.naturalOrder(), this.nullsFirst));
4045

4146
public PropertyPathComparator(String path) {
4247
this.path = path;
4348
}
4449

4550
@Override
46-
public int compare(T o1, T o2) {
51+
public int compare(@Nullable T o1, @Nullable T o2) {
4752

4853
if (o1 == null && o2 == null) {
4954
return 0;
@@ -56,10 +61,19 @@ public int compare(T o1, T o2) {
5661
}
5762

5863
PropertyPath propertyPath = pathCache.computeIfAbsent(o1.getClass(), it -> PropertyPath.from(path, it));
59-
Object value1 = new SimplePropertyPathAccessor<>(o1).getValue(propertyPath);
60-
Object value2 = new SimplePropertyPathAccessor<>(o2).getValue(propertyPath);
64+
Object value1 = getCompareValue(o1, propertyPath);
65+
Object value2 = getCompareValue(o2, propertyPath);
66+
67+
return getComparator().compare(value1, value2) * (asc ? 1 : -1);
68+
}
69+
70+
protected <T> Object getCompareValue(T object, PropertyPath propertyPath) {
71+
return new SimplePropertyPathAccessor<>(object).getValue(propertyPath);
72+
}
6173

62-
return comparator.get().compare(value1, value2) * (asc ? 1 : -1);
74+
@SuppressWarnings("unchecked")
75+
private Comparator<Object> getComparator() {
76+
return (Comparator<Object>) (nullsFirst ? NULLS_FIRST : NULLS_LAST);
6377
}
6478

6579
/**

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.expression.spel.support.SimpleEvaluationContext;
2323
import org.springframework.lang.Nullable;
2424
import org.springframework.util.Assert;
25-
import org.springframework.util.comparator.NullSafeComparator;
2625

2726
/**
2827
* {@link Comparator} implementation using {@link SpelExpression}.
@@ -34,6 +33,9 @@
3433
*/
3534
public class SpelPropertyComparator<T> implements Comparator<T> {
3635

36+
private static final Comparator<?> NULLS_FIRST = Comparator.nullsFirst(Comparator.naturalOrder());
37+
private static final Comparator<?> NULLS_LAST = Comparator.nullsLast(Comparator.naturalOrder());
38+
3739
private final String path;
3840
private final SpelExpressionParser parser;
3941

@@ -129,7 +131,7 @@ public int compare(T arg1, T arg2) {
129131
SpelExpression expressionToUse = getExpression();
130132

131133
SimpleEvaluationContext ctx = SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().build();
132-
ctx.setVariable("comparator", new NullSafeComparator(Comparator.naturalOrder(), this.nullsFirst));
134+
ctx.setVariable("comparator", nullsFirst ? NULLS_FIRST : NULLS_LAST);
133135
ctx.setVariable("arg1", arg1);
134136
ctx.setVariable("arg2", arg2);
135137

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.keyvalue.repository.query;
1717

1818
import java.util.Collection;
19+
import java.util.Comparator;
1920
import java.util.Iterator;
2021
import java.util.Map;
2122
import java.util.Objects;
@@ -35,7 +36,6 @@
3536
import org.springframework.data.repository.query.parser.PartTree;
3637
import org.springframework.lang.Nullable;
3738
import org.springframework.util.ObjectUtils;
38-
import org.springframework.util.comparator.NullSafeComparator;
3939

4040
/**
4141
* {@link AbstractQueryCreator} to create {@link Predicate}-based {@link KeyValueQuery}s.
@@ -45,6 +45,8 @@
4545
*/
4646
public class PredicateQueryCreator extends AbstractQueryCreator<KeyValueQuery<Predicate<?>>, Predicate<?>> {
4747

48+
private static final Comparator<?> COMPARATOR = Comparator.nullsFirst(Comparator.naturalOrder());
49+
4850
public PredicateQueryCreator(PartTree tree, ParameterAccessor parameters) {
4951
super(tree, parameters);
5052
}
@@ -118,6 +120,10 @@ public PredicateBuilder(Part part) {
118120
this.part = part;
119121
}
120122

123+
static <T> Comparator<T> comparator() {
124+
return (Comparator<T>) COMPARATOR;
125+
}
126+
121127
static PredicateBuilder propertyValueOf(Part part) {
122128
return new PredicateBuilder(part);
123129
}
@@ -152,23 +158,19 @@ public Predicate<Object> isNotNull() {
152158
}
153159

154160
public Predicate<Object> isLessThan(Object value) {
155-
return new ValueComparingPredicate(part.getProperty(),
156-
o -> NullSafeComparator.NULLS_HIGH.compare(o, value) == -1 ? true : false);
161+
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) == -1 ? true : false);
157162
}
158163

159164
public Predicate<Object> isLessThanEqual(Object value) {
160-
return new ValueComparingPredicate(part.getProperty(),
161-
o -> NullSafeComparator.NULLS_HIGH.compare(o, value) <= 0 ? true : false);
165+
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) <= 0 ? true : false);
162166
}
163167

164168
public Predicate<Object> isGreaterThan(Object value) {
165-
return new ValueComparingPredicate(part.getProperty(),
166-
o -> NullSafeComparator.NULLS_HIGH.compare(o, value) == 1 ? true : false);
169+
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) == 1 ? true : false);
167170
}
168171

169172
public Predicate<Object> isGreaterThanEqual(Object value) {
170-
return new ValueComparingPredicate(part.getProperty(),
171-
o -> NullSafeComparator.NULLS_HIGH.compare(o, value) >= 0 ? true : false);
173+
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) >= 0 ? true : false);
172174
}
173175

174176
public Predicate<Object> matches(Pattern pattern) {
@@ -271,6 +273,7 @@ public Predicate<Object> startsWith(Object value) {
271273
}
272274

273275
public Predicate<Object> endsWith(Object value) {
276+
274277
return new ValueComparingPredicate(part.getProperty(), o -> {
275278

276279
if (!(o instanceof String s)) {
@@ -283,7 +286,6 @@ public Predicate<Object> endsWith(Object value) {
283286

284287
return s.toLowerCase().endsWith(value.toString().toLowerCase());
285288
});
286-
287289
}
288290
}
289291

0 commit comments

Comments
 (0)