mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-23 12:32:10 +00:00
Use pattern matching instead of type casting.
Original Pull Request #2742 Closes #2741
This commit is contained in:
parent
64ada11462
commit
ba446845e3
@ -115,8 +115,8 @@ final class DocumentAdapters {
|
|||||||
if (source == null) {
|
if (source == null) {
|
||||||
document = Document.from(hitFieldsAsMap);
|
document = Document.from(hitFieldsAsMap);
|
||||||
} else {
|
} else {
|
||||||
if (source instanceof EntityAsMap) {
|
if (source instanceof EntityAsMap entityAsMap) {
|
||||||
document = Document.from((EntityAsMap) source);
|
document = Document.from(entityAsMap);
|
||||||
} else if (source instanceof JsonData jsonData) {
|
} else if (source instanceof JsonData jsonData) {
|
||||||
document = Document.from(jsonData.to(EntityAsMap.class));
|
document = Document.from(jsonData.to(EntityAsMap.class));
|
||||||
} else {
|
} else {
|
||||||
|
@ -143,8 +143,8 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
|||||||
setEntityCallbacks(EntityCallbacks.create(applicationContext));
|
setEntityCallbacks(EntityCallbacks.create(applicationContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (elasticsearchConverter instanceof ApplicationContextAware) {
|
if (elasticsearchConverter instanceof ApplicationContextAware contextAware) {
|
||||||
((ApplicationContextAware) elasticsearchConverter).setApplicationContext(applicationContext);
|
contextAware.setApplicationContext(applicationContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,8 +229,8 @@ public class SearchHitMapping<T> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
String scrollId = null;
|
String scrollId = null;
|
||||||
if (searchHits instanceof SearchHitsImpl) {
|
if (searchHits instanceof SearchHitsImpl<?> searchHitsImpl) {
|
||||||
scrollId = ((SearchHitsImpl<?>) searchHits).getScrollId();
|
scrollId = searchHitsImpl.getScrollId();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SearchHitsImpl<>(searchHits.getTotalHits(), //
|
return new SearchHitsImpl<>(searchHits.getTotalHits(), //
|
||||||
|
@ -323,9 +323,9 @@ final public class ElasticsearchDateConverter {
|
|||||||
try {
|
try {
|
||||||
return dateTimeFormatter.format(accessor);
|
return dateTimeFormatter.format(accessor);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (accessor instanceof Instant) {
|
if (accessor instanceof Instant instant) {
|
||||||
// as alternatives try to format a ZonedDateTime or LocalDateTime
|
// as alternatives try to format a ZonedDateTime or LocalDateTime
|
||||||
return dateTimeFormatter.format(ZonedDateTime.ofInstant((Instant) accessor, ZoneId.of("UTC")));
|
return dateTimeFormatter.format(ZonedDateTime.ofInstant(instant, ZoneId.of("UTC")));
|
||||||
} else {
|
} else {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
@ -141,20 +141,20 @@ public class GeoConverters {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> convert(GeoJson<? extends Iterable<?>> source) {
|
public Map<String, Object> convert(GeoJson<? extends Iterable<?>> source) {
|
||||||
if (source instanceof GeoJsonPoint) {
|
if (source instanceof GeoJsonPoint geoJsonPoint) {
|
||||||
return GeoJsonPointToMapConverter.INSTANCE.convert((GeoJsonPoint) source);
|
return GeoJsonPointToMapConverter.INSTANCE.convert(geoJsonPoint);
|
||||||
} else if (source instanceof GeoJsonMultiPoint) {
|
} else if (source instanceof GeoJsonMultiPoint geoJsonMultiPoint) {
|
||||||
return GeoJsonMultiPointToMapConverter.INSTANCE.convert((GeoJsonMultiPoint) source);
|
return GeoJsonMultiPointToMapConverter.INSTANCE.convert(geoJsonMultiPoint);
|
||||||
} else if (source instanceof GeoJsonLineString) {
|
} else if (source instanceof GeoJsonLineString geoJsonLineString) {
|
||||||
return GeoJsonLineStringToMapConverter.INSTANCE.convert((GeoJsonLineString) source);
|
return GeoJsonLineStringToMapConverter.INSTANCE.convert(geoJsonLineString);
|
||||||
} else if (source instanceof GeoJsonMultiLineString) {
|
} else if (source instanceof GeoJsonMultiLineString geoJsonMultiLineString) {
|
||||||
return GeoJsonMultiLineStringToMapConverter.INSTANCE.convert((GeoJsonMultiLineString) source);
|
return GeoJsonMultiLineStringToMapConverter.INSTANCE.convert(geoJsonMultiLineString);
|
||||||
} else if (source instanceof GeoJsonPolygon) {
|
} else if (source instanceof GeoJsonPolygon geoJsonPolygon) {
|
||||||
return GeoJsonPolygonToMapConverter.INSTANCE.convert((GeoJsonPolygon) source);
|
return GeoJsonPolygonToMapConverter.INSTANCE.convert(geoJsonPolygon);
|
||||||
} else if (source instanceof GeoJsonMultiPolygon) {
|
} else if (source instanceof GeoJsonMultiPolygon geoJsonMultiPolygon) {
|
||||||
return GeoJsonMultiPolygonToMapConverter.INSTANCE.convert((GeoJsonMultiPolygon) source);
|
return GeoJsonMultiPolygonToMapConverter.INSTANCE.convert(geoJsonMultiPolygon);
|
||||||
} else if (source instanceof GeoJsonGeometryCollection) {
|
} else if (source instanceof GeoJsonGeometryCollection geoJsonGeometryCollection) {
|
||||||
return GeoJsonGeometryCollectionToMapConverter.INSTANCE.convert((GeoJsonGeometryCollection) source);
|
return GeoJsonGeometryCollectionToMapConverter.INSTANCE.convert(geoJsonGeometryCollection);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("unknown GeoJson class " + source.getClass().getSimpleName());
|
throw new IllegalArgumentException("unknown GeoJson class " + source.getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
|
@ -116,8 +116,8 @@ public class MappingElasticsearchConverter
|
|||||||
@Override
|
@Override
|
||||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
|
||||||
if (mappingContext instanceof ApplicationContextAware) {
|
if (mappingContext instanceof ApplicationContextAware contextAware) {
|
||||||
((ApplicationContextAware) mappingContext).setApplicationContext(applicationContext);
|
contextAware.setApplicationContext(applicationContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1186,8 +1186,8 @@ public class MappingElasticsearchConverter
|
|||||||
*/
|
*/
|
||||||
private static Collection<?> asCollection(Object source) {
|
private static Collection<?> asCollection(Object source) {
|
||||||
|
|
||||||
if (source instanceof Collection) {
|
if (source instanceof Collection<?> collection) {
|
||||||
return (Collection<?>) source;
|
return collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source);
|
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source);
|
||||||
@ -1201,9 +1201,9 @@ public class MappingElasticsearchConverter
|
|||||||
|
|
||||||
Assert.notNull(query, "query must not be null");
|
Assert.notNull(query, "query must not be null");
|
||||||
|
|
||||||
if (query instanceof BaseQuery) {
|
if (query instanceof BaseQuery baseQuery) {
|
||||||
|
|
||||||
if (((BaseQuery) query).queryIsUpdatedByConverter()) {
|
if (baseQuery.queryIsUpdatedByConverter()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1214,12 +1214,12 @@ public class MappingElasticsearchConverter
|
|||||||
|
|
||||||
updatePropertiesInFieldsAndSourceFilter(query, domainClass);
|
updatePropertiesInFieldsAndSourceFilter(query, domainClass);
|
||||||
|
|
||||||
if (query instanceof CriteriaQuery) {
|
if (query instanceof CriteriaQuery criteriaQuery) {
|
||||||
updatePropertiesInCriteriaQuery((CriteriaQuery) query, domainClass);
|
updatePropertiesInCriteriaQuery(criteriaQuery, domainClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (query instanceof BaseQuery) {
|
if (query instanceof BaseQuery baseQuery) {
|
||||||
((BaseQuery) query).setQueryIsUpdatedByConverter(true);
|
baseQuery.setQueryIsUpdatedByConverter(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,8 +194,8 @@ public class MappingBuilder {
|
|||||||
if (writeTypeHints) {
|
if (writeTypeHints) {
|
||||||
String typeHintProperty = null;
|
String typeHintProperty = null;
|
||||||
|
|
||||||
if (elasticsearchConverter instanceof MappingElasticsearchConverter) {
|
if (elasticsearchConverter instanceof MappingElasticsearchConverter mappingElasticsearchConverter) {
|
||||||
typeHintProperty = ((MappingElasticsearchConverter) elasticsearchConverter).getTypeMapper().getTypeKey();
|
typeHintProperty = mappingElasticsearchConverter.getTypeMapper().getTypeKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeHintProperty == null) {
|
if (typeHintProperty == null) {
|
||||||
|
@ -125,10 +125,10 @@ public final class MappingParameters {
|
|||||||
|
|
||||||
Assert.notNull(annotation, "annotation must not be null!");
|
Assert.notNull(annotation, "annotation must not be null!");
|
||||||
|
|
||||||
if (annotation instanceof Field) {
|
if (annotation instanceof Field field) {
|
||||||
return new MappingParameters((Field) annotation);
|
return new MappingParameters(field);
|
||||||
} else if (annotation instanceof InnerField) {
|
} else if (annotation instanceof InnerField innerField) {
|
||||||
return new MappingParameters((InnerField) annotation);
|
return new MappingParameters(innerField);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("annotation must be an instance of @Field or @InnerField");
|
throw new IllegalArgumentException("annotation must be an instance of @Field or @InnerField");
|
||||||
}
|
}
|
||||||
|
@ -344,8 +344,8 @@ public class SimpleElasticsearchPersistentProperty extends
|
|||||||
private FieldNamingStrategy getFieldNamingStrategy() {
|
private FieldNamingStrategy getFieldNamingStrategy() {
|
||||||
PersistentEntity<?, ElasticsearchPersistentProperty> owner = getOwner();
|
PersistentEntity<?, ElasticsearchPersistentProperty> owner = getOwner();
|
||||||
|
|
||||||
if (owner instanceof ElasticsearchPersistentEntity) {
|
if (owner instanceof ElasticsearchPersistentEntity<?> persistentEntity) {
|
||||||
return ((ElasticsearchPersistentEntity<?>) owner).getFieldNamingStrategy();
|
return persistentEntity.getFieldNamingStrategy();
|
||||||
}
|
}
|
||||||
|
|
||||||
return DEFAULT_FIELD_NAMING_STRATEGY;
|
return DEFAULT_FIELD_NAMING_STRATEGY;
|
||||||
|
@ -386,8 +386,8 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
|||||||
private void doRefresh() {
|
private void doRefresh() {
|
||||||
RefreshPolicy refreshPolicy = null;
|
RefreshPolicy refreshPolicy = null;
|
||||||
|
|
||||||
if (operations instanceof AbstractElasticsearchTemplate) {
|
if (operations instanceof AbstractElasticsearchTemplate template) {
|
||||||
refreshPolicy = ((AbstractElasticsearchTemplate) operations).getRefreshPolicy();
|
refreshPolicy = template.getRefreshPolicy();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (refreshPolicy == null) {
|
if (refreshPolicy == null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user