From 8613eb26e0d8b4d6889f3d70354d326f10649835 Mon Sep 17 00:00:00 2001 From: Junghoon Ban Date: Sun, 3 Dec 2023 21:22:06 +0900 Subject: [PATCH] Use pattern matching instead of type casting. Original Pull Request #2784 Closes #2785 --- .../client/elc/CriteriaFilterProcessor.java | 11 +-- .../elc/ElasticsearchExceptionTranslator.java | 3 +- .../elc/ReactiveElasticsearchTemplate.java | 3 +- .../MappingElasticsearchConverter.java | 13 ++-- .../parser/ElasticsearchQueryCreator.java | 78 ++++++++++--------- 5 files changed, 59 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/springframework/data/elasticsearch/client/elc/CriteriaFilterProcessor.java b/src/main/java/org/springframework/data/elasticsearch/client/elc/CriteriaFilterProcessor.java index 9b52c375e..ed388c471 100644 --- a/src/main/java/org/springframework/data/elasticsearch/client/elc/CriteriaFilterProcessor.java +++ b/src/main/java/org/springframework/data/elasticsearch/client/elc/CriteriaFilterProcessor.java @@ -50,6 +50,7 @@ import org.springframework.util.Assert; * filter. * * @author Peter-Josef Meisch + * @author Junghoon Ban * @since 4.4 */ class CriteriaFilterProcessor { @@ -169,7 +170,7 @@ class CriteriaFilterProcessor { Assert.isTrue(values[1] instanceof String || values[1] instanceof Distance, "Second element of a geo distance filter must be a text or a Distance"); - String dist = (values[1] instanceof Distance) ? extractDistanceString((Distance) values[1]) : (String) values[1]; + String dist = (values[1] instanceof Distance distance) ? extractDistanceString(distance) : (String) values[1]; return QueryBuilders.geoDistance() // .field(fieldName) // @@ -178,8 +179,8 @@ class CriteriaFilterProcessor { .location(location -> { if (values[0]instanceof GeoPoint loc) { location.latlon(latlon -> latlon.lat(loc.getLat()).lon(loc.getLon())); - } else if (values[0] instanceof Point) { - GeoPoint loc = GeoPoint.fromPoint((Point) values[0]); + } else if (values[0] instanceof Point point) { + GeoPoint loc = GeoPoint.fromPoint(point); location.latlon(latlon -> latlon.lat(loc.getLat()).lon(loc.getLon())); } else { String loc = (String) values[0]; @@ -220,8 +221,8 @@ class CriteriaFilterProcessor { "single-element of boundedBy filter must be type of GeoBox or Box"); GeoBox geoBBox; - if (value instanceof Box) { - geoBBox = GeoBox.fromBox((Box) value); + if (value instanceof Box box) { + geoBBox = GeoBox.fromBox(box); } else { geoBBox = (GeoBox) value; } diff --git a/src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchExceptionTranslator.java b/src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchExceptionTranslator.java index 2851190d9..b773506d7 100644 --- a/src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchExceptionTranslator.java +++ b/src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchExceptionTranslator.java @@ -40,6 +40,7 @@ import org.springframework.data.elasticsearch.VersionConflictException; * appropriate: any other exception may have resulted from user code, and should not be translated. * * @author Peter-Josef Meisch + * @author Junghoon Ban * @since 4.4 */ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTranslator { @@ -59,7 +60,7 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra */ public RuntimeException translateException(Throwable throwable) { - RuntimeException runtimeException = throwable instanceof RuntimeException ? (RuntimeException) throwable + RuntimeException runtimeException = throwable instanceof RuntimeException ex ? ex : new RuntimeException(throwable.getMessage(), throwable); RuntimeException potentiallyTranslatedException = translateExceptionIfPossible(runtimeException); diff --git a/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java index 06b177b51..1d60ab94a 100644 --- a/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java @@ -79,6 +79,7 @@ import org.springframework.util.StringUtils; * * @author Peter-Josef Meisch * @author Illia Ulianov + * @author Junghoon Ban * @since 4.4 */ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearchTemplate { @@ -645,7 +646,7 @@ public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearch */ private RuntimeException translateException(Throwable throwable) { - RuntimeException runtimeException = throwable instanceof RuntimeException ? (RuntimeException) throwable + RuntimeException runtimeException = throwable instanceof RuntimeException ex ? ex : new RuntimeException(throwable.getMessage(), throwable); RuntimeException potentiallyTranslatedException = exceptionTranslator .translateExceptionIfPossible(runtimeException); diff --git a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java index 01b49cc93..d6cbb9dc8 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java @@ -80,6 +80,7 @@ import org.springframework.util.ObjectUtils; * @author Marc Vanbrabant * @author Anton Naydenov * @author vdisk + * @author Junghoon Ban * @since 3.2 */ public class MappingElasticsearchConverter @@ -503,16 +504,16 @@ public class MappingElasticsearchConverter private Object propertyConverterRead(ElasticsearchPersistentProperty property, Object source) { PropertyValueConverter propertyValueConverter = Objects.requireNonNull(property.getPropertyValueConverter()); - if (source instanceof String[]) { + if (source instanceof String[] strings) { // convert to a List - source = Arrays.asList((String[]) source); + source = Arrays.asList(strings); } - if (source instanceof List) { - source = ((List) source).stream().map(it -> convertOnRead(propertyValueConverter, it)) + if (source instanceof List list) { + source = list.stream().map(it -> convertOnRead(propertyValueConverter, it)) .collect(Collectors.toList()); - } else if (source instanceof Set) { - source = ((Set) source).stream().map(it -> convertOnRead(propertyValueConverter, it)) + } else if (source instanceof Set set) { + source = set.stream().map(it -> convertOnRead(propertyValueConverter, it)) .collect(Collectors.toSet()); } else { source = convertOnRead(propertyValueConverter, source); diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/query/parser/ElasticsearchQueryCreator.java b/src/main/java/org/springframework/data/elasticsearch/repository/query/parser/ElasticsearchQueryCreator.java index c8079922b..600e02a8a 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/query/parser/ElasticsearchQueryCreator.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/query/parser/ElasticsearchQueryCreator.java @@ -17,7 +17,6 @@ package org.springframework.data.elasticsearch.repository.query.parser; import java.util.Collection; import java.util.Iterator; - import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.core.geo.GeoBox; @@ -44,6 +43,7 @@ import org.springframework.lang.Nullable; * @author Franck Marchand * @author Artur Konczak * @author Peter-Josef Meisch + * @author Junghoon Ban */ public class ElasticsearchQueryCreator extends AbstractQueryCreator { @@ -62,8 +62,8 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator iterator) { - PersistentPropertyPath path = context - .getPersistentPropertyPath(part.getProperty()); + PersistentPropertyPath path = context.getPersistentPropertyPath( + part.getProperty()); return new CriteriaQuery(from(part, new Criteria(path.toDotPath(ElasticsearchPersistentProperty.QueryPropertyToFieldNameConverter.INSTANCE)), iterator)); @@ -74,8 +74,8 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator path = context - .getPersistentPropertyPath(part.getProperty()); + PersistentPropertyPath path = context.getPersistentPropertyPath( + part.getProperty()); return base.addCriteria(from(part, new Criteria(path.toDotPath(ElasticsearchPersistentProperty.QueryPropertyToFieldNameConverter.INSTANCE)), iterator)); @@ -109,8 +109,7 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator) o).toArray();