Skip to content

Commit 8613eb2

Browse files
Use pattern matching instead of type casting.
Original Pull Request #2784 Closes #2785
1 parent 415d5e0 commit 8613eb2

File tree

5 files changed

+59
-49
lines changed

5 files changed

+59
-49
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/CriteriaFilterProcessor.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* filter.
5151
*
5252
* @author Peter-Josef Meisch
53+
* @author Junghoon Ban
5354
* @since 4.4
5455
*/
5556
class CriteriaFilterProcessor {
@@ -169,7 +170,7 @@ private static ObjectBuilder<GeoDistanceQuery> withinQuery(String fieldName, Obj
169170
Assert.isTrue(values[1] instanceof String || values[1] instanceof Distance,
170171
"Second element of a geo distance filter must be a text or a Distance");
171172

172-
String dist = (values[1] instanceof Distance) ? extractDistanceString((Distance) values[1]) : (String) values[1];
173+
String dist = (values[1] instanceof Distance distance) ? extractDistanceString(distance) : (String) values[1];
173174

174175
return QueryBuilders.geoDistance() //
175176
.field(fieldName) //
@@ -178,8 +179,8 @@ private static ObjectBuilder<GeoDistanceQuery> withinQuery(String fieldName, Obj
178179
.location(location -> {
179180
if (values[0]instanceof GeoPoint loc) {
180181
location.latlon(latlon -> latlon.lat(loc.getLat()).lon(loc.getLon()));
181-
} else if (values[0] instanceof Point) {
182-
GeoPoint loc = GeoPoint.fromPoint((Point) values[0]);
182+
} else if (values[0] instanceof Point point) {
183+
GeoPoint loc = GeoPoint.fromPoint(point);
183184
location.latlon(latlon -> latlon.lat(loc.getLat()).lon(loc.getLon()));
184185
} else {
185186
String loc = (String) values[0];
@@ -220,8 +221,8 @@ private static void oneParameterBBox(GeoBoundingBoxQuery.Builder queryBuilder, O
220221
"single-element of boundedBy filter must be type of GeoBox or Box");
221222

222223
GeoBox geoBBox;
223-
if (value instanceof Box) {
224-
geoBBox = GeoBox.fromBox((Box) value);
224+
if (value instanceof Box box) {
225+
geoBBox = GeoBox.fromBox(box);
225226
} else {
226227
geoBBox = (GeoBox) value;
227228
}

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchExceptionTranslator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* appropriate: any other exception may have resulted from user code, and should not be translated.
4141
*
4242
* @author Peter-Josef Meisch
43+
* @author Junghoon Ban
4344
* @since 4.4
4445
*/
4546
public class ElasticsearchExceptionTranslator implements PersistenceExceptionTranslator {
@@ -59,7 +60,7 @@ public ElasticsearchExceptionTranslator(JsonpMapper jsonpMapper) {
5960
*/
6061
public RuntimeException translateException(Throwable throwable) {
6162

62-
RuntimeException runtimeException = throwable instanceof RuntimeException ? (RuntimeException) throwable
63+
RuntimeException runtimeException = throwable instanceof RuntimeException ex ? ex
6364
: new RuntimeException(throwable.getMessage(), throwable);
6465
RuntimeException potentiallyTranslatedException = translateExceptionIfPossible(runtimeException);
6566

src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
*
8080
* @author Peter-Josef Meisch
8181
* @author Illia Ulianov
82+
* @author Junghoon Ban
8283
* @since 4.4
8384
*/
8485
public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearchTemplate {
@@ -645,7 +646,7 @@ public <T> Publisher<T> execute(ReactiveElasticsearchTemplate.ClientCallback<Pub
645646
*/
646647
private RuntimeException translateException(Throwable throwable) {
647648

648-
RuntimeException runtimeException = throwable instanceof RuntimeException ? (RuntimeException) throwable
649+
RuntimeException runtimeException = throwable instanceof RuntimeException ex ? ex
649650
: new RuntimeException(throwable.getMessage(), throwable);
650651
RuntimeException potentiallyTranslatedException = exceptionTranslator
651652
.translateExceptionIfPossible(runtimeException);

src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
* @author Marc Vanbrabant
8181
* @author Anton Naydenov
8282
* @author vdisk
83+
* @author Junghoon Ban
8384
* @since 3.2
8485
*/
8586
public class MappingElasticsearchConverter
@@ -503,16 +504,16 @@ TypeInformation<?> getCollectionComponentType(TypeInformation<?> type) {
503504
private Object propertyConverterRead(ElasticsearchPersistentProperty property, Object source) {
504505
PropertyValueConverter propertyValueConverter = Objects.requireNonNull(property.getPropertyValueConverter());
505506

506-
if (source instanceof String[]) {
507+
if (source instanceof String[] strings) {
507508
// convert to a List
508-
source = Arrays.asList((String[]) source);
509+
source = Arrays.asList(strings);
509510
}
510511

511-
if (source instanceof List) {
512-
source = ((List<?>) source).stream().map(it -> convertOnRead(propertyValueConverter, it))
512+
if (source instanceof List<?> list) {
513+
source = list.stream().map(it -> convertOnRead(propertyValueConverter, it))
513514
.collect(Collectors.toList());
514-
} else if (source instanceof Set) {
515-
source = ((Set<?>) source).stream().map(it -> convertOnRead(propertyValueConverter, it))
515+
} else if (source instanceof Set<?> set) {
516+
source = set.stream().map(it -> convertOnRead(propertyValueConverter, it))
516517
.collect(Collectors.toSet());
517518
} else {
518519
source = convertOnRead(propertyValueConverter, source);

src/main/java/org/springframework/data/elasticsearch/repository/query/parser/ElasticsearchQueryCreator.java

+42-36
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.Collection;
1919
import java.util.Iterator;
20-
2120
import org.springframework.dao.InvalidDataAccessApiUsageException;
2221
import org.springframework.data.domain.Sort;
2322
import org.springframework.data.elasticsearch.core.geo.GeoBox;
@@ -44,6 +43,7 @@
4443
* @author Franck Marchand
4544
* @author Artur Konczak
4645
* @author Peter-Josef Meisch
46+
* @author Junghoon Ban
4747
*/
4848
public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuery, CriteriaQuery> {
4949

@@ -62,8 +62,8 @@ public ElasticsearchQueryCreator(PartTree tree, MappingContext<?, ElasticsearchP
6262

6363
@Override
6464
protected CriteriaQuery create(Part part, Iterator<Object> iterator) {
65-
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context
66-
.getPersistentPropertyPath(part.getProperty());
65+
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context.getPersistentPropertyPath(
66+
part.getProperty());
6767
return new CriteriaQuery(from(part,
6868
new Criteria(path.toDotPath(ElasticsearchPersistentProperty.QueryPropertyToFieldNameConverter.INSTANCE)),
6969
iterator));
@@ -74,8 +74,8 @@ protected CriteriaQuery and(Part part, CriteriaQuery base, Iterator<Object> iter
7474
if (base == null) {
7575
return create(part, iterator);
7676
}
77-
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context
78-
.getPersistentPropertyPath(part.getProperty());
77+
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context.getPersistentPropertyPath(
78+
part.getProperty());
7979
return base.addCriteria(from(part,
8080
new Criteria(path.toDotPath(ElasticsearchPersistentProperty.QueryPropertyToFieldNameConverter.INSTANCE)),
8181
iterator));
@@ -109,31 +109,27 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
109109
return criteria.is(parameters.next()).not();
110110
case REGEX:
111111
return criteria.expression(parameters.next().toString());
112-
case LIKE:
113-
case STARTING_WITH:
112+
case LIKE, STARTING_WITH:
114113
return criteria.startsWith(parameters.next().toString());
115114
case ENDING_WITH:
116115
return criteria.endsWith(parameters.next().toString());
117116
case CONTAINING:
118117
return criteria.contains(parameters.next().toString());
119118
case GREATER_THAN:
120119
return criteria.greaterThan(parameters.next());
121-
case AFTER:
122-
case GREATER_THAN_EQUAL:
120+
case AFTER, GREATER_THAN_EQUAL:
123121
return criteria.greaterThanEqual(parameters.next());
124122
case LESS_THAN:
125123
return criteria.lessThan(parameters.next());
126-
case BEFORE:
127-
case LESS_THAN_EQUAL:
124+
case BEFORE, LESS_THAN_EQUAL:
128125
return criteria.lessThanEqual(parameters.next());
129126
case BETWEEN:
130127
return criteria.between(parameters.next(), parameters.next());
131128
case IN:
132129
return criteria.in(asArray(parameters.next()));
133130
case NOT_IN:
134131
return criteria.notIn(asArray(parameters.next()));
135-
case SIMPLE_PROPERTY:
136-
case WITHIN: {
132+
case SIMPLE_PROPERTY, WITHIN: {
137133
Object firstParameter = parameters.next();
138134
Object secondParameter = null;
139135
if (type == Part.Type.SIMPLE_PROPERTY) {
@@ -154,40 +150,24 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
154150
secondParameter = parameters.next();
155151
}
156152

157-
if (firstParameter instanceof GeoPoint && secondParameter instanceof String)
158-
return criteria.within((GeoPoint) firstParameter, (String) secondParameter);
159-
160-
if (firstParameter instanceof Point && secondParameter instanceof Distance)
161-
return criteria.within((Point) firstParameter, (Distance) secondParameter);
162-
163-
if (firstParameter instanceof String && secondParameter instanceof String)
164-
return criteria.within((String) firstParameter, (String) secondParameter);
153+
return doWithinIfPossible(criteria, firstParameter, secondParameter);
165154
}
166155
case NEAR: {
167156
Object firstParameter = parameters.next();
168157

169-
if (firstParameter instanceof GeoBox) {
170-
return criteria.boundedBy((GeoBox) firstParameter);
158+
if (firstParameter instanceof GeoBox geoBox) {
159+
return criteria.boundedBy(geoBox);
171160
}
172161

173-
if (firstParameter instanceof Box) {
174-
return criteria.boundedBy(GeoBox.fromBox((Box) firstParameter));
162+
if (firstParameter instanceof Box box) {
163+
return criteria.boundedBy(GeoBox.fromBox(box));
175164
}
176165

177166
Object secondParameter = parameters.next();
178167

179-
// "near" query can be the same query as the "within" query
180-
if (firstParameter instanceof GeoPoint && secondParameter instanceof String)
181-
return criteria.within((GeoPoint) firstParameter, (String) secondParameter);
182-
183-
if (firstParameter instanceof Point && secondParameter instanceof Distance)
184-
return criteria.within((Point) firstParameter, (Distance) secondParameter);
185-
186-
if (firstParameter instanceof String && secondParameter instanceof String)
187-
return criteria.within((String) firstParameter, (String) secondParameter);
168+
return doWithinIfPossible(criteria, firstParameter, secondParameter);
188169
}
189-
case EXISTS:
190-
case IS_NOT_NULL:
170+
case EXISTS, IS_NOT_NULL:
191171
return criteria.exists();
192172
case IS_NULL:
193173
return criteria.not().exists();
@@ -200,6 +180,32 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
200180
}
201181
}
202182

183+
/**
184+
* Do a within query if possible, otherwise return the criteria unchanged.
185+
*
186+
* @param criteria must not be {@literal null}
187+
* @param firstParameter must not be {@literal null}
188+
* @param secondParameter must not be {@literal null}
189+
* @return the criteria with the within query applied if possible.
190+
* @author Junghoon Ban
191+
*/
192+
private Criteria doWithinIfPossible(Criteria criteria, Object firstParameter, Object secondParameter) {
193+
194+
if (firstParameter instanceof GeoPoint geoPoint && secondParameter instanceof String string) {
195+
return criteria.within(geoPoint, string);
196+
}
197+
198+
if (firstParameter instanceof Point point && secondParameter instanceof Distance distance) {
199+
return criteria.within(point, distance);
200+
}
201+
202+
if (firstParameter instanceof String firstString && secondParameter instanceof String secondString) {
203+
return criteria.within(firstString, secondString);
204+
}
205+
206+
return criteria;
207+
}
208+
203209
private Object[] asArray(Object o) {
204210
if (o instanceof Collection) {
205211
return ((Collection<?>) o).toArray();

0 commit comments

Comments
 (0)