Refactor code with instanceof pattern variable.

In some cases, we currently use the traditional `instanceof` checks followed by explicit type casting.
With the introduction of pattern matching in recent Java versions, we can refactor these checks to make the code more concise and readable.

Original pull request #1868
This commit is contained in:
arefbehboudi
2024-08-29 18:26:23 +03:30
committed by Jens Schauder
parent 9b0b91bfd0
commit c8b9697c51
9 changed files with 47 additions and 57 deletions

View File

@@ -81,32 +81,32 @@ class AggregateChangeExecutor {
private void execute(DbAction<?> action, JdbcAggregateChangeExecutionContext executionContext) {
try {
if (action instanceof DbAction.InsertRoot) {
executionContext.executeInsertRoot((DbAction.InsertRoot<?>) action);
} else if (action instanceof DbAction.BatchInsertRoot<?>) {
executionContext.executeBatchInsertRoot((DbAction.BatchInsertRoot<?>) action);
} else if (action instanceof DbAction.Insert) {
executionContext.executeInsert((DbAction.Insert<?>) action);
} else if (action instanceof DbAction.BatchInsert) {
executionContext.executeBatchInsert((DbAction.BatchInsert<?>) action);
} else if (action instanceof DbAction.UpdateRoot) {
executionContext.executeUpdateRoot((DbAction.UpdateRoot<?>) action);
} else if (action instanceof DbAction.Delete) {
executionContext.executeDelete((DbAction.Delete<?>) action);
} else if (action instanceof DbAction.BatchDelete<?>) {
executionContext.executeBatchDelete((DbAction.BatchDelete<?>) action);
} else if (action instanceof DbAction.DeleteAll) {
executionContext.executeDeleteAll((DbAction.DeleteAll<?>) action);
} else if (action instanceof DbAction.DeleteRoot) {
executionContext.executeDeleteRoot((DbAction.DeleteRoot<?>) action);
} else if (action instanceof DbAction.BatchDeleteRoot) {
executionContext.executeBatchDeleteRoot((DbAction.BatchDeleteRoot<?>) action);
} else if (action instanceof DbAction.DeleteAllRoot) {
executionContext.executeDeleteAllRoot((DbAction.DeleteAllRoot<?>) action);
} else if (action instanceof DbAction.AcquireLockRoot) {
executionContext.executeAcquireLock((DbAction.AcquireLockRoot<?>) action);
} else if (action instanceof DbAction.AcquireLockAllRoot) {
executionContext.executeAcquireLockAllRoot((DbAction.AcquireLockAllRoot<?>) action);
if (action instanceof DbAction.InsertRoot<?> insertRoot) {
executionContext.executeInsertRoot(insertRoot);
} else if (action instanceof DbAction.BatchInsertRoot<?> batchInsertRoot) {
executionContext.executeBatchInsertRoot(batchInsertRoot);
} else if (action instanceof DbAction.Insert<?> insert) {
executionContext.executeInsert(insert);
} else if (action instanceof DbAction.BatchInsert<?> batchInsert) {
executionContext.executeBatchInsert(batchInsert);
} else if (action instanceof DbAction.UpdateRoot<?> updateRoot) {
executionContext.executeUpdateRoot(updateRoot);
} else if (action instanceof DbAction.Delete<?> delete) {
executionContext.executeDelete(delete);
} else if (action instanceof DbAction.BatchDelete<?> batchDelete) {
executionContext.executeBatchDelete(batchDelete);
} else if (action instanceof DbAction.DeleteAll<?> deleteAll) {
executionContext.executeDeleteAll(deleteAll);
} else if (action instanceof DbAction.DeleteRoot<?> deleteRoot) {
executionContext.executeDeleteRoot(deleteRoot);
} else if (action instanceof DbAction.BatchDeleteRoot<?> batchDeleteRoot) {
executionContext.executeBatchDeleteRoot(batchDeleteRoot);
} else if (action instanceof DbAction.DeleteAllRoot<?> deleteAllRoot) {
executionContext.executeDeleteAllRoot(deleteAllRoot);
} else if (action instanceof DbAction.AcquireLockRoot<?> acquireLockRoot) {
executionContext.executeAcquireLock(acquireLockRoot);
} else if (action instanceof DbAction.AcquireLockAllRoot<?> acquireLockAllRoot) {
executionContext.executeAcquireLockAllRoot(acquireLockAllRoot);
} else {
throw new RuntimeException("unexpected action");
}

View File

@@ -312,8 +312,8 @@ class JdbcAggregateChangeExecutionContext {
@SuppressWarnings("unchecked")
private PersistentPropertyPath<?> getRelativePath(DbAction<?> action, PersistentPropertyPath<?> pathToValue) {
if (action instanceof DbAction.Insert) {
return pathToValue.getExtensionForBaseOf(((DbAction.Insert) action).getPropertyPath());
if (action instanceof DbAction.Insert insert) {
return pathToValue.getExtensionForBaseOf(insert.getPropertyPath());
}
if (action instanceof DbAction.InsertRoot) {

View File

@@ -192,9 +192,9 @@ public class MappingJdbcConverter extends MappingRelationalConverter implements
return value;
}
if (value instanceof Array) {
if (value instanceof Array array) {
try {
return super.readValue(((Array) value).getArray(), type);
return super.readValue(array.getArray(), type);
} catch (SQLException | ConverterNotFoundException e) {
LOG.info("Failed to extract a value of type %s from an Array; Attempting to use standard conversions", e);
}

View File

@@ -148,7 +148,7 @@ public class QueryMapper {
Assert.state(table != null, String.format("The column %s must have a table set", column));
Column columnFromTable = table.column(field.getMappedColumnName());
return column instanceof Aliased ? columnFromTable.as(((Aliased) column).getAlias()) : columnFromTable;
return column instanceof Aliased aliased ? columnFromTable.as(aliased.getAlias()) : columnFromTable;
}
if (expression instanceof SimpleFunction function) {
@@ -162,7 +162,7 @@ public class QueryMapper {
SimpleFunction mappedFunction = SimpleFunction.create(function.getFunctionName(), mappedArguments);
return function instanceof Aliased ? mappedFunction.as(((Aliased) function).getAlias()) : mappedFunction;
return function instanceof Aliased aliased ? mappedFunction.as(aliased.getAlias()) : mappedFunction;
}
throw new IllegalArgumentException(String.format("Cannot map %s", expression));

View File

@@ -37,7 +37,7 @@ class ResultSetAccessorPropertyAccessor implements PropertyAccessor {
@Override
public boolean canRead(EvaluationContext context, @Nullable Object target, String name) {
return target instanceof ResultSetAccessor && ((ResultSetAccessor) target).hasValue(name);
return target instanceof ResultSetAccessor resultSetAccessor && resultSetAccessor.hasValue(name);
}
@Override