Polishing

- extract methods
- remove unused code
This commit is contained in:
Gerrit Meier
2019-05-21 11:39:57 +02:00
committed by Michael Simons
parent 8018d5d133
commit ab8a187607
3 changed files with 24 additions and 38 deletions

View File

@@ -102,20 +102,11 @@ abstract class AbstractNeo4jQuery implements RepositoryQuery {
*/
final Object convertParameter(Object parameter) {
if (parameter instanceof Range) {
Range range = (Range) parameter;
Map<String, Object> map = new HashMap<>();
range.getLowerBound().getValue().map(this::convertParameter).ifPresent(v -> map.put("lb", v));
range.getUpperBound().getValue().map(this::convertParameter).ifPresent(v -> map.put("ub", v));
return map;
return convertRange((Range) parameter);
} else if (parameter instanceof Distance) {
return calculateDistanceInMeter((Distance) parameter);
} else if (parameter instanceof Circle) {
Circle circle = (Circle) parameter;
Map<String, Object> map = new HashMap<>();
map.put("x", convertParameter(circle.getCenter().getX()));
map.put("y", convertParameter(circle.getCenter().getY()));
map.put("radius", convertParameter(calculateDistanceInMeter(circle.getRadius())));
return map;
return convertCircle((Circle) parameter);
}
// Good hook to check the NodeManager whether the thing is an entity and we replace the value with a known id.
@@ -123,12 +114,31 @@ abstract class AbstractNeo4jQuery implements RepositoryQuery {
return parameter;
}
private Map<String, Object> convertRange(Range range) {
Map<String, Object> map = new HashMap<>();
range.getLowerBound().getValue().map(this::convertParameter).ifPresent(v -> map.put("lb", v));
range.getUpperBound().getValue().map(this::convertParameter).ifPresent(v -> map.put("ub", v));
return map;
}
private Map<String, Object> convertCircle(Circle circle) {
Map<String, Object> map = new HashMap<>();
map.put("x", convertParameter(circle.getCenter().getX()));
map.put("y", convertParameter(circle.getCenter().getY()));
map.put("radius", convertParameter(calculateDistanceInMeter(circle.getRadius())));
return map;
}
private static double calculateDistanceInMeter(Distance distance) {
if (distance.getMetric() == Metrics.KILOMETERS) {
return distance.getValue() / 0.001d;
double kilometersDivisor = 0.001d;
return distance.getValue() / kilometersDivisor;
} else if (distance.getMetric() == Metrics.MILES) {
return distance.getValue() / 0.00062137d;
double milesDivisor = 0.00062137d;
return distance.getValue() / milesDivisor;
} else {
return distance.getValue();
}

View File

@@ -42,7 +42,6 @@ import org.springframework.data.neo4j.repository.query.Neo4jQueryMethod.Neo4jPar
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.ResultProcessor;
import org.springframework.data.repository.query.parser.Part;
import org.springframework.data.repository.query.parser.PartTree;
import org.springframework.data.repository.query.parser.PartTree.OrPart;
@@ -73,7 +72,6 @@ final class PartTreeNeo4jQuery extends AbstractNeo4jQuery {
Part.Type.NOT_CONTAINING,
Part.Type.NOT_LIKE, Part.Type.SIMPLE_PROPERTY, Part.Type.STARTING_WITH);
private final ResultProcessor processor;
private final PartTree tree;
PartTreeNeo4jQuery(
@@ -83,7 +81,6 @@ final class PartTreeNeo4jQuery extends AbstractNeo4jQuery {
) {
super(nodeManager, mappingContext, queryMethod);
this.processor = queryMethod.getResultProcessor();
this.tree = new PartTree(queryMethod.getName(), domainType);
// Validate parts. Sort properties will be validated by Spring Data already.

View File

@@ -33,7 +33,6 @@ import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.SpelEvaluator;
import org.springframework.data.repository.query.SpelQueryContext;
import org.springframework.data.repository.query.SpelQueryContext.SpelExtractor;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -113,26 +112,6 @@ final class StringBasedNeo4jQuery extends AbstractNeo4jQuery {
cypherTemplate, queryAnnotation.count(), queryAnnotation.exists(), queryAnnotation.delete());
}
/**
* Create a {@link StringBasedNeo4jQuery} based on an explicit Cypher template.
*
* @param nodeManager
* @param mappingContext
* @param evaluationContextProvider
* @param queryMethod
* @param cypherTemplate The template to use.
* @return A new instance of a String based Neo4j query.
*/
static StringBasedNeo4jQuery create(NodeManager nodeManager, Neo4jMappingContext mappingContext,
QueryMethodEvaluationContextProvider evaluationContextProvider,
Neo4jQueryMethod queryMethod, String cypherTemplate) {
Assert.hasText(cypherTemplate, "Cannot create String based Neo4j query without a cypher template.");
return new StringBasedNeo4jQuery(nodeManager, mappingContext, evaluationContextProvider, queryMethod,
cypherTemplate, false, false, false);
}
private StringBasedNeo4jQuery(NodeManager nodeManager,
Neo4jMappingContext mappingContext, QueryMethodEvaluationContextProvider evaluationContextProvider,
Neo4jQueryMethod queryMethod, String cypherTemplate, boolean countQuery,
@@ -198,7 +177,7 @@ final class StringBasedNeo4jQuery extends AbstractNeo4jQuery {
int parameterIndex = parameter.getIndex();
Object parameterValue = super.convertParameter(actualParameters[parameterIndex]);
// Add the parameter under it's name when possible
// Add the parameter under its name when possible
parameter.getName()
.ifPresent(parameterName -> resolvedParameters.put(parameterName, parameterValue));
// Always add under its index.