Skip to content

Commit 4c2328a

Browse files
michael-simonsmeistermeier
authored andcommitted
Use switch expressions or enhanced switch statements where possible.
1 parent 4c9d493 commit 4c2328a

File tree

12 files changed

+112
-217
lines changed

12 files changed

+112
-217
lines changed

src/main/java/org/springframework/data/neo4j/core/Neo4jTemplate.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,11 @@ <T, R> List<R> doFind(@Nullable String cypherQuery, @Nullable Map<String, Object
263263
} else {
264264
executableQuery = createExecutableQuery(domainType, resultType, queryFragmentsAndParameters);
265265
}
266-
switch (fetchType) {
267-
case ALL:
268-
intermediaResults = executableQuery.getResults();
269-
break;
270-
case ONE:
271-
intermediaResults = executableQuery.getSingleResult().map(Collections::singletonList)
272-
.orElseGet(Collections::emptyList);
273-
break;
274-
}
266+
intermediaResults = switch (fetchType) {
267+
case ALL -> executableQuery.getResults();
268+
case ONE -> executableQuery.getSingleResult().map(Collections::singletonList)
269+
.orElseGet(Collections::emptyList);
270+
};
275271
}
276272

277273
if (resultType.isAssignableFrom(domainType)) {
@@ -408,7 +404,7 @@ private <T> T saveImpl(T instance, @Nullable Map<PropertyPath, Boolean> included
408404
.fetchAs(Entity.class)
409405
.one();
410406

411-
if (!newOrUpdatedNode.isPresent()) {
407+
if (newOrUpdatedNode.isEmpty()) {
412408
if (entityMetaData.hasVersionProperty()) {
413409
throw new OptimisticLockingFailureException(OPTIMISTIC_LOCKING_ERROR_MESSAGE);
414410
}

src/main/java/org/springframework/data/neo4j/core/ReactiveNeo4jTemplate.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public <T> ExecutableFind<T> find(Class<T> domainType) {
233233
@SuppressWarnings("unchecked")
234234
<T, R> Flux<R> doFind(@Nullable String cypherQuery, @Nullable Map<String, Object> parameters, Class<T> domainType, Class<R> resultType, TemplateSupport.FetchType fetchType, @Nullable QueryFragmentsAndParameters queryFragmentsAndParameters) {
235235

236-
Flux<T> intermediaResults = null;
236+
Flux<T> intermediaResults;
237237
if (cypherQuery == null && queryFragmentsAndParameters == null && fetchType == TemplateSupport.FetchType.ALL) {
238238
intermediaResults = doFindAll(domainType, resultType);
239239
} else {
@@ -245,14 +245,10 @@ <T, R> Flux<R> doFind(@Nullable String cypherQuery, @Nullable Map<String, Object
245245
executableQuery = createExecutableQuery(domainType, resultType, queryFragmentsAndParameters);
246246
}
247247

248-
switch (fetchType) {
249-
case ALL:
250-
intermediaResults = executableQuery.flatMapMany(ExecutableQuery::getResults);
251-
break;
252-
case ONE:
253-
intermediaResults = executableQuery.flatMap(ExecutableQuery::getSingleResult).flux();
254-
break;
255-
}
248+
intermediaResults = switch (fetchType) {
249+
case ALL -> executableQuery.flatMapMany(ExecutableQuery::getResults);
250+
case ONE -> executableQuery.flatMap(ExecutableQuery::getSingleResult).flux();
251+
};
256252
}
257253

258254
if (resultType.isAssignableFrom(domainType)) {

src/main/java/org/springframework/data/neo4j/core/ResultSummaries.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,11 @@ private static void logNotifications(ResultSummary resultSummary) {
5757
String query = resultSummary.query().text();
5858
resultSummary.notifications()
5959
.forEach(notification -> {
60-
Consumer<String> log;
61-
switch (notification.severity()) {
62-
case "WARNING":
63-
log = Neo4jClient.cypherLog::warn;
64-
break;
65-
case "INFORMATION":
66-
log = Neo4jClient.cypherLog::info;
67-
break;
68-
default:
69-
log = Neo4jClient.cypherLog::debug;
70-
}
60+
Consumer<String> log = switch (notification.severity()) {
61+
case "WARNING" -> Neo4jClient.cypherLog::warn;
62+
case "INFORMATION" -> Neo4jClient.cypherLog::info;
63+
default -> Neo4jClient.cypherLog::debug;
64+
};
7165
log.accept(ResultSummaries.format(notification, query));
7266
});
7367
}

src/main/java/org/springframework/data/neo4j/core/mapping/CypherGenerator.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,10 @@ public StatementBuilder.OngoingReading prepareMatchOf(NodeDescription<?> nodeDes
154154
}
155155
String[] types = relationshipTypes.toArray(new String[]{});
156156

157-
Relationship relationship;
158-
switch (relationshipDescription.getDirection()) {
159-
case OUTGOING:
160-
relationship = rootNode.relationshipTo(targetNode, types);
161-
break;
162-
case INCOMING:
163-
relationship = rootNode.relationshipFrom(targetNode, types);
164-
break;
165-
default:
166-
relationship = rootNode.relationshipBetween(targetNode, types);
167-
break;
168-
}
157+
Relationship relationship = switch (relationshipDescription.getDirection()) {
158+
case OUTGOING -> rootNode.relationshipTo(targetNode, types);
159+
case INCOMING -> rootNode.relationshipFrom(targetNode, types);
160+
};
169161

170162
relationship = relationship.named(Constants.NAME_OF_SYNTHESIZED_RELATIONS);
171163
List<Expression> expressions = new ArrayList<>();

src/main/java/org/springframework/data/neo4j/core/mapping/NodeDescriptionStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public NodeDescriptionAndLabels deriveConcreteNodeDescription(Neo4jPersistentEnt
100100
Function<NodeDescription<?>, Integer> count = (nodeDescription) -> Math.toIntExact(nodeDescription.getStaticLabels().stream().filter(labels::contains).count());
101101
Optional<Map.Entry<NodeDescription<?>, Integer>> mostMatchingNodeDescription = haystack.stream()
102102
.filter(nd -> labels.containsAll(nd.getStaticLabels())) // remove candidates having more mandatory labels
103-
.collect(Collectors.toMap(Function.identity(), nodeDescription -> count.apply(nodeDescription)))
103+
.collect(Collectors.toMap(Function.identity(), count::apply))
104104
.entrySet().stream()
105105
.max(Comparator.comparingInt(Map.Entry::getValue));
106106

src/main/java/org/springframework/data/neo4j/repository/query/CypherQueryCreator.java

Lines changed: 50 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,12 @@ private ExposesRelationships<?> createRelationshipChain(ExposesRelationships<?>
200200
relatedNode = relatedNode.named(getNodeName());
201201
}
202202

203-
switch (relationshipDescription.getDirection()) {
204-
case OUTGOING:
205-
cypherRelationship = cypherRelationship
206-
.relationshipTo(relatedNode, relationshipDescription.getType());
207-
break;
208-
case INCOMING:
209-
cypherRelationship = cypherRelationship
210-
.relationshipFrom(relatedNode, relationshipDescription.getType());
211-
break;
212-
default:
213-
cypherRelationship = cypherRelationship
214-
.relationshipBetween(relatedNode, relationshipDescription.getType());
215-
}
203+
cypherRelationship = switch (relationshipDescription.getDirection()) {
204+
case OUTGOING -> cypherRelationship
205+
.relationshipTo(relatedNode, relationshipDescription.getType());
206+
case INCOMING -> cypherRelationship
207+
.relationshipFrom(relatedNode, relationshipDescription.getType());
208+
};
216209

217210
if (lastNode || hasTargetNode) {
218211
cypherRelationship = ((RelationshipPattern) cypherRelationship).named(getRelationshipName());
@@ -326,73 +319,45 @@ private Condition createImpl(Part part, Iterator<Object> actualParameters) {
326319
Neo4jPersistentProperty property = path.getRequiredLeafProperty();
327320

328321
boolean ignoreCase = ignoreCase(part);
329-
switch (part.getType()) {
330-
case AFTER:
331-
case GREATER_THAN:
332-
return toCypherProperty(path, ignoreCase)
333-
.gt(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
334-
case BEFORE:
335-
case LESS_THAN:
336-
return toCypherProperty(path, ignoreCase)
337-
.lt(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
338-
case BETWEEN:
339-
return betweenCondition(path, actualParameters, ignoreCase);
340-
case CONTAINING:
341-
return containingCondition(path, property, actualParameters, ignoreCase);
342-
case ENDING_WITH:
343-
return toCypherProperty(path, ignoreCase)
344-
.endsWith(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
345-
case EXISTS:
346-
return Predicates.exists(toCypherProperty(property));
347-
case FALSE:
348-
return toCypherProperty(path, ignoreCase).isFalse();
349-
case GREATER_THAN_EQUAL:
350-
return toCypherProperty(path, ignoreCase)
351-
.gte(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
352-
case IN:
353-
return toCypherProperty(path, ignoreCase)
354-
.in(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
355-
case IS_EMPTY:
356-
return toCypherProperty(path, ignoreCase).isEmpty();
357-
case IS_NOT_EMPTY:
358-
return toCypherProperty(path, ignoreCase).isEmpty().not();
359-
case IS_NOT_NULL:
360-
return toCypherProperty(path, ignoreCase).isNotNull();
361-
case IS_NULL:
362-
return toCypherProperty(path, ignoreCase).isNull();
363-
case LESS_THAN_EQUAL:
364-
return toCypherProperty(path, ignoreCase)
365-
.lte(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
366-
case LIKE:
367-
return likeCondition(path, nextRequiredParameter(actualParameters, property).nameOrIndex, ignoreCase);
368-
case NEAR:
369-
return createNearCondition(path, actualParameters);
370-
case NEGATING_SIMPLE_PROPERTY:
371-
return toCypherProperty(path, ignoreCase)
372-
.isNotEqualTo(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
373-
case NOT_CONTAINING:
374-
return containingCondition(path, property, actualParameters, ignoreCase).not();
375-
case NOT_IN:
376-
return toCypherProperty(path, ignoreCase)
377-
.in(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase)).not();
378-
case NOT_LIKE:
379-
return likeCondition(path, nextRequiredParameter(actualParameters, property).nameOrIndex, ignoreCase).not();
380-
case SIMPLE_PROPERTY:
381-
return toCypherProperty(path, ignoreCase)
382-
.isEqualTo(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
383-
case STARTING_WITH:
384-
return toCypherProperty(path, ignoreCase)
385-
.startsWith(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
386-
case REGEX:
387-
return toCypherProperty(path, ignoreCase)
388-
.matches(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
389-
case TRUE:
390-
return toCypherProperty(path, ignoreCase).isTrue();
391-
case WITHIN:
392-
return createWithinCondition(path, actualParameters);
393-
default:
394-
throw new IllegalArgumentException("Unsupported part type: " + part.getType());
395-
}
322+
return switch (part.getType()) {
323+
case AFTER, GREATER_THAN -> toCypherProperty(path, ignoreCase)
324+
.gt(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
325+
case BEFORE, LESS_THAN -> toCypherProperty(path, ignoreCase)
326+
.lt(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
327+
case BETWEEN -> betweenCondition(path, actualParameters, ignoreCase);
328+
case CONTAINING -> containingCondition(path, property, actualParameters, ignoreCase);
329+
case ENDING_WITH -> toCypherProperty(path, ignoreCase)
330+
.endsWith(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
331+
case EXISTS -> Predicates.exists(toCypherProperty(property));
332+
case FALSE -> toCypherProperty(path, ignoreCase).isFalse();
333+
case GREATER_THAN_EQUAL -> toCypherProperty(path, ignoreCase)
334+
.gte(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
335+
case IN -> toCypherProperty(path, ignoreCase)
336+
.in(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
337+
case IS_EMPTY -> toCypherProperty(path, ignoreCase).isEmpty();
338+
case IS_NOT_EMPTY -> toCypherProperty(path, ignoreCase).isEmpty().not();
339+
case IS_NOT_NULL -> toCypherProperty(path, ignoreCase).isNotNull();
340+
case IS_NULL -> toCypherProperty(path, ignoreCase).isNull();
341+
case LESS_THAN_EQUAL -> toCypherProperty(path, ignoreCase)
342+
.lte(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
343+
case LIKE -> likeCondition(path, nextRequiredParameter(actualParameters, property).nameOrIndex, ignoreCase);
344+
case NEAR -> createNearCondition(path, actualParameters);
345+
case NEGATING_SIMPLE_PROPERTY -> toCypherProperty(path, ignoreCase)
346+
.isNotEqualTo(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
347+
case NOT_CONTAINING -> containingCondition(path, property, actualParameters, ignoreCase).not();
348+
case NOT_IN -> toCypherProperty(path, ignoreCase)
349+
.in(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase)).not();
350+
case NOT_LIKE -> likeCondition(path, nextRequiredParameter(actualParameters, property).nameOrIndex,
351+
ignoreCase).not();
352+
case SIMPLE_PROPERTY -> toCypherProperty(path, ignoreCase)
353+
.isEqualTo(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
354+
case STARTING_WITH -> toCypherProperty(path, ignoreCase)
355+
.startsWith(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
356+
case REGEX -> toCypherProperty(path, ignoreCase)
357+
.matches(toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase));
358+
case TRUE -> toCypherProperty(path, ignoreCase).isTrue();
359+
case WITHIN -> createWithinCondition(path, actualParameters);
360+
};
396361
}
397362

398363
private Condition containingCondition(PersistentPropertyPath<Neo4jPersistentProperty> path,
@@ -415,16 +380,11 @@ private Condition containingCondition(PersistentPropertyPath<Neo4jPersistentProp
415380
*/
416381
boolean ignoreCase(Part part) {
417382

418-
switch (part.shouldIgnoreCase()) {
419-
case ALWAYS:
420-
return true;
421-
case WHEN_POSSIBLE:
422-
return PartValidator.canIgnoreCase(part);
423-
case NEVER:
424-
return false;
425-
default:
426-
throw new IllegalArgumentException("Unsupported option for ignoring case: " + part.shouldIgnoreCase());
427-
}
383+
return switch (part.shouldIgnoreCase()) {
384+
case ALWAYS -> true;
385+
case WHEN_POSSIBLE -> PartValidator.canIgnoreCase(part);
386+
case NEVER -> false;
387+
};
428388
}
429389

430390
private Condition likeCondition(PersistentPropertyPath<Neo4jPersistentProperty> path, String parameterName,

src/main/java/org/springframework/data/neo4j/repository/query/PartValidator.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,9 @@ void validatePart(Part part) {
7575

7676
validateIgnoreCase(part);
7777
switch (part.getType()) {
78-
case AFTER:
79-
case BEFORE:
80-
validateTemporalProperty(part);
81-
break;
82-
case IS_EMPTY:
83-
case IS_NOT_EMPTY:
84-
validateCollectionProperty(part);
85-
break;
86-
case NEAR:
87-
case WITHIN:
88-
validatePointProperty(part);
89-
break;
78+
case AFTER, BEFORE -> validateTemporalProperty(part);
79+
case IS_EMPTY, IS_NOT_EMPTY -> validateCollectionProperty(part);
80+
case NEAR, WITHIN -> validatePointProperty(part);
9081
}
9182

9283
validateNotACompositeProperty(part);

src/main/java/org/springframework/data/neo4j/repository/query/Predicate.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
import org.neo4j.cypherdsl.core.StatementBuilder;
3434
import org.springframework.data.domain.Example;
3535
import org.springframework.data.domain.ExampleMatcher;
36+
import org.springframework.data.neo4j.core.convert.Neo4jConversionService;
3637
import org.springframework.data.neo4j.core.mapping.Constants;
3738
import org.springframework.data.neo4j.core.mapping.GraphPropertyDescription;
38-
import org.springframework.data.neo4j.core.convert.Neo4jConversionService;
3939
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;
4040
import org.springframework.data.neo4j.core.mapping.Neo4jPersistentEntity;
4141
import org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty;
@@ -82,7 +82,7 @@ static <S> Predicate create(Neo4jMappingContext mappingContext, Example<S> examp
8282
Optional<Object> optionalValue = transformer
8383
.apply(Optional.ofNullable(beanWrapper.getPropertyValue(currentPath)));
8484

85-
if (!optionalValue.isPresent()) {
85+
if (optionalValue.isEmpty()) {
8686
if (!internalId && matcherAccessor.getNullHandler().equals(ExampleMatcher.NullHandler.INCLUDE)) {
8787
predicate.add(mode, property(Constants.NAME_OF_TYPED_ROOT_NODE.apply(probeNodeDescription), propertyName).isNull());
8888
}
@@ -108,29 +108,15 @@ static <S> Predicate create(Neo4jMappingContext mappingContext, Example<S> examp
108108
parameter = Functions.toLower(parameter);
109109
}
110110

111-
switch (matcherAccessor.getStringMatcherForPath(currentPath)) {
112-
case DEFAULT:
113-
case EXACT:
114-
// This needs to be recreated as both property and parameter might have changed above
115-
condition = property.isEqualTo(parameter);
116-
break;
117-
case CONTAINING:
118-
condition = property.contains(parameter);
119-
break;
120-
case STARTING:
121-
condition = property.startsWith(parameter);
122-
break;
123-
case ENDING:
124-
condition = property.endsWith(parameter);
125-
break;
126-
case REGEX:
127-
condition = property.matches(parameter);
128-
break;
129-
default:
130-
throw new IllegalArgumentException(
131-
"Unsupported StringMatcher " + matcherAccessor
132-
.getStringMatcherForPath(currentPath));
133-
}
111+
condition = switch (matcherAccessor.getStringMatcherForPath(currentPath)) {
112+
case DEFAULT, EXACT ->
113+
// This needs to be recreated as both property and parameter might have changed above
114+
property.isEqualTo(parameter);
115+
case CONTAINING -> property.contains(parameter);
116+
case STARTING -> property.startsWith(parameter);
117+
case ENDING -> property.endsWith(parameter);
118+
case REGEX -> property.matches(parameter);
119+
};
134120
}
135121
predicate.add(mode, condition);
136122
predicate.parameters.put(propertyName, optionalValue.map(
@@ -167,16 +153,10 @@ StatementBuilder.OrderableOngoingReadingAndWith useWithReadingFragment(
167153

168154
private void add(ExampleMatcher.MatchMode matchMode, Condition additionalCondition) {
169155

170-
switch (matchMode) {
171-
case ALL:
172-
this.condition = this.condition.and(additionalCondition);
173-
break;
174-
case ANY:
175-
this.condition = this.condition.or(additionalCondition);
176-
break;
177-
default:
178-
throw new IllegalArgumentException("Unsupported match mode: " + matchMode);
179-
}
156+
this.condition = switch (matchMode) {
157+
case ALL -> this.condition.and(additionalCondition);
158+
case ANY -> this.condition.or(additionalCondition);
159+
};
180160
}
181161

182162
public NodeDescription<?> getNeo4jPersistentEntity() {

src/test/java/org/springframework/data/neo4j/integration/conversion_imperative/Neo4jConversionsIT.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,12 @@ Stream<DynamicTest> customConversions() {
8787

8888
ConverterBuilder.ConverterAware converterAware = ConverterBuilder.reading(Value.class, LocalDate.class, v -> {
8989
String s = v.asString();
90-
switch (s) {
91-
case "gestern":
92-
return LocalDate.now().minusDays(1);
93-
case "heute":
94-
return LocalDate.now();
95-
case "morgen":
96-
return LocalDate.now().plusDays(1);
97-
default:
98-
throw new IllegalArgumentException();
99-
}
90+
return switch (s) {
91+
case "gestern" -> LocalDate.now().minusDays(1);
92+
case "heute" -> LocalDate.now();
93+
case "morgen" -> LocalDate.now().plusDays(1);
94+
default -> throw new IllegalArgumentException();
95+
};
10096
}).andWriting(d -> {
10197
if (d.isBefore(LocalDate.now())) {
10298
return Values.value("gestern");

0 commit comments

Comments
 (0)