Skip to content

Commit db6f723

Browse files
committed
Use pattern matching instead of type casting.
1 parent 05ca90e commit db6f723

File tree

5 files changed

+34
-29
lines changed

5 files changed

+34
-29
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

+17-16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @author Franck Marchand
4545
* @author Artur Konczak
4646
* @author Peter-Josef Meisch
47+
* @author Junghoon Ban
4748
*/
4849
public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuery, CriteriaQuery> {
4950

@@ -154,37 +155,37 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
154155
secondParameter = parameters.next();
155156
}
156157

157-
if (firstParameter instanceof GeoPoint && secondParameter instanceof String)
158-
return criteria.within((GeoPoint) firstParameter, (String) secondParameter);
158+
if (firstParameter instanceof GeoPoint geoPoint && secondParameter instanceof String string)
159+
return criteria.within(geoPoint, string);
159160

160-
if (firstParameter instanceof Point && secondParameter instanceof Distance)
161-
return criteria.within((Point) firstParameter, (Distance) secondParameter);
161+
if (firstParameter instanceof Point point && secondParameter instanceof Distance distance)
162+
return criteria.within(point, distance);
162163

163-
if (firstParameter instanceof String && secondParameter instanceof String)
164-
return criteria.within((String) firstParameter, (String) secondParameter);
164+
if (firstParameter instanceof String firstString && secondParameter instanceof String secondString)
165+
return criteria.within(firstString, secondString);
165166
}
166167
case NEAR: {
167168
Object firstParameter = parameters.next();
168169

169-
if (firstParameter instanceof GeoBox) {
170-
return criteria.boundedBy((GeoBox) firstParameter);
170+
if (firstParameter instanceof GeoBox geoBox) {
171+
return criteria.boundedBy(geoBox);
171172
}
172173

173-
if (firstParameter instanceof Box) {
174-
return criteria.boundedBy(GeoBox.fromBox((Box) firstParameter));
174+
if (firstParameter instanceof Box box) {
175+
return criteria.boundedBy(GeoBox.fromBox(box));
175176
}
176177

177178
Object secondParameter = parameters.next();
178179

179180
// "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);
181+
if (firstParameter instanceof GeoPoint geoPoint && secondParameter instanceof String string)
182+
return criteria.within(geoPoint, string);
182183

183-
if (firstParameter instanceof Point && secondParameter instanceof Distance)
184-
return criteria.within((Point) firstParameter, (Distance) secondParameter);
184+
if (firstParameter instanceof Point point && secondParameter instanceof Distance distance)
185+
return criteria.within(point, distance);
185186

186-
if (firstParameter instanceof String && secondParameter instanceof String)
187-
return criteria.within((String) firstParameter, (String) secondParameter);
187+
if (firstParameter instanceof String firstString && secondParameter instanceof String secondString)
188+
return criteria.within(firstString, secondString);
188189
}
189190
case EXISTS:
190191
case IS_NOT_NULL:

0 commit comments

Comments
 (0)