Skip to content

Use pattern matching instead of type casting. #2784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* filter.
*
* @author Peter-Josef Meisch
* @author Junghoon Ban
* @since 4.4
*/
class CriteriaFilterProcessor {
Expand Down Expand Up @@ -169,7 +170,7 @@ private static ObjectBuilder<GeoDistanceQuery> withinQuery(String fieldName, Obj
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) //
Expand All @@ -178,8 +179,8 @@ private static ObjectBuilder<GeoDistanceQuery> withinQuery(String fieldName, Obj
.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];
Expand Down Expand Up @@ -220,8 +221,8 @@ private static void oneParameterBBox(GeoBoundingBoxQuery.Builder queryBuilder, O
"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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* 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 {
Expand All @@ -59,7 +60,7 @@ public ElasticsearchExceptionTranslator(JsonpMapper jsonpMapper) {
*/
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
*
* @author Peter-Josef Meisch
* @author Illia Ulianov
* @author Junghoon Ban
* @since 4.4
*/
public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearchTemplate {
Expand Down Expand Up @@ -645,7 +646,7 @@ public <T> Publisher<T> execute(ReactiveElasticsearchTemplate.ClientCallback<Pub
*/
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
* @author Marc Vanbrabant
* @author Anton Naydenov
* @author vdisk
* @author Junghoon Ban
* @since 3.2
*/
public class MappingElasticsearchConverter
Expand Down Expand Up @@ -503,16 +504,16 @@ TypeInformation<?> getCollectionComponentType(TypeInformation<?> type) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

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;
Expand All @@ -44,6 +43,7 @@
* @author Franck Marchand
* @author Artur Konczak
* @author Peter-Josef Meisch
* @author Junghoon Ban
*/
public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuery, CriteriaQuery> {

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

@Override
protected CriteriaQuery create(Part part, Iterator<Object> iterator) {
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context
.getPersistentPropertyPath(part.getProperty());
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context.getPersistentPropertyPath(
part.getProperty());
return new CriteriaQuery(from(part,
new Criteria(path.toDotPath(ElasticsearchPersistentProperty.QueryPropertyToFieldNameConverter.INSTANCE)),
iterator));
Expand All @@ -74,8 +74,8 @@ protected CriteriaQuery and(Part part, CriteriaQuery base, Iterator<Object> iter
if (base == null) {
return create(part, iterator);
}
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context
.getPersistentPropertyPath(part.getProperty());
PersistentPropertyPath<ElasticsearchPersistentProperty> path = context.getPersistentPropertyPath(
part.getProperty());
return base.addCriteria(from(part,
new Criteria(path.toDotPath(ElasticsearchPersistentProperty.QueryPropertyToFieldNameConverter.INSTANCE)),
iterator));
Expand Down Expand Up @@ -109,31 +109,27 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
return criteria.is(parameters.next()).not();
case REGEX:
return criteria.expression(parameters.next().toString());
case LIKE:
case STARTING_WITH:
case LIKE, STARTING_WITH:
return criteria.startsWith(parameters.next().toString());
case ENDING_WITH:
return criteria.endsWith(parameters.next().toString());
case CONTAINING:
return criteria.contains(parameters.next().toString());
case GREATER_THAN:
return criteria.greaterThan(parameters.next());
case AFTER:
case GREATER_THAN_EQUAL:
case AFTER, GREATER_THAN_EQUAL:
return criteria.greaterThanEqual(parameters.next());
case LESS_THAN:
return criteria.lessThan(parameters.next());
case BEFORE:
case LESS_THAN_EQUAL:
case BEFORE, LESS_THAN_EQUAL:
return criteria.lessThanEqual(parameters.next());
case BETWEEN:
return criteria.between(parameters.next(), parameters.next());
case IN:
return criteria.in(asArray(parameters.next()));
case NOT_IN:
return criteria.notIn(asArray(parameters.next()));
case SIMPLE_PROPERTY:
case WITHIN: {
case SIMPLE_PROPERTY, WITHIN: {
Object firstParameter = parameters.next();
Object secondParameter = null;
if (type == Part.Type.SIMPLE_PROPERTY) {
Expand All @@ -154,40 +150,24 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
secondParameter = parameters.next();
}

if (firstParameter instanceof GeoPoint && secondParameter instanceof String)
return criteria.within((GeoPoint) firstParameter, (String) secondParameter);

if (firstParameter instanceof Point && secondParameter instanceof Distance)
return criteria.within((Point) firstParameter, (Distance) secondParameter);

if (firstParameter instanceof String && secondParameter instanceof String)
return criteria.within((String) firstParameter, (String) secondParameter);
return doWithinIfPossible(criteria, firstParameter, secondParameter);
}
case NEAR: {
Object firstParameter = parameters.next();

if (firstParameter instanceof GeoBox) {
return criteria.boundedBy((GeoBox) firstParameter);
if (firstParameter instanceof GeoBox geoBox) {
return criteria.boundedBy(geoBox);
}

if (firstParameter instanceof Box) {
return criteria.boundedBy(GeoBox.fromBox((Box) firstParameter));
if (firstParameter instanceof Box box) {
return criteria.boundedBy(GeoBox.fromBox(box));
}

Object secondParameter = parameters.next();

// "near" query can be the same query as the "within" query
if (firstParameter instanceof GeoPoint && secondParameter instanceof String)
return criteria.within((GeoPoint) firstParameter, (String) secondParameter);

if (firstParameter instanceof Point && secondParameter instanceof Distance)
return criteria.within((Point) firstParameter, (Distance) secondParameter);

if (firstParameter instanceof String && secondParameter instanceof String)
return criteria.within((String) firstParameter, (String) secondParameter);
return doWithinIfPossible(criteria, firstParameter, secondParameter);
}
case EXISTS:
case IS_NOT_NULL:
case EXISTS, IS_NOT_NULL:
return criteria.exists();
case IS_NULL:
return criteria.not().exists();
Expand All @@ -200,6 +180,32 @@ private Criteria from(Part part, Criteria criteria, Iterator<?> parameters) {
}
}

/**
* Do a within query if possible, otherwise return the criteria unchanged.
*
* @param criteria must not be {@literal null}
* @param firstParameter must not be {@literal null}
* @param secondParameter must not be {@literal null}
* @return the criteria with the within query applied if possible.
* @author Junghoon Ban
*/
private Criteria doWithinIfPossible(Criteria criteria, Object firstParameter, Object secondParameter) {

if (firstParameter instanceof GeoPoint geoPoint && secondParameter instanceof String string) {
return criteria.within(geoPoint, string);
}

if (firstParameter instanceof Point point && secondParameter instanceof Distance distance) {
return criteria.within(point, distance);
}

if (firstParameter instanceof String firstString && secondParameter instanceof String secondString) {
return criteria.within(firstString, secondString);
}

return criteria;
}

private Object[] asArray(Object o) {
if (o instanceof Collection) {
return ((Collection<?>) o).toArray();
Expand Down