Skip to content

Commit e4739d5

Browse files
committed
Use pattern matching instead of type casting.
1 parent 64ada11 commit e4739d5

14 files changed

+85
-71
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/DocumentAdapters.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* {@link org.springframework.data.elasticsearch.core.document.Document}
5050
*
5151
* @author Peter-Josef Meisch
52+
* @author Junghoon Ban
5253
* @since 4.4
5354
*/
5455
final class DocumentAdapters {
@@ -115,8 +116,8 @@ public static SearchDocument from(Hit<?> hit, JsonpMapper jsonpMapper) {
115116
if (source == null) {
116117
document = Document.from(hitFieldsAsMap);
117118
} else {
118-
if (source instanceof EntityAsMap) {
119-
document = Document.from((EntityAsMap) source);
119+
if (source instanceof EntityAsMap entityAsMap) {
120+
document = Document.from(entityAsMap);
120121
} else if (source instanceof JsonData jsonData) {
121122
document = Document.from(jsonData.to(EntityAsMap.class));
122123
} else {

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 {
@@ -642,7 +643,7 @@ public <T> Publisher<T> execute(ReactiveElasticsearchTemplate.ClientCallback<Pub
642643
*/
643644
private RuntimeException translateException(Throwable throwable) {
644645

645-
RuntimeException runtimeException = throwable instanceof RuntimeException ? (RuntimeException) throwable
646+
RuntimeException runtimeException = throwable instanceof RuntimeException ex ? ex
646647
: new RuntimeException(throwable.getMessage(), throwable);
647648
RuntimeException potentiallyTranslatedException = exceptionTranslator
648649
.translateExceptionIfPossible(runtimeException);

src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
* @author Steven Pearce
8080
* @author Anton Naydenov
8181
* @author Haibo Liu
82+
* @author Junghoon Ban
8283
*/
8384
public abstract class AbstractElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
8485

@@ -143,8 +144,8 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
143144
setEntityCallbacks(EntityCallbacks.create(applicationContext));
144145
}
145146

146-
if (elasticsearchConverter instanceof ApplicationContextAware) {
147-
((ApplicationContextAware) elasticsearchConverter).setApplicationContext(applicationContext);
147+
if (elasticsearchConverter instanceof ApplicationContextAware contextAware) {
148+
contextAware.setApplicationContext(applicationContext);
148149
}
149150
}
150151

src/main/java/org/springframework/data/elasticsearch/core/SearchHitMapping.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @author Matt Gilene
4747
* @author Sascha Woo
4848
* @author Jakob Hoeper
49+
* @author Junghoon Ban
4950
* @since 4.0
5051
*/
5152
public class SearchHitMapping<T> {
@@ -229,8 +230,8 @@ private SearchHits<?> mapInnerDocuments(SearchHits<SearchDocument> searchHits, C
229230
});
230231

231232
String scrollId = null;
232-
if (searchHits instanceof SearchHitsImpl) {
233-
scrollId = ((SearchHitsImpl<?>) searchHits).getScrollId();
233+
if (searchHits instanceof SearchHitsImpl<?> searchHitsImpl) {
234+
scrollId = searchHitsImpl.getScrollId();
234235
}
235236

236237
return new SearchHitsImpl<>(searchHits.getTotalHits(), //

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* understands.
3838
*
3939
* @author Peter-Josef Meisch
40+
* @author Junghoon Ban
4041
* @since 4.0
4142
*/
4243
final public class ElasticsearchDateConverter {
@@ -323,9 +324,9 @@ public String format(TemporalAccessor accessor) {
323324
try {
324325
return dateTimeFormatter.format(accessor);
325326
} catch (Exception e) {
326-
if (accessor instanceof Instant) {
327+
if (accessor instanceof Instant instant) {
327328
// as alternatives try to format a ZonedDateTime or LocalDateTime
328-
return dateTimeFormatter.format(ZonedDateTime.ofInstant((Instant) accessor, ZoneId.of("UTC")));
329+
return dateTimeFormatter.format(ZonedDateTime.ofInstant(instant, ZoneId.of("UTC")));
329330
} else {
330331
throw e;
331332
}

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*
4545
* @author Christoph Strobl
4646
* @author Peter-Josef Meisch
47+
* @author Junghoon Ban
4748
* @since 3.2
4849
*/
4950
public class GeoConverters {
@@ -141,20 +142,20 @@ public enum GeoJsonToMapConverter implements Converter<GeoJson<? extends Iterabl
141142

142143
@Override
143144
public Map<String, Object> convert(GeoJson<? extends Iterable<?>> source) {
144-
if (source instanceof GeoJsonPoint) {
145-
return GeoJsonPointToMapConverter.INSTANCE.convert((GeoJsonPoint) source);
146-
} else if (source instanceof GeoJsonMultiPoint) {
147-
return GeoJsonMultiPointToMapConverter.INSTANCE.convert((GeoJsonMultiPoint) source);
148-
} else if (source instanceof GeoJsonLineString) {
149-
return GeoJsonLineStringToMapConverter.INSTANCE.convert((GeoJsonLineString) source);
150-
} else if (source instanceof GeoJsonMultiLineString) {
151-
return GeoJsonMultiLineStringToMapConverter.INSTANCE.convert((GeoJsonMultiLineString) source);
152-
} else if (source instanceof GeoJsonPolygon) {
153-
return GeoJsonPolygonToMapConverter.INSTANCE.convert((GeoJsonPolygon) source);
154-
} else if (source instanceof GeoJsonMultiPolygon) {
155-
return GeoJsonMultiPolygonToMapConverter.INSTANCE.convert((GeoJsonMultiPolygon) source);
156-
} else if (source instanceof GeoJsonGeometryCollection) {
157-
return GeoJsonGeometryCollectionToMapConverter.INSTANCE.convert((GeoJsonGeometryCollection) source);
145+
if (source instanceof GeoJsonPoint geoJsonPoint) {
146+
return GeoJsonPointToMapConverter.INSTANCE.convert(geoJsonPoint);
147+
} else if (source instanceof GeoJsonMultiPoint geoJsonMultiPoint) {
148+
return GeoJsonMultiPointToMapConverter.INSTANCE.convert(geoJsonMultiPoint);
149+
} else if (source instanceof GeoJsonLineString geoJsonLineString) {
150+
return GeoJsonLineStringToMapConverter.INSTANCE.convert(geoJsonLineString);
151+
} else if (source instanceof GeoJsonMultiLineString geoJsonMultiLineString) {
152+
return GeoJsonMultiLineStringToMapConverter.INSTANCE.convert(geoJsonMultiLineString);
153+
} else if (source instanceof GeoJsonPolygon geoJsonPolygon) {
154+
return GeoJsonPolygonToMapConverter.INSTANCE.convert(geoJsonPolygon);
155+
} else if (source instanceof GeoJsonMultiPolygon geoJsonMultiPolygon) {
156+
return GeoJsonMultiPolygonToMapConverter.INSTANCE.convert(geoJsonMultiPolygon);
157+
} else if (source instanceof GeoJsonGeometryCollection geoJsonGeometryCollection) {
158+
return GeoJsonGeometryCollectionToMapConverter.INSTANCE.convert(geoJsonGeometryCollection);
158159
} else {
159160
throw new IllegalArgumentException("unknown GeoJson class " + source.getClass().getSimpleName());
160161
}

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

+17-16
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
@@ -116,8 +117,8 @@ public MappingElasticsearchConverter(
116117
@Override
117118
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
118119

119-
if (mappingContext instanceof ApplicationContextAware) {
120-
((ApplicationContextAware) mappingContext).setApplicationContext(applicationContext);
120+
if (mappingContext instanceof ApplicationContextAware contextAware) {
121+
contextAware.setApplicationContext(applicationContext);
121122
}
122123
}
123124

@@ -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);
@@ -1186,8 +1187,8 @@ protected Map<String, Object> createMap(Map<?, ?> map, ElasticsearchPersistentPr
11861187
*/
11871188
private static Collection<?> asCollection(Object source) {
11881189

1189-
if (source instanceof Collection) {
1190-
return (Collection<?>) source;
1190+
if (source instanceof Collection<?> collection) {
1191+
return collection;
11911192
}
11921193

11931194
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source);
@@ -1201,9 +1202,9 @@ public void updateQuery(Query query, @Nullable Class<?> domainClass) {
12011202

12021203
Assert.notNull(query, "query must not be null");
12031204

1204-
if (query instanceof BaseQuery) {
1205+
if (query instanceof BaseQuery baseQuery) {
12051206

1206-
if (((BaseQuery) query).queryIsUpdatedByConverter()) {
1207+
if (baseQuery.queryIsUpdatedByConverter()) {
12071208
return;
12081209
}
12091210
}
@@ -1214,12 +1215,12 @@ public void updateQuery(Query query, @Nullable Class<?> domainClass) {
12141215

12151216
updatePropertiesInFieldsAndSourceFilter(query, domainClass);
12161217

1217-
if (query instanceof CriteriaQuery) {
1218-
updatePropertiesInCriteriaQuery((CriteriaQuery) query, domainClass);
1218+
if (query instanceof CriteriaQuery criteriaQuery) {
1219+
updatePropertiesInCriteriaQuery(criteriaQuery, domainClass);
12191220
}
12201221

1221-
if (query instanceof BaseQuery) {
1222-
((BaseQuery) query).setQueryIsUpdatedByConverter(true);
1222+
if (query instanceof BaseQuery baseQuery) {
1223+
baseQuery.setQueryIsUpdatedByConverter(true);
12231224
}
12241225
}
12251226

src/main/java/org/springframework/data/elasticsearch/core/index/MappingBuilder.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* @author Peter-Josef Meisch
7070
* @author Xiao Yu
7171
* @author Subhobrata Dey
72+
* @author Junghoon Ban
7273
*/
7374
public class MappingBuilder {
7475

@@ -194,8 +195,8 @@ private void writeTypeHintMapping(ObjectNode propertiesNode) throws IOException
194195
if (writeTypeHints) {
195196
String typeHintProperty = null;
196197

197-
if (elasticsearchConverter instanceof MappingElasticsearchConverter) {
198-
typeHintProperty = ((MappingElasticsearchConverter) elasticsearchConverter).getTypeMapper().getTypeKey();
198+
if (elasticsearchConverter instanceof MappingElasticsearchConverter mappingElasticsearchConverter) {
199+
typeHintProperty = mappingElasticsearchConverter.getTypeMapper().getTypeKey();
199200
}
200201

201202
if (typeHintProperty == null) {

src/main/java/org/springframework/data/elasticsearch/core/index/MappingParameters.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Brian Kimmig
5050
* @author Morgan Lutz
5151
* @author Sascha Woo
52+
* @author Junghoon Ban
5253
* @since 4.0
5354
*/
5455
public final class MappingParameters {
@@ -125,10 +126,10 @@ public static MappingParameters from(Annotation annotation) {
125126

126127
Assert.notNull(annotation, "annotation must not be null!");
127128

128-
if (annotation instanceof Field) {
129-
return new MappingParameters((Field) annotation);
130-
} else if (annotation instanceof InnerField) {
131-
return new MappingParameters((InnerField) annotation);
129+
if (annotation instanceof Field field) {
130+
return new MappingParameters(field);
131+
} else if (annotation instanceof InnerField innerField) {
132+
return new MappingParameters(innerField);
132133
} else {
133134
throw new IllegalArgumentException("annotation must be an instance of @Field or @InnerField");
134135
}

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
* @author Oliver Gierke
7272
* @author Peter-Josef Meisch
7373
* @author Roman Puchkovskiy
74+
* @author Junghoon Ban
7475
*/
7576
public class SimpleElasticsearchPersistentProperty extends
7677
AnnotationBasedPersistentProperty<ElasticsearchPersistentProperty> implements ElasticsearchPersistentProperty {
@@ -344,8 +345,8 @@ public String getFieldName() {
344345
private FieldNamingStrategy getFieldNamingStrategy() {
345346
PersistentEntity<?, ElasticsearchPersistentProperty> owner = getOwner();
346347

347-
if (owner instanceof ElasticsearchPersistentEntity) {
348-
return ((ElasticsearchPersistentEntity<?>) owner).getFieldNamingStrategy();
348+
if (owner instanceof ElasticsearchPersistentEntity<?> persistentEntity) {
349+
return persistentEntity.getFieldNamingStrategy();
349350
}
350351

351352
return DEFAULT_FIELD_NAMING_STRATEGY;

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)