From 3edeb03edc8236806e16a65fc30955bf75385d0d Mon Sep 17 00:00:00 2001 From: James Agnew Date: Sun, 26 Jan 2020 22:23:06 -0500 Subject: [PATCH] Rework hashcode generation (#1684) * Rework hashcode generation * Compile fix * Hashcoode refactoring * FIx compile errors --- .../migrate/taskdef/AddForeignKeyTask.java | 34 +++----- .../migrate/taskdef/AddIdGeneratorTask.java | 25 ++---- .../jpa/migrate/taskdef/AddIndexTask.java | 29 +++---- .../migrate/taskdef/AddTableByColumnTask.java | 27 +++---- .../migrate/taskdef/AddTableRawSqlTask.java | 29 +++---- .../jpa/migrate/taskdef/ArbitrarySqlTask.java | 34 +++----- .../migrate/taskdef/BaseTableColumnTask.java | 25 ++---- .../taskdef/BaseTableColumnTypeTask.java | 81 ++++++++----------- .../jpa/migrate/taskdef/BaseTableTask.java | 22 ++--- .../fhir/jpa/migrate/taskdef/BaseTask.java | 36 +++++++-- .../migrate/taskdef/CalculateHashesTask.java | 4 +- .../migrate/taskdef/DropForeignKeyTask.java | 39 ++++----- .../migrate/taskdef/DropIdGeneratorTask.java | 25 ++---- .../jpa/migrate/taskdef/DropIndexTask.java | 35 +++----- .../migrate/taskdef/ExecuteRawSqlTask.java | 21 ++--- .../migrate/taskdef/InitializeSchemaTask.java | 19 ++--- .../jpa/migrate/taskdef/RenameColumnTask.java | 26 +----- .../jpa/migrate/taskdef/RenameIndexTask.java | 39 ++++----- 18 files changed, 212 insertions(+), 338 deletions(-) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddForeignKeyTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddForeignKeyTask.java index 06fe8f2ada9..d087243e480 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddForeignKeyTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddForeignKeyTask.java @@ -74,7 +74,7 @@ public class AddForeignKeyTask extends BaseTableColumnTask { return; } - String sql = null; + String sql; switch (getDriverType()) { case MARIADB_10_1: case MYSQL_5_7: @@ -105,28 +105,20 @@ public class AddForeignKeyTask extends BaseTableColumnTask { } @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (theO == null || getClass() != theO.getClass()) return false; - - AddForeignKeyTask that = (AddForeignKeyTask) theO; - - return new EqualsBuilder() - .appendSuper(super.equals(theO)) - .append(myConstraintName, that.myConstraintName) - .append(myForeignTableName, that.myForeignTableName) - .append(myForeignColumnName, that.myForeignColumnName) - .isEquals(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + super.generateHashCode(theBuilder); + theBuilder.append(myConstraintName); + theBuilder.append(myForeignTableName); + theBuilder.append(myForeignColumnName); } @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .appendSuper(super.hashCode()) - .append(myConstraintName) - .append(myForeignTableName) - .append(myForeignColumnName) - .toHashCode(); + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + AddForeignKeyTask otherObject = (AddForeignKeyTask) theOtherObject; + super.generateEquals(theBuilder, otherObject); + theBuilder.append(myConstraintName, otherObject.myConstraintName); + theBuilder.append(myForeignTableName, otherObject.myForeignTableName); + theBuilder.append(myForeignColumnName, otherObject.myForeignColumnName); } + } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIdGeneratorTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIdGeneratorTask.java index f96127ddcf7..b32fa3d3e1a 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIdGeneratorTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIdGeneratorTask.java @@ -88,9 +88,9 @@ public class AddIdGeneratorTask extends BaseTask { if (isNotBlank(sql)) { Set sequenceNames = JdbcUtils.getSequenceNames(getConnectionProperties()) - .stream() - .map(String::toLowerCase) - .collect(Collectors.toSet()); + .stream() + .map(String::toLowerCase) + .collect(Collectors.toSet()); ourLog.debug("Currently have sequences: {}", sequenceNames); if (sequenceNames.contains(myGeneratorName.toLowerCase())) { logInfo(ourLog, "Sequence {} already exists - No action performed", myGeneratorName); @@ -103,22 +103,13 @@ public class AddIdGeneratorTask extends BaseTask { } @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (!(theO instanceof AddIdGeneratorTask)) return false; - - AddIdGeneratorTask that = (AddIdGeneratorTask) theO; - - return new EqualsBuilder() - .append(myGeneratorName, that.myGeneratorName) - .isEquals(); + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + AddIdGeneratorTask otherObject = (AddIdGeneratorTask) theOtherObject; + theBuilder.append(myGeneratorName, otherObject.myGeneratorName); } @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .append(myGeneratorName) - .toHashCode(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + theBuilder.append(myGeneratorName); } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIndexTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIndexTask.java index 1510dc75560..ad82b38000f 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIndexTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddIndexTask.java @@ -97,28 +97,21 @@ public class AddIndexTask extends BaseTableTask { } @Override - public boolean equals(Object theO) { - if (this == theO) return true; + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + super.generateEquals(theBuilder, theOtherObject); - if (theO == null || getClass() != theO.getClass()) return false; + AddIndexTask otherObject = (AddIndexTask) theOtherObject; + theBuilder.append(myIndexName, otherObject.myIndexName); + theBuilder.append(myColumns, otherObject.myColumns); + theBuilder.append(myUnique, otherObject.myUnique); - AddIndexTask that = (AddIndexTask) theO; - - return new EqualsBuilder() - .appendSuper(super.equals(theO)) - .append(myIndexName, that.myIndexName) - .append(myColumns, that.myColumns) - .append(myUnique, that.myUnique) - .isEquals(); } @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .appendSuper(super.hashCode()) - .append(myIndexName) - .append(myColumns) - .append(myUnique) - .toHashCode(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + super.generateHashCode(theBuilder); + theBuilder.append(myIndexName); + theBuilder.append(myColumns); + theBuilder.append(myUnique); } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTask.java index bb6b6fb9aee..25bdbda4458 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableByColumnTask.java @@ -110,26 +110,17 @@ public class AddTableByColumnTask extends BaseTableTask { } @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (theO == null || getClass() != theO.getClass()) return false; - - AddTableByColumnTask that = (AddTableByColumnTask) theO; - - return new EqualsBuilder() - .appendSuper(super.equals(theO)) - .append(myAddColumnTasks, that.myAddColumnTasks) - .append(myPkColumns, that.myPkColumns) - .isEquals(); + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + super.generateEquals(theBuilder, theOtherObject); + AddTableByColumnTask otherObject = (AddTableByColumnTask) theOtherObject; + theBuilder.append(myAddColumnTasks, otherObject.myAddColumnTasks); + theBuilder.append(myPkColumns, otherObject.myPkColumns); } @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .appendSuper(super.hashCode()) - .append(myAddColumnTasks) - .append(myPkColumns) - .toHashCode(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + super.generateHashCode(theBuilder); + theBuilder.append(myAddColumnTasks); + theBuilder.append(myPkColumns); } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableRawSqlTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableRawSqlTask.java index 2e2d968421d..6c9e980c0cf 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableRawSqlTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/AddTableRawSqlTask.java @@ -31,7 +31,11 @@ import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; import java.sql.SQLException; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; public class AddTableRawSqlTask extends BaseTableTask { @@ -87,24 +91,15 @@ public class AddTableRawSqlTask extends BaseTableTask { } @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (theO == null || getClass() != theO.getClass()) return false; - - AddTableRawSqlTask that = (AddTableRawSqlTask) theO; - - return new EqualsBuilder() - .appendSuper(super.equals(theO)) - .append(myDriverNeutralSqls, that.myDriverNeutralSqls) - .isEquals(); + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + super.generateEquals(theBuilder, theOtherObject); + AddTableRawSqlTask otherObject = (AddTableRawSqlTask) theOtherObject; + theBuilder.append(myDriverNeutralSqls, otherObject.myDriverNeutralSqls); } @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .appendSuper(super.hashCode()) - .append(myDriverNeutralSqls) - .toHashCode(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + super.generateHashCode(theBuilder); + theBuilder.append(myDriverNeutralSqls); } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ArbitrarySqlTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ArbitrarySqlTask.java index 3587add5501..18763523739 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ArbitrarySqlTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ArbitrarySqlTask.java @@ -103,6 +103,17 @@ public class ArbitrarySqlTask extends BaseTask { myConditionalOnExistenceOf.add(new TableAndColumn(theTableName, theColumnName)); } + @Override + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + ArbitrarySqlTask otherObject = (ArbitrarySqlTask) theOtherObject; + theBuilder.append(myTableName, otherObject.myTableName); + } + + @Override + protected void generateHashCode(HashCodeBuilder theBuilder) { + theBuilder.append(myTableName); + } + public enum QueryModeEnum { BATCH_UNTIL_NO_MORE } @@ -134,7 +145,7 @@ public class ArbitrarySqlTask extends BaseTask { do { logInfo(ourLog, "Querying for up to {} rows", myBatchSize); rows = getTxTemplate().execute(t -> { - JdbcTemplate jdbcTemplate = newJdbcTemnplate(); + JdbcTemplate jdbcTemplate = newJdbcTemplate(); jdbcTemplate.setMaxRows(myBatchSize); return jdbcTemplate.query(mySql, new ColumnMapRowMapper()); }); @@ -168,25 +179,4 @@ public class ArbitrarySqlTask extends BaseTask { return myColumn; } } - - @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (!(theO instanceof ArbitrarySqlTask)) return false; - - ArbitrarySqlTask that = (ArbitrarySqlTask) theO; - - return new EqualsBuilder() - .append(myTableName, that.myTableName) - .append(myExecuteOnlyIfTableExists, that.myExecuteOnlyIfTableExists) - .isEquals(); - } - - @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .append(myTableName) - .toHashCode(); - } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTask.java index 87f08f9f228..e4e024ed1e1 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTask.java @@ -27,7 +27,7 @@ import org.thymeleaf.util.StringUtils; import java.util.Locale; -public abstract class BaseTableColumnTask extends BaseTableTask { +public abstract class BaseTableColumnTask> extends BaseTableTask { private String myColumnName; @@ -53,24 +53,15 @@ public abstract class BaseTableColumnTask extends BaseT } @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (!(theO instanceof BaseTableColumnTask)) return false; - - BaseTableColumnTask that = (BaseTableColumnTask) theO; - - return new EqualsBuilder() - .appendSuper(super.equals(theO)) - .append(myColumnName, that.myColumnName) - .isEquals(); + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + BaseTableColumnTask otherObject = (BaseTableColumnTask) theOtherObject; + super.generateEquals(theBuilder, otherObject); + theBuilder.append(myColumnName, otherObject.myColumnName); } @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .appendSuper(super.hashCode()) - .append(myColumnName) - .toHashCode(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + super.generateHashCode(theBuilder); + theBuilder.append(myColumnName); } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTypeTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTypeTask.java index ae04fe11f9a..49fee53525d 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTypeTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableColumnTypeTask.java @@ -25,17 +25,12 @@ import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; import java.util.Objects; -public abstract class BaseTableColumnTypeTask extends BaseTableColumnTask { - private static final Logger ourLog = LoggerFactory.getLogger(BaseTableColumnTypeTask.class); - - +public abstract class BaseTableColumnTypeTask> extends BaseTableColumnTask { private ColumnTypeEnum myColumnType; private Map> myColumnTypeToDriverTypeToSqlType = new HashMap<>(); private Boolean myNullable; @@ -116,6 +111,12 @@ public abstract class BaseTableColumnTypeTask extends B return myColumnType; } + @SuppressWarnings("unchecked") + public T setColumnType(ColumnTypeEnum theColumnType) { + myColumnType = theColumnType; + return (T) this; + } + private void setColumnType(ColumnTypeEnum theColumnType, DriverTypeEnum theDriverType, String theColumnTypeSql) { Map columnSqlType = myColumnTypeToDriverTypeToSqlType.computeIfAbsent(theColumnType, k -> new HashMap<>()); if (columnSqlType.containsKey(theDriverType)) { @@ -124,7 +125,6 @@ public abstract class BaseTableColumnTypeTask extends B columnSqlType.put(theDriverType, theColumnTypeSql); } - @Override public void validate() { super.validate(); @@ -138,12 +138,6 @@ public abstract class BaseTableColumnTypeTask extends B } } - @SuppressWarnings("unchecked") - public T setColumnType(ColumnTypeEnum theColumnType) { - myColumnType = theColumnType; - return (T) this; - } - protected String getSqlType() { return getSqlType(getColumnLength()); } @@ -181,6 +175,30 @@ public abstract class BaseTableColumnTypeTask extends B return this; } + @Override + protected void generateHashCode(HashCodeBuilder theBuilder) { + super.generateHashCode(theBuilder); + theBuilder.append(getColumnTypeName(myColumnType)); + theBuilder.append(myNullable); + theBuilder.append(myColumnLength); + } + + @Override + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + BaseTableColumnTypeTask otherObject = (BaseTableColumnTypeTask) theOtherObject; + super.generateEquals(theBuilder, otherObject); + theBuilder.append(getColumnTypeName(myColumnType), getColumnTypeName(otherObject.myColumnType)); + theBuilder.append(myNullable, otherObject.myNullable); + theBuilder.append(myColumnLength, otherObject.myColumnLength); + } + + @Nullable + private Object getColumnTypeName(ColumnTypeEnum theColumnType) { + if (theColumnType == null) { + return null; + } + return myColumnType.name(); + } public enum ColumnTypeEnum { @@ -191,42 +209,7 @@ public abstract class BaseTableColumnTypeTask extends B FLOAT, INT, BLOB, - CLOB - ; + CLOB; } - - @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (!(theO instanceof BaseTableColumnTypeTask)) return false; - - BaseTableColumnTypeTask that = (BaseTableColumnTypeTask) theO; - - return new EqualsBuilder() - .appendSuper(super.equals(theO)) - .append(getColumnTypeName(myColumnType), getColumnTypeName(that.myColumnType)) - .append(myNullable, that.myNullable) - .append(myColumnLength, that.myColumnLength) - .isEquals(); - } - - @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .appendSuper(super.hashCode()) - .append(getColumnTypeName(myColumnType)) - .append(myNullable) - .append(myColumnLength) - .toHashCode(); - } - - @Nullable - private Object getColumnTypeName(ColumnTypeEnum theColumnType) { - if (theColumnType == null) { - return null; - } - return myColumnType.name(); - } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableTask.java index ae9ee7d8618..3fce1b54fa3 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTableTask.java @@ -24,7 +24,7 @@ import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; -public abstract class BaseTableTask extends BaseTask { +public abstract class BaseTableTask> extends BaseTask { private String myTableName; public BaseTableTask(String theProductVersion, String theSchemaVersion) { @@ -38,6 +38,7 @@ public abstract class BaseTableTask extends BaseTask { public T setTableName(String theTableName) { Validate.notBlank(theTableName); myTableName = theTableName; + //noinspection unchecked return (T) this; } @@ -47,22 +48,13 @@ public abstract class BaseTableTask extends BaseTask { } @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (!(theO instanceof BaseTableTask)) return false; - - BaseTableTask that = (BaseTableTask) theO; - - return new EqualsBuilder() - .append(myTableName, that.myTableName) - .isEquals(); + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + BaseTableTask otherObject = (BaseTableTask) theOtherObject; + theBuilder.append(myTableName, otherObject.myTableName); } @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .append(myTableName) - .toHashCode(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + theBuilder.append(myTableName); } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java index 42fa89faf90..e68d1609158 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java @@ -21,6 +21,8 @@ package ca.uhn.fhir.jpa.migrate.taskdef; */ import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.intellij.lang.annotations.Language; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,9 +40,11 @@ import java.util.regex.Pattern; public abstract class BaseTask { - private static final Logger ourLog = LoggerFactory.getLogger(BaseTask.class); public static final String MIGRATION_VERSION_PATTERN = "\\d{8}\\.\\d+"; + private static final Logger ourLog = LoggerFactory.getLogger(BaseTask.class); private static final Pattern versionPattern = Pattern.compile(MIGRATION_VERSION_PATTERN); + private final String myProductVersion; + private final String mySchemaVersion; private DriverTypeEnum.ConnectionProperties myConnectionProperties; private DriverTypeEnum myDriverType; private String myDescription; @@ -50,8 +54,6 @@ public abstract class BaseTask { private List myExecutedStatements = new ArrayList<>(); private boolean myNoColumnShrink; private boolean myFailureAllowed; - private final String myProductVersion; - private final String mySchemaVersion; protected BaseTask(String theProductVersion, String theSchemaVersion) { myProductVersion = theProductVersion; @@ -106,7 +108,7 @@ public abstract class BaseTask { JdbcTemplate jdbcTemplate = getConnectionProperties().newJdbcTemplate(); try { int changesCount = jdbcTemplate.update(theSql, theArguments); - logInfo(ourLog, "SQL \"{}\" returned {}", theSql, changesCount); + logInfo(ourLog, "SQL \"{}\" returned {}", theSql, changesCount); return changesCount; } catch (DataAccessException e) { if (myFailureAllowed) { @@ -151,7 +153,7 @@ public abstract class BaseTask { return getConnectionProperties().getTxTemplate(); } - public JdbcTemplate newJdbcTemnplate() { + public JdbcTemplate newJdbcTemplate() { return getConnectionProperties().newJdbcTemplate(); } @@ -197,6 +199,30 @@ public abstract class BaseTask { return this; } + @Override + public final int hashCode() { + HashCodeBuilder builder = new HashCodeBuilder(); + generateHashCode(builder); + return builder.hashCode(); + } + + protected abstract void generateHashCode(HashCodeBuilder theBuilder); + + @Override + public final boolean equals(Object theObject) { + if (theObject == null || getClass().equals(theObject.getClass()) == false) { + return false; + } + @SuppressWarnings("unchecked") + T otherObject = (T) theObject; + + EqualsBuilder b = new EqualsBuilder(); + generateEquals(b, otherObject); + return b.isEquals(); + } + + protected abstract void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject); + public static class ExecutedStatement { private final String mySql; private final List myArguments; diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateHashesTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateHashesTask.java index 47b6c7d3a70..cc60f48a428 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateHashesTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/CalculateHashesTask.java @@ -77,7 +77,7 @@ public class CalculateHashesTask extends BaseTableColumnTask { - JdbcTemplate jdbcTemplate = newJdbcTemnplate(); + JdbcTemplate jdbcTemplate = newJdbcTemplate(); jdbcTemplate.setMaxRows(100000); String sql = "SELECT * FROM " + getTableName() + " WHERE " + getColumnName() + " IS NULL"; logInfo(ourLog, "Finding up to {} rows in {} that requires hashes", myBatchSize, getTableName()); @@ -184,7 +184,7 @@ public class CalculateHashesTask extends BaseTableColumnTask { } + @Override + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + DropForeignKeyTask otherObject = (DropForeignKeyTask) theOtherObject; + super.generateEquals(theBuilder, otherObject); + theBuilder.append(myConstraintName, otherObject.myConstraintName); + theBuilder.append(myParentTableName, otherObject.myParentTableName); + } + + @Override + protected void generateHashCode(HashCodeBuilder theBuilder) { + super.generateHashCode(theBuilder); + theBuilder.append(myConstraintName); + theBuilder.append(myParentTableName); + } + @Nonnull static List generateSql(String theTableName, String theConstraintName, DriverTypeEnum theDriverType) { List sqls = new ArrayList<>(); @@ -103,28 +118,4 @@ public class DropForeignKeyTask extends BaseTableTask { } return sqls; } - - @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (theO == null || getClass() != theO.getClass()) return false; - - DropForeignKeyTask that = (DropForeignKeyTask) theO; - - return new EqualsBuilder() - .appendSuper(super.equals(theO)) - .append(myConstraintName, that.myConstraintName) - .append(myParentTableName, that.myParentTableName) - .isEquals(); - } - - @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .appendSuper(super.hashCode()) - .append(myConstraintName) - .append(myParentTableName) - .toHashCode(); - } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIdGeneratorTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIdGeneratorTask.java index be4adf0e7f5..344571c2554 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIdGeneratorTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIdGeneratorTask.java @@ -88,9 +88,9 @@ public class DropIdGeneratorTask extends BaseTask { if (isNotBlank(sql)) { Set sequenceNames = JdbcUtils.getSequenceNames(getConnectionProperties()) - .stream() - .map(String::toLowerCase) - .collect(Collectors.toSet()); + .stream() + .map(String::toLowerCase) + .collect(Collectors.toSet()); ourLog.debug("Currently have sequences: {}", sequenceNames); if (!sequenceNames.contains(myGeneratorName.toLowerCase())) { logInfo(ourLog, "Sequence {} does not exist - No action performed", myGeneratorName); @@ -103,22 +103,13 @@ public class DropIdGeneratorTask extends BaseTask { } @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (!(theO instanceof DropIdGeneratorTask)) return false; - - DropIdGeneratorTask that = (DropIdGeneratorTask) theO; - - return new EqualsBuilder() - .append(myGeneratorName, that.myGeneratorName) - .isEquals(); + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + DropIdGeneratorTask otherObject = (DropIdGeneratorTask) theOtherObject; + theBuilder.append(myGeneratorName, otherObject.myGeneratorName); } @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .append(myGeneratorName) - .toHashCode(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + theBuilder.append(myGeneratorName); } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIndexTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIndexTask.java index b43bf064316..f07f0a95de2 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIndexTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/DropIndexTask.java @@ -78,6 +78,19 @@ public class DropIndexTask extends BaseTableTask { return this; } + @Override + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + DropIndexTask otherObject = (DropIndexTask) theOtherObject; + super.generateEquals(theBuilder, otherObject); + theBuilder.append(myIndexName, otherObject.myIndexName); + } + + @Override + protected void generateHashCode(HashCodeBuilder theBuilder) { + super.generateHashCode(theBuilder); + theBuilder.append(myIndexName); + } + static List createDropIndexSql(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theIndexName, DriverTypeEnum theDriverType) throws SQLException { Validate.notBlank(theIndexName, "theIndexName must not be blank"); Validate.notBlank(theTableName, "theTableName must not be blank"); @@ -130,26 +143,4 @@ public class DropIndexTask extends BaseTableTask { } return sql; } - - @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (theO == null || getClass() != theO.getClass()) return false; - - DropIndexTask that = (DropIndexTask) theO; - - return new EqualsBuilder() - .appendSuper(super.equals(theO)) - .append(myIndexName, that.myIndexName) - .isEquals(); - } - - @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .appendSuper(super.hashCode()) - .append(myIndexName) - .toHashCode(); - } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ExecuteRawSqlTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ExecuteRawSqlTask.java index dfa10a8f3c3..00ba0650836 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ExecuteRawSqlTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ExecuteRawSqlTask.java @@ -80,22 +80,15 @@ public class ExecuteRawSqlTask extends BaseTask { } @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (!(theO instanceof ExecuteRawSqlTask)) return false; - - ExecuteRawSqlTask that = (ExecuteRawSqlTask) theO; - - return new EqualsBuilder() - .append(myDriverNeutralSqls, that.myDriverNeutralSqls) - .isEquals(); + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + ExecuteRawSqlTask otherObject = (ExecuteRawSqlTask) theOtherObject; + theBuilder.append(myDriverNeutralSqls, otherObject.myDriverNeutralSqls); + theBuilder.append(myDriverToSqls, otherObject.myDriverToSqls); } @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .append(myDriverNeutralSqls) - .toHashCode(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + theBuilder.append(myDriverNeutralSqls); + theBuilder.append(myDriverToSqls); } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/InitializeSchemaTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/InitializeSchemaTask.java index 41e409517a2..2c856eac168 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/InitializeSchemaTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/InitializeSchemaTask.java @@ -68,22 +68,13 @@ public class InitializeSchemaTask extends BaseTask { } @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (theO == null || getClass() != theO.getClass()) return false; - - InitializeSchemaTask that = (InitializeSchemaTask) theO; - - return new EqualsBuilder() - .append(mySchemaInitializationProvider, that.mySchemaInitializationProvider) - .isEquals(); + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + InitializeSchemaTask otherObject = (InitializeSchemaTask) theOtherObject; + theBuilder.append(mySchemaInitializationProvider, otherObject.mySchemaInitializationProvider); } @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .append(mySchemaInitializationProvider) - .toHashCode(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + theBuilder.append(mySchemaInitializationProvider); } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTask.java index 7c63309674e..edb003a299f 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameColumnTask.java @@ -22,7 +22,6 @@ package ca.uhn.fhir.jpa.migrate.taskdef; import ca.uhn.fhir.jpa.migrate.JdbcUtils; import org.apache.commons.lang3.Validate; -import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -137,26 +136,9 @@ public class RenameColumnTask extends BaseTableTask { } @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (theO == null || getClass() != theO.getClass()) return false; - - RenameColumnTask that = (RenameColumnTask) theO; - - return new EqualsBuilder() - .appendSuper(super.equals(theO)) - .append(myOldName, that.myOldName) - .append(myNewName, that.myNewName) - .isEquals(); - } - - @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .appendSuper(super.hashCode()) - .append(myOldName) - .append(myNewName) - .toHashCode(); + protected void generateHashCode(HashCodeBuilder theBuilder) { + super.generateHashCode(theBuilder); + theBuilder.append(myOldName); + theBuilder.append(myNewName); } } diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameIndexTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameIndexTask.java index 85f1dfae9a8..7e394ff64e5 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameIndexTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/RenameIndexTask.java @@ -81,6 +81,21 @@ public class RenameIndexTask extends BaseTableTask { return this; } + @Override + protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { + RenameIndexTask otherObject = (RenameIndexTask) theOtherObject; + super.generateEquals(theBuilder, otherObject); + theBuilder.append(myOldIndexName, otherObject.myOldIndexName); + theBuilder.append(myNewIndexName, otherObject.myNewIndexName); + } + + @Override + protected void generateHashCode(HashCodeBuilder theBuilder) { + super.generateHashCode(theBuilder); + theBuilder.append(myOldIndexName); + theBuilder.append(myNewIndexName); + } + static List createRenameIndexSql(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theOldIndexName, String theNewIndexName, DriverTypeEnum theDriverType) throws SQLException { Validate.notBlank(theOldIndexName, "theOldIndexName must not be blank"); Validate.notBlank(theNewIndexName, "theNewIndexName must not be blank"); @@ -110,28 +125,4 @@ public class RenameIndexTask extends BaseTableTask { } return sql; } - - @Override - public boolean equals(Object theO) { - if (this == theO) return true; - - if (theO == null || getClass() != theO.getClass()) return false; - - RenameIndexTask that = (RenameIndexTask) theO; - - return new EqualsBuilder() - .appendSuper(super.equals(theO)) - .append(myOldIndexName, that.myOldIndexName) - .append(myNewIndexName, that.myNewIndexName) - .isEquals(); - } - - @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .appendSuper(super.hashCode()) - .append(myOldIndexName) - .append(myNewIndexName) - .toHashCode(); - } }