@@ -44,7 +44,11 @@ class KeyValueRuntimeHints implements RuntimeHintsRegistrar {
|
||||
TypeReference.of(KeyValuePartTreeQuery.class)),
|
||||
hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS));
|
||||
|
||||
hints.reflection().registerType(org.springframework.util.comparator.NullSafeComparator.class,
|
||||
hints.reflection().registerType(TypeReference.of("java.util.Comparators.NaturalOrderComparator"),
|
||||
builder -> builder.withMethod("compare",
|
||||
List.of(TypeReference.of(Object.class), TypeReference.of(Object.class)), ExecutableMode.INVOKE));
|
||||
|
||||
hints.reflection().registerType(TypeReference.of("java.util.Comparators.NullComparator"),
|
||||
builder -> builder.withMethod("compare",
|
||||
List.of(TypeReference.of(Object.class), TypeReference.of(Object.class)), ExecutableMode.INVOKE));
|
||||
}
|
||||
|
||||
@@ -20,30 +20,35 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
import org.springframework.data.util.Lazy;
|
||||
import org.springframework.util.comparator.NullSafeComparator;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* {@link Comparator} implementation to compare objects based on a {@link PropertyPath}. This comparator obtains the
|
||||
* value at {@link PropertyPath} from the {@link #compare(Object, Object) given comparison objects} and then performs
|
||||
* the comparison.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @since 3.1.10
|
||||
*/
|
||||
public class PropertyPathComparator<T> implements Comparator<T> {
|
||||
|
||||
private static final Comparator<?> NULLS_FIRST = Comparator.nullsFirst(Comparator.naturalOrder());
|
||||
private static final Comparator<?> NULLS_LAST = Comparator.nullsLast(Comparator.naturalOrder());
|
||||
|
||||
private final String path;
|
||||
|
||||
private boolean asc = true;
|
||||
private boolean nullsFirst = true;
|
||||
|
||||
private final Map<Class<?>, PropertyPath> pathCache = new HashMap<>(2);
|
||||
private Lazy<Comparator<Object>> comparator = Lazy
|
||||
.of(() -> new NullSafeComparator(Comparator.naturalOrder(), this.nullsFirst));
|
||||
|
||||
public PropertyPathComparator(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(T o1, T o2) {
|
||||
public int compare(@Nullable T o1, @Nullable T o2) {
|
||||
|
||||
if (o1 == null && o2 == null) {
|
||||
return 0;
|
||||
@@ -56,10 +61,19 @@ public class PropertyPathComparator<T> implements Comparator<T> {
|
||||
}
|
||||
|
||||
PropertyPath propertyPath = pathCache.computeIfAbsent(o1.getClass(), it -> PropertyPath.from(path, it));
|
||||
Object value1 = new SimplePropertyPathAccessor<>(o1).getValue(propertyPath);
|
||||
Object value2 = new SimplePropertyPathAccessor<>(o2).getValue(propertyPath);
|
||||
Object value1 = getCompareValue(o1, propertyPath);
|
||||
Object value2 = getCompareValue(o2, propertyPath);
|
||||
|
||||
return comparator.get().compare(value1, value2) * (asc ? 1 : -1);
|
||||
return getComparator().compare(value1, value2) * (asc ? 1 : -1);
|
||||
}
|
||||
|
||||
protected <T> Object getCompareValue(T object, PropertyPath propertyPath) {
|
||||
return new SimplePropertyPathAccessor<>(object).getValue(propertyPath);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Comparator<Object> getComparator() {
|
||||
return (Comparator<Object>) (nullsFirst ? NULLS_FIRST : NULLS_LAST);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.SimpleEvaluationContext;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.comparator.NullSafeComparator;
|
||||
|
||||
/**
|
||||
* {@link Comparator} implementation using {@link SpelExpression}.
|
||||
@@ -34,6 +33,9 @@ import org.springframework.util.comparator.NullSafeComparator;
|
||||
*/
|
||||
public class SpelPropertyComparator<T> implements Comparator<T> {
|
||||
|
||||
private static final Comparator<?> NULLS_FIRST = Comparator.nullsFirst(Comparator.naturalOrder());
|
||||
private static final Comparator<?> NULLS_LAST = Comparator.nullsLast(Comparator.naturalOrder());
|
||||
|
||||
private final String path;
|
||||
private final SpelExpressionParser parser;
|
||||
|
||||
@@ -129,7 +131,7 @@ public class SpelPropertyComparator<T> implements Comparator<T> {
|
||||
SpelExpression expressionToUse = getExpression();
|
||||
|
||||
SimpleEvaluationContext ctx = SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().build();
|
||||
ctx.setVariable("comparator", new NullSafeComparator(Comparator.naturalOrder(), this.nullsFirst));
|
||||
ctx.setVariable("comparator", nullsFirst ? NULLS_FIRST : NULLS_LAST);
|
||||
ctx.setVariable("arg1", arg1);
|
||||
ctx.setVariable("arg2", arg2);
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.keyvalue.repository.query;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -35,7 +36,6 @@ import org.springframework.data.repository.query.parser.Part.IgnoreCaseType;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.comparator.NullSafeComparator;
|
||||
|
||||
/**
|
||||
* {@link AbstractQueryCreator} to create {@link Predicate}-based {@link KeyValueQuery}s.
|
||||
@@ -45,6 +45,8 @@ import org.springframework.util.comparator.NullSafeComparator;
|
||||
*/
|
||||
public class PredicateQueryCreator extends AbstractQueryCreator<KeyValueQuery<Predicate<?>>, Predicate<?>> {
|
||||
|
||||
private static final Comparator<?> COMPARATOR = Comparator.nullsFirst(Comparator.naturalOrder());
|
||||
|
||||
public PredicateQueryCreator(PartTree tree, ParameterAccessor parameters) {
|
||||
super(tree, parameters);
|
||||
}
|
||||
@@ -118,6 +120,10 @@ public class PredicateQueryCreator extends AbstractQueryCreator<KeyValueQuery<Pr
|
||||
this.part = part;
|
||||
}
|
||||
|
||||
static <T> Comparator<T> comparator() {
|
||||
return (Comparator<T>) COMPARATOR;
|
||||
}
|
||||
|
||||
static PredicateBuilder propertyValueOf(Part part) {
|
||||
return new PredicateBuilder(part);
|
||||
}
|
||||
@@ -152,23 +158,19 @@ public class PredicateQueryCreator extends AbstractQueryCreator<KeyValueQuery<Pr
|
||||
}
|
||||
|
||||
public Predicate<Object> isLessThan(Object value) {
|
||||
return new ValueComparingPredicate(part.getProperty(),
|
||||
o -> NullSafeComparator.NULLS_HIGH.compare(o, value) == -1 ? true : false);
|
||||
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) == -1 ? true : false);
|
||||
}
|
||||
|
||||
public Predicate<Object> isLessThanEqual(Object value) {
|
||||
return new ValueComparingPredicate(part.getProperty(),
|
||||
o -> NullSafeComparator.NULLS_HIGH.compare(o, value) <= 0 ? true : false);
|
||||
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) <= 0 ? true : false);
|
||||
}
|
||||
|
||||
public Predicate<Object> isGreaterThan(Object value) {
|
||||
return new ValueComparingPredicate(part.getProperty(),
|
||||
o -> NullSafeComparator.NULLS_HIGH.compare(o, value) == 1 ? true : false);
|
||||
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) == 1 ? true : false);
|
||||
}
|
||||
|
||||
public Predicate<Object> isGreaterThanEqual(Object value) {
|
||||
return new ValueComparingPredicate(part.getProperty(),
|
||||
o -> NullSafeComparator.NULLS_HIGH.compare(o, value) >= 0 ? true : false);
|
||||
return new ValueComparingPredicate(part.getProperty(), o -> comparator().compare(o, value) >= 0 ? true : false);
|
||||
}
|
||||
|
||||
public Predicate<Object> matches(Pattern pattern) {
|
||||
@@ -271,6 +273,7 @@ public class PredicateQueryCreator extends AbstractQueryCreator<KeyValueQuery<Pr
|
||||
}
|
||||
|
||||
public Predicate<Object> endsWith(Object value) {
|
||||
|
||||
return new ValueComparingPredicate(part.getProperty(), o -> {
|
||||
|
||||
if (!(o instanceof String s)) {
|
||||
@@ -283,7 +286,6 @@ public class PredicateQueryCreator extends AbstractQueryCreator<KeyValueQuery<Pr
|
||||
|
||||
return s.toLowerCase().endsWith(value.toString().toLowerCase());
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user