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:
committed by
Jens Schauder
parent
9b0b91bfd0
commit
c8b9697c51
@@ -491,8 +491,7 @@ public class AggregateChangeIdGenerationImmutableUnitTests {
|
||||
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof DummyEntity)) return false;
|
||||
final DummyEntity other = (DummyEntity) o;
|
||||
if (!(o instanceof DummyEntity other)) return false;
|
||||
final Object this$rootId = this.getRootId();
|
||||
final Object other$rootId = other.getRootId();
|
||||
if (this$rootId == null ? other$rootId != null : !this$rootId.equals(other$rootId)) return false;
|
||||
@@ -623,8 +622,7 @@ public class AggregateChangeIdGenerationImmutableUnitTests {
|
||||
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof Content)) return false;
|
||||
final Content other = (Content) o;
|
||||
if (!(o instanceof Content other)) return false;
|
||||
final Object this$id = this.getId();
|
||||
final Object other$id = other.getId();
|
||||
if (this$id == null ? other$id != null : !this$id.equals(other$id)) return false;
|
||||
@@ -725,8 +723,7 @@ public class AggregateChangeIdGenerationImmutableUnitTests {
|
||||
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof ContentNoId)) return false;
|
||||
final ContentNoId other = (ContentNoId) o;
|
||||
if (!(o instanceof ContentNoId other)) return false;
|
||||
final Object this$single = this.getSingle();
|
||||
final Object other$single = other.getSingle();
|
||||
if (this$single == null ? other$single != null : !this$single.equals(other$single)) return false;
|
||||
@@ -805,8 +802,7 @@ public class AggregateChangeIdGenerationImmutableUnitTests {
|
||||
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof Tag)) return false;
|
||||
final Tag other = (Tag) o;
|
||||
if (!(o instanceof Tag other)) return false;
|
||||
final Object this$id = this.getId();
|
||||
final Object other$id = other.getId();
|
||||
if (this$id == null ? other$id != null : !this$id.equals(other$id)) return false;
|
||||
|
||||
@@ -941,8 +941,8 @@ class SqlGeneratorUnitTests {
|
||||
@Nullable
|
||||
private SqlIdentifier getAlias(Object maybeAliased) {
|
||||
|
||||
if (maybeAliased instanceof Aliased) {
|
||||
return ((Aliased) maybeAliased).getAlias();
|
||||
if (maybeAliased instanceof Aliased aliased) {
|
||||
return aliased.getAlias();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -115,10 +115,8 @@ class LiquibaseChangeSetWriterUnitTests {
|
||||
|
||||
ChangeSet changeSet = writer.createChangeSet(ChangeSetMetadata.create(), new DatabaseChangeLog());
|
||||
|
||||
Optional<Change> tableWithFk = changeSet.getChanges().stream().filter(change -> {
|
||||
return change instanceof CreateTableChange
|
||||
&& ((CreateTableChange) change).getTableName().equals("table_with_fk_field");
|
||||
}).findFirst();
|
||||
Optional<Change> tableWithFk = changeSet.getChanges().stream().filter(change -> change instanceof CreateTableChange createTableChange
|
||||
&& createTableChange.getTableName().equals("table_with_fk_field")).findFirst();
|
||||
assertThat(tableWithFk.isPresent()).isEqualTo(true);
|
||||
|
||||
List<ColumnConfig> columns = ((CreateTableChange) tableWithFk.get()).getColumns();
|
||||
@@ -181,9 +179,7 @@ class LiquibaseChangeSetWriterUnitTests {
|
||||
|
||||
|
||||
void assertCreateTable(ChangeSet changeSet, String tableName, Tuple... columnTuples) {
|
||||
Optional<Change> createTableOptional = changeSet.getChanges().stream().filter(change -> {
|
||||
return change instanceof CreateTableChange && ((CreateTableChange) change).getTableName().equals(tableName);
|
||||
}).findFirst();
|
||||
Optional<Change> createTableOptional = changeSet.getChanges().stream().filter(change -> change instanceof CreateTableChange createTableChange && createTableChange.getTableName().equals(tableName)).findFirst();
|
||||
assertThat(createTableOptional.isPresent()).isTrue();
|
||||
CreateTableChange createTable = (CreateTableChange) createTableOptional.get();
|
||||
assertThat(createTable.getColumns())
|
||||
@@ -193,11 +189,9 @@ class LiquibaseChangeSetWriterUnitTests {
|
||||
|
||||
void assertAddForeignKey(ChangeSet changeSet, String baseTableName, String baseColumnNames,
|
||||
String referencedTableName, String referencedColumnNames) {
|
||||
Optional<Change> addFkOptional = changeSet.getChanges().stream().filter(change -> {
|
||||
return change instanceof AddForeignKeyConstraintChange
|
||||
&& ((AddForeignKeyConstraintChange) change).getBaseTableName().equals(baseTableName)
|
||||
&& ((AddForeignKeyConstraintChange) change).getBaseColumnNames().equals(baseColumnNames);
|
||||
}).findFirst();
|
||||
Optional<Change> addFkOptional = changeSet.getChanges().stream().filter(change -> change instanceof AddForeignKeyConstraintChange addForeignKeyConstraintChange
|
||||
&& addForeignKeyConstraintChange.getBaseTableName().equals(baseTableName)
|
||||
&& addForeignKeyConstraintChange.getBaseColumnNames().equals(baseColumnNames)).findFirst();
|
||||
assertThat(addFkOptional.isPresent()).isTrue();
|
||||
AddForeignKeyConstraintChange addFk = (AddForeignKeyConstraintChange) addFkOptional.get();
|
||||
assertThat(addFk.getBaseTableName()).isEqualTo(baseTableName);
|
||||
|
||||
@@ -66,8 +66,8 @@ public class JdbcRepositoryWithCollectionsAndManuallyAssignedIdHsqlIntegrationTe
|
||||
|
||||
return (ApplicationListener<BeforeConvertEvent>) event -> {
|
||||
|
||||
if (event.getEntity() instanceof DummyEntity) {
|
||||
setIds((DummyEntity) event.getEntity());
|
||||
if (event.getEntity() instanceof DummyEntity dummyEntity) {
|
||||
setIds(dummyEntity);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user