Polishing.
Improve pattern variable naming, use ClassUtils.getDescriptiveType(…) to avoid null-pointer exceptions. Simplify RedisOperationChain condition checks. See #2788
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.repository.query;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
@@ -30,6 +31,7 @@ import org.springframework.data.repository.query.parser.AbstractQueryCreator;
|
||||
import org.springframework.data.repository.query.parser.Part;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
@@ -80,17 +82,15 @@ public class RedisQueryCreator extends AbstractQueryCreator<KeyValueQuery<RedisO
|
||||
}
|
||||
|
||||
@Override
|
||||
protected KeyValueQuery<RedisOperationChain> complete(@Nullable final RedisOperationChain criteria, Sort sort) {
|
||||
protected KeyValueQuery<RedisOperationChain> complete(@Nullable RedisOperationChain criteria, Sort sort) {
|
||||
|
||||
KeyValueQuery<RedisOperationChain> query = new KeyValueQuery<>(criteria);
|
||||
|
||||
if (criteria != null && !CollectionUtils.isEmpty(criteria.getSismember())
|
||||
&& !CollectionUtils.isEmpty(criteria.getOrSismember()))
|
||||
if (criteria.getSismember().size() == 1 && criteria.getOrSismember().size() == 1) {
|
||||
|
||||
criteria.getOrSismember().add(criteria.getSismember().iterator().next());
|
||||
criteria.getSismember().clear();
|
||||
}
|
||||
if (criteria != null && containsExactlyOne(criteria.getSismember())
|
||||
&& containsExactlyOne(criteria.getOrSismember())) {
|
||||
criteria.getOrSismember().addAll(criteria.getSismember());
|
||||
criteria.getSismember().clear();
|
||||
}
|
||||
|
||||
if (sort.isSorted()) {
|
||||
query.setSort(sort);
|
||||
@@ -111,29 +111,33 @@ public class RedisQueryCreator extends AbstractQueryCreator<KeyValueQuery<RedisO
|
||||
if (value instanceof Point point) {
|
||||
|
||||
if (!iterator.hasNext()) {
|
||||
String message = "Expected to find distance value for geo query; Are you missing a parameter";
|
||||
String message = "Expected to find distance value for geo query; Are you missing a parameter?";
|
||||
throw new InvalidDataAccessApiUsageException(message);
|
||||
}
|
||||
|
||||
Distance distance;
|
||||
Object distObject = iterator.next();
|
||||
|
||||
if (distObject instanceof Distance distanceValue) {
|
||||
distance = distanceValue;
|
||||
} else if (distObject instanceof Number numberValue) {
|
||||
distance = new Distance(numberValue.doubleValue(), Metrics.KILOMETERS);
|
||||
if (distObject instanceof Distance dist) {
|
||||
distance = dist;
|
||||
} else if (distObject instanceof Number num) {
|
||||
distance = new Distance(num.doubleValue(), Metrics.KILOMETERS);
|
||||
} else {
|
||||
|
||||
String message = String.format("Expected to find Distance or Numeric value for geo query but was %s",
|
||||
distObject.getClass());
|
||||
ClassUtils.getDescriptiveType(distObject));
|
||||
throw new InvalidDataAccessApiUsageException(message);
|
||||
}
|
||||
|
||||
return new NearPath(path, point, distance);
|
||||
}
|
||||
|
||||
String message = String.format("Expected to find a Circle or Point/Distance for geo query but was %s.",
|
||||
value.getClass());
|
||||
String message = String.format("Expected to find a Circle or Point/Distance for geo query but was %s",
|
||||
ClassUtils.getDescriptiveType(value.getClass()));
|
||||
throw new InvalidDataAccessApiUsageException(message);
|
||||
}
|
||||
|
||||
private static boolean containsExactlyOne(Collection<?> collection) {
|
||||
return !CollectionUtils.isEmpty(collection) && collection.size() == 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user