Skip to content

Commit

Permalink
Java 17 Pattern matching. (#2807)
Browse files Browse the repository at this point in the history
Fix checkstyle errors
  • Loading branch information
altro3 authored Apr 3, 2024
1 parent f838ed0 commit 06a5348
Show file tree
Hide file tree
Showing 78 changed files with 423 additions and 456 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public static CosmosAccessException cosmosAccessException(CosmosDiagnosticsProce
}
// Unwrap the exception in case if it is a reactive exception
final Throwable unwrappedThrowable = Exceptions.unwrap(throwable);
if (unwrappedThrowable instanceof CosmosException && cosmosDiagnosticsProcessor != null) {
CosmosException cosmosException = (CosmosException) unwrappedThrowable;
if (unwrappedThrowable instanceof CosmosException cosmosException && cosmosDiagnosticsProcessor != null) {
processDiagnostics(cosmosDiagnosticsProcessor, operationName, cosmosException.getDiagnostics(), cosmosException.getActivityId(),
cosmosException.getRequestCharge());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public String getUpdate() {
}

private <T, K> CosmosSqlStoredQuery<T, K> getCosmosSqlStoredQuery(StoredQuery<T, K> storedQuery) {
if (storedQuery instanceof CosmosSqlStoredQuery) {
return (CosmosSqlStoredQuery<T, K>) storedQuery;
if (storedQuery instanceof CosmosSqlStoredQuery<T, K> cosmosSqlStoredQuery) {
return cosmosSqlStoredQuery;
}
throw new IllegalStateException("Expected for stored query query to be of type: CosmosSqlStoredQuery got: " + sqlStoredQuery.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ public <E, R> StoredQuery<E, R> decorate(MethodInvocationContext<?, ?> context,
defaultCosmosSqlQueryBuilder = new CosmosSqlQueryBuilder(context.getAnnotationMetadata());
}
String update = null;
if (storedQuery instanceof QueryResultStoredQuery) {
update = ((QueryResultStoredQuery<E, R>) storedQuery).getQueryResult().getUpdate();
if (storedQuery instanceof QueryResultStoredQuery<E, R> queryResultStoredQuery) {
update = queryResultStoredQuery.getQueryResult().getUpdate();
}
RuntimePersistentEntity<E> runtimePersistentEntity = runtimeEntityRegistry.getEntity(storedQuery.getRootEntity());
return new CosmosSqlStoredQuery<>(storedQuery, runtimePersistentEntity, defaultCosmosSqlQueryBuilder, update);
Expand Down Expand Up @@ -561,15 +561,15 @@ private boolean isRawQuery(@NonNull PreparedQuery<?, ?> preparedQuery) {
}

private <E, R> SqlPreparedQuery<E, R> getSqlPreparedQuery(PreparedQuery<E, R> preparedQuery) {
if (preparedQuery instanceof SqlPreparedQuery) {
return (SqlPreparedQuery<E, R>) preparedQuery;
if (preparedQuery instanceof SqlPreparedQuery<E, R> sqlPreparedQuery) {
return sqlPreparedQuery;
}
throw new IllegalStateException("Expected for prepared query to be of type: SqlPreparedQuery got: " + preparedQuery.getClass().getName());
}

private <E, R> CosmosSqlPreparedQuery<E, R> getCosmosSqlPreparedQuery(PreparedQuery<E, R> preparedQuery) {
if (preparedQuery instanceof CosmosSqlPreparedQuery) {
return (CosmosSqlPreparedQuery<E, R>) preparedQuery;
if (preparedQuery instanceof CosmosSqlPreparedQuery<E, R> cosmosSqlPreparedQuery) {
return cosmosSqlPreparedQuery;
}
throw new IllegalStateException("Expected for prepared query to be of type: CosmosSqlPreparedQuery got: " + preparedQuery.getClass().getName());
}
Expand Down Expand Up @@ -896,8 +896,7 @@ private void setIfMatchETag(CosmosItemRequestOptions requestOptions, ObjectNode
}

private Throwable handleCosmosOperationException(String message, Throwable e, String operationName, RuntimePersistentEntity<?> persistentEntity) {
if (e instanceof CosmosException) {
CosmosException cosmosException = (CosmosException) e;
if (e instanceof CosmosException cosmosException) {
if (cosmosException.getStatusCode() == HttpResponseStatus.PRECONDITION_FAILED.code()) {
CosmosEntity cosmosEntity = CosmosEntity.get(persistentEntity);
if (cosmosEntity.getVersionField() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ private void initializeCriteriaHandlers() {
appendPropertyRef(whereClause, ctx.getAnnotationMetadata(), ctx.getPersistentEntity(), propertyPath);
whereClause.append(COMMA);
Object value = criterion.getValue();
if (value instanceof BindingParameter) {
ctx.pushParameter((BindingParameter) value, newBindingContext(propertyPath.getPropertyPath()));
if (value instanceof BindingParameter bindingParameter) {
ctx.pushParameter(bindingParameter, newBindingContext(propertyPath.getPropertyPath()));
} else {
asLiterals(ctx.query(), value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public final class MongoQueryBuilder implements QueryBuilder {
return;
}
if (criterion instanceof QueryModel.PropertyCriterion || criterion instanceof QueryModel.PropertyComparisonCriterion) {
Map<String, Object> neg = new LinkedHashMap<>();
var neg = new LinkedHashMap<String, Object>();
handleCriterion(ctx, neg, criterion);
if (neg.size() != 1) {
throw new IllegalStateException("Expected size of 1");
Expand Down Expand Up @@ -174,8 +174,8 @@ public final class MongoQueryBuilder implements QueryBuilder {
PersistentPropertyPath propertyPath = context.getRequiredProperty(criterion);
Object value = criterion.getValue();
String criterionPropertyName = getCriterionPropertyName(criterion.getProperty(), context);
if (value instanceof Iterable) {
List<?> values = CollectionUtils.iterableToList((Iterable) value);
if (value instanceof Iterable<?> iterable) {
List<?> values = CollectionUtils.iterableToList(iterable);
obj.put(criterionPropertyName, singletonMap("$in", values.stream().map(val -> valueRepresentation(context, propertyPath, val)).collect(Collectors.toList())));
} else {
obj.put(criterionPropertyName, singletonMap("$in", singletonList(valueRepresentation(context, propertyPath, value))));
Expand All @@ -185,8 +185,8 @@ public final class MongoQueryBuilder implements QueryBuilder {
PersistentPropertyPath propertyPath = context.getRequiredProperty(criterion);
Object value = criterion.getValue();
String criterionPropertyName = getCriterionPropertyName(criterion.getProperty(), context);
if (value instanceof Iterable) {
List<?> values = CollectionUtils.iterableToList((Iterable) value);
if (value instanceof Iterable<?> iterable) {
List<?> values = CollectionUtils.iterableToList(iterable);
obj.put(criterionPropertyName, singletonMap("$nin", values.stream().map(val -> valueRepresentation(context, propertyPath, val)).collect(Collectors.toList())));
} else {
obj.put(criterionPropertyName, singletonMap("$nin", singletonList(valueRepresentation(context, propertyPath, value))));
Expand Down Expand Up @@ -223,8 +223,8 @@ public final class MongoQueryBuilder implements QueryBuilder {
Object value = criterion.getValue();
String criterionPropertyName = getCriterionPropertyName(criterion.getProperty(), context);
Object criteriaValue;
if (value instanceof Iterable) {
List<?> values = CollectionUtils.iterableToList((Iterable) value);
if (value instanceof Iterable<?> iterable) {
List<?> values = CollectionUtils.iterableToList(iterable);
criteriaValue = values.stream().map(val -> valueRepresentation(context, propertyPath, val)).collect(Collectors.toList());
} else {
criteriaValue = singletonList(valueRepresentation(context, propertyPath, value));
Expand Down Expand Up @@ -275,9 +275,9 @@ private <T extends QueryModel.PropertyCriterion> void handleRegexPropertyExpress
Map<String, Object> regexCriteria = new HashMap<>(2);
regexCriteria.put(OPTIONS, ignoreCase ? "i" : "");
String regexValue;
if (value instanceof BindingParameter) {
if (value instanceof BindingParameter bindingParameter) {
int index = context.pushParameter(
(BindingParameter) value,
bindingParameter,
newBindingContext(propertyPath, propertyPath)
);
regexValue = QUERY_PARAMETER_PLACEHOLDER + ":" + index;
Expand Down Expand Up @@ -336,15 +336,15 @@ private Object valueRepresentation(CriteriaContext context,
PersistentPropertyPath inPropertyPath,
PersistentPropertyPath outPropertyPath,
Object value) {
if (value instanceof LocalDate) {
return singletonMap(MONGO_DATE_IDENTIFIER, formatDate((LocalDate) value));
if (value instanceof LocalDate localDate) {
return singletonMap(MONGO_DATE_IDENTIFIER, formatDate(localDate));
}
if (value instanceof LocalDateTime) {
return singletonMap(MONGO_DATE_IDENTIFIER, formatDate((LocalDateTime) value));
if (value instanceof LocalDateTime localDateTime) {
return singletonMap(MONGO_DATE_IDENTIFIER, formatDate(localDateTime));
}
if (value instanceof BindingParameter) {
if (value instanceof BindingParameter bindingParameter) {
int index = context.pushParameter(
(BindingParameter) value,
bindingParameter,
newBindingContext(inPropertyPath, outPropertyPath)
);
return singletonMap(QUERY_PARAMETER_PLACEHOLDER, index);
Expand Down Expand Up @@ -378,8 +378,8 @@ operator, asList(
}

private Object asLiteral(@Nullable Object value) {
if (value instanceof RegexPattern) {
return "'" + Pattern.quote(((RegexPattern) value).value) + "'";
if (value instanceof RegexPattern regexPattern) {
return "'" + Pattern.quote(regexPattern.value) + "'";
}
return value;
}
Expand Down Expand Up @@ -576,19 +576,19 @@ private void addLookups(Collection<JoinPath> joins, QueryState queryState) {
if (mappedByPath == null) {
throw new IllegalStateException("Cannot find mapped path: " + mappedBy);
}
if (!(mappedByPath.getProperty() instanceof Association)) {
if (!(mappedByPath.getProperty() instanceof Association associationProperty)) {
throw new IllegalStateException("Expected association as a mapped path: " + mappedBy);
}

List<String> localMatchFields = new ArrayList<>();
List<String> foreignMatchFields = new ArrayList<>();
var localMatchFields = new ArrayList<String>();
var foreignMatchFields = new ArrayList<String>();
traversePersistentProperties(currentLookup.persistentEntity.getIdentity(), (associations, p) -> {
String fieldPath = asPath(associations, p);
localMatchFields.add(fieldPath);
});

List<Association> mappedAssociations = new ArrayList<>(mappedByPath.getAssociations());
mappedAssociations.add((Association) mappedByPath.getProperty());
var mappedAssociations = new ArrayList<>(mappedByPath.getAssociations());
mappedAssociations.add(associationProperty);

traversePersistentProperties(mappedAssociations, currentLookup.persistentEntity.getIdentity(), (associations, p) -> {
String fieldPath = asPath(associations, p);
Expand All @@ -603,11 +603,11 @@ private void addLookups(Collection<JoinPath> joins, QueryState queryState) {
currentPath)
);
} else {
List<Association> mappedAssociations = new ArrayList<>(propertyPath.getAssociations());
var mappedAssociations = new ArrayList<>(propertyPath.getAssociations());
mappedAssociations.add((Association) propertyPath.getProperty());

List<String> localMatchFields = new ArrayList<>();
List<String> foreignMatchFields = new ArrayList<>();
var localMatchFields = new ArrayList<String>();
var foreignMatchFields = new ArrayList<String>();
PersistentProperty identity = lookupStage.persistentEntity.getIdentity();
if (identity == null) {
throw new IllegalStateException("Null identity of persistent entity: " + lookupStage.persistentEntity);
Expand Down Expand Up @@ -645,7 +645,7 @@ private List<String> resolveJoinTableJoinFields(AnnotationMetadata annotationMet
if (!joinColumns.isEmpty()) {
return joinColumns;
}
List<String> fields = new ArrayList<>();
var fields = new ArrayList<String>();
traversePersistentProperties(entity.getIdentity(), (associations, property) -> {
fields.add(asPath(associations, property));
});
Expand All @@ -662,7 +662,7 @@ private List<String> resolveJoinTableAssociatedFields(AnnotationMetadata annotat
if (identity == null) {
throw new MappingException("Cannot have a foreign key association without an ID on entity: " + entity.getName());
}
List<String> fields = new ArrayList<>();
var fields = new ArrayList<String>();
traversePersistentProperties(identity, (associations, property) -> {
fields.add(asPath(associations, property));
});
Expand All @@ -679,7 +679,7 @@ private String asPath(List<Association> associations, PersistentProperty propert
if (associations.isEmpty()) {
return getPropertyPersistName(property);
}
StringJoiner joiner = new StringJoiner(".");
var joiner = new StringJoiner(".");
for (Association association : associations) {
joiner.add(getPropertyPersistName(association));
}
Expand Down Expand Up @@ -934,9 +934,9 @@ public QueryResult buildUpdate(AnnotationMetadata annotationMetadata, QueryModel
for (Map.Entry<String, Object> e : propertiesToUpdate.entrySet()) {
PersistentPropertyPath propertyPath = findProperty(queryState, e.getKey(), null);
String propertyPersistName = getPropertyPersistName(propertyPath.getProperty());
if (e.getValue() instanceof BindingParameter) {
if (e.getValue() instanceof BindingParameter bindingParameter) {
int index = queryState.pushParameter(
(BindingParameter) e.getValue(),
bindingParameter,
newBindingContext(propertyPath)
);
sets.put(propertyPersistName, singletonMap(QUERY_PARAMETER_PLACEHOLDER, index));
Expand Down Expand Up @@ -1043,11 +1043,11 @@ private void appendMap(StringBuilder sb, Map<String, Object> map) {
}

private boolean skipValue(Object obj) {
if (obj instanceof Map) {
return ((Map<?, ?>) obj).isEmpty();
if (obj instanceof Map<?, ?> map) {
return map.isEmpty();
}
if (obj instanceof Collection) {
return ((Collection<?>) obj).isEmpty();
if (obj instanceof Collection<?> collection) {
return collection.isEmpty();
}
return false;
}
Expand All @@ -1065,22 +1065,20 @@ private void appendArray(StringBuilder sb, Collection<Object> collection) {
}

private void append(StringBuilder sb, Object obj) {
if (obj instanceof Map) {
appendMap(sb, (Map) obj);
} else if (obj instanceof Collection) {
appendArray(sb, (Collection<Object>) obj);
} else if (obj instanceof RawJsonValue) {
sb.append(((RawJsonValue) obj).value);
if (obj instanceof Map map) {
appendMap(sb, map);
} else if (obj instanceof Collection collection) {
appendArray(sb, collection);
} else if (obj instanceof RawJsonValue rawJsonValue) {
sb.append(rawJsonValue.value);
} else if (obj == null) {
sb.append("null");
} else if (obj instanceof Boolean) {
sb.append(obj.toString().toLowerCase(Locale.ROOT));
} else if (obj instanceof Number) {
sb.append(obj);
} else {
sb.append("'");
sb.append(obj);
sb.append("'");
sb.append('\'').append(obj).append('\'');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ private void removeAnnotation(AnnotationMetadata annotationMetadata, String anno
removeAnnotation(hierarchy.getRootMetadata(), annotation);
return;
}
if (annotationMetadata instanceof MutableAnnotationMetadata) {
((MutableAnnotationMetadata) annotationMetadata).removeAnnotation(annotation);
((MutableAnnotationMetadata) annotationMetadata).removeStereotype(annotation);
if (annotationMetadata instanceof MutableAnnotationMetadata mutableAnnotationMetadata) {
mutableAnnotationMetadata.removeAnnotation(annotation);
mutableAnnotationMetadata.removeStereotype(annotation);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ public void bindOne(QueryParameterBinding binding, Object value) {
Collection<Object> coll;
if (value == null) {
coll = Collections.emptyList();
} else if (value instanceof Collection) {
coll = (Collection<Object>) value;
} else if (value instanceof Collection collection) {
coll = collection;
} else {
coll = Arrays.asList((Object[]) value);
}
Expand Down Expand Up @@ -633,8 +633,8 @@ private <T> void bindCriteriaSort(CriteriaQuery<T> criteriaQuery, Root<?> root,
}

private <E, R> BindableParametersPreparedQuery<E, R> getBindableParametersPreparedQuery(PreparedQuery<E, R> preparedQuery) {
if (preparedQuery instanceof BindableParametersPreparedQuery) {
return (BindableParametersPreparedQuery<E, R>) preparedQuery;
if (preparedQuery instanceof BindableParametersPreparedQuery<E, R> bindableParametersPreparedQuery) {
return bindableParametersPreparedQuery;
}
throw new IllegalStateException("Expected for prepared query to be of type: BindableParametersPreparedQuery");
}
Expand Down
Loading

0 comments on commit 06a5348

Please sign in to comment.