Author: tomdz Date: Mon Dec 19 13:17:50 2005 New Revision: 357803 URL: http://svn.apache.org/viewcvs?rev=357803&view=rev Log: Implemented equals, hashCode, toString and (for some) toVerboseString for all model classes
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ForeignKey.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Index.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/IndexColumn.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Reference.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Table.java db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java?rev=357803&r1=357802&r2=357803&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Column.java Mon Dec 19 13:17:50 2005 @@ -18,6 +18,9 @@ import java.io.Serializable; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + /** * Represents a column in the database model. * @@ -397,9 +400,9 @@ /** * [EMAIL PROTECTED] */ - public Object clone() throws CloneNotSupportedException + protected Object clone() throws CloneNotSupportedException { - Column result = new Column(); + Column result = (Column)super.clone(); result._name = _name; result._javaName = _javaName; @@ -413,7 +416,54 @@ result._scale = _scale; result._size = _size; result._sizeAsInt = _sizeAsInt; + return result; + } + + /** + * [EMAIL PROTECTED] + */ + public boolean equals(Object obj) + { + if (obj instanceof Column) + { + Column other = (Column)obj; + + // Note that this compares case sensitive + return new EqualsBuilder().append(_name, other._name) + .append(_primaryKey, other._primaryKey) + .append(_required, other._required) + .append(_autoIncrement, other._autoIncrement) + .append(_typeCode, other._typeCode) + .append(_type, other._type) + .append(_size, other._size) + .append(_sizeAsInt, other._sizeAsInt) + .append(_scale, other._scale) + .append(_defaultValue, other._defaultValue) + .isEquals(); + } + else + { + return false; + } + } + + /** + * [EMAIL PROTECTED] + */ + public int hashCode() + { + return new HashCodeBuilder(17, 37).append(_name) + .append(_primaryKey) + .append(_required) + .append(_autoIncrement) + .append(_typeCode) + .append(_type) + .append(_size) + .append(_sizeAsInt) + .append(_scale) + .append(_defaultValue) + .toHashCode(); } /** Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java?rev=357803&r1=357802&r2=357803&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java Mon Dec 19 13:17:50 2005 @@ -23,6 +23,7 @@ import java.util.Iterator; import org.apache.commons.beanutils.DynaBean; +import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.ddlutils.DynaSqlException; import org.apache.ddlutils.dynabean.DynaClassCache; @@ -496,12 +497,13 @@ */ protected Object clone() throws CloneNotSupportedException { - Database result = new Database(); + Database result = (Database)super.clone(); result._name = _name; result._idMethod = _idMethod; result._version = _version; result._tables = (ArrayList)_tables.clone(); + return result; } @@ -514,8 +516,10 @@ { Database other = (Database)obj; - return _name.equals(other._name) && - _tables.equals(other._tables); + // Note that this compares case sensitive + return new EqualsBuilder().append(_name, other._name) + .append(_tables, other._tables) + .isEquals(); } else { @@ -528,10 +532,9 @@ */ public int hashCode() { - return new HashCodeBuilder(17, 37) - .append(_name) - .append(_tables) - .toHashCode(); + return new HashCodeBuilder(17, 37).append(_name) + .append(_tables) + .toHashCode(); } /** @@ -539,6 +542,35 @@ */ public String toString() { - return "Database " + _name + " [" + _tables.size() + " tables]"; + StringBuffer result = new StringBuffer(); + + result.append("Database [name="); + result.append(getName()); + result.append("; "); + result.append(getTableCount()); + result.append(" tables]"); + + return result.toString(); + } + + /** + * Returns a verbose string representation of this database. + * + * @return The string representation + */ + public String toVerboseString() + { + StringBuffer result = new StringBuffer(); + + result.append("Database ["); + result.append(getName()); + result.append("] tables:"); + for (int idx = 0; idx < getTableCount(); idx++) + { + result.append(" "); + result.append(getTable(idx).toVerboseString()); + } + + return result.toString(); } } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ForeignKey.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ForeignKey.java?rev=357803&r1=357802&r2=357803&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ForeignKey.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/ForeignKey.java Mon Dec 19 13:17:50 2005 @@ -16,9 +16,11 @@ * limitations under the License. */ -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.Iterator; + +import org.apache.commons.collections.set.ListOrderedSet; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; /** * Represents a database foreign key. @@ -31,13 +33,13 @@ // TODO: Make the create/alter/drop functionality respect the name property /** The name of the foreign key, may be <code>null</code>. */ - private String _name; + private String _name; /** The target table. */ - private Table _foreignTable; + private Table _foreignTable; /** The name of the foreign table. */ - private String _foreignTableName; + private String _foreignTableName; /** The references between local and remote columns. */ - private ArrayList _references = new ArrayList(); + private ListOrderedSet _references = new ListOrderedSet(); /** * Creates a new foreign key object that has no name. @@ -220,49 +222,104 @@ /** * [EMAIL PROTECTED] */ - public boolean equals(Object other) + protected Object clone() throws CloneNotSupportedException { - boolean result = (other != null) && getClass().equals(other.getClass()); + ForeignKey result = (ForeignKey)super.clone(); + + result._name = _name; + result._foreignTableName = _foreignTableName; + result._references = new ListOrderedSet(); - if (result) + for (Iterator it = _references.iterator(); it.hasNext();) { - ForeignKey fk = (ForeignKey) other; + result._references.add(it.next()); + } - result = _foreignTableName.equals(fk._foreignTableName) && - (_references.size() == fk._references.size()); + return result; + } - if (result) - { - //check all references - need to ensure order is same for valid comparison - List copyThis = (List)_references.clone(); - List copyThat = (List)fk._references.clone(); + /** + * [EMAIL PROTECTED] + */ + public boolean equals(Object obj) + { + if (obj instanceof ForeignKey) + { + ForeignKey other = (ForeignKey)obj; - Collections.sort(copyThis); - Collections.sort(copyThat); - result = copyThis.equals(copyThat); - } + // Note that this compares case sensitive + // Note also that we can simply compare the references regardless of their order + // (which is irrelevant for fks) because they are contained in a set + return new EqualsBuilder().append(_name, other._name) + .append(_foreignTableName, other._foreignTableName) + .append(_references, other._references) + .isEquals(); + } + else + { + return false; } - return result; } /** * [EMAIL PROTECTED] */ - public String toString() + public int hashCode() { - //TODO show name and references - return "ForeignKey[" + _foreignTableName + "]"; + return new HashCodeBuilder(17, 37).append(_name) + .append(_foreignTableName) + .append(_references) + .toHashCode(); } /** * [EMAIL PROTECTED] */ - public Object clone() throws CloneNotSupportedException + public String toString() { - ForeignKey result = new ForeignKey(_name); + StringBuffer result = new StringBuffer(); - result._foreignTableName = _foreignTableName; - result._references = (ArrayList)_references.clone(); - return result; + result.append("Foreign key ["); + if ((getName() != null) && (getName().length() > 0)) + { + result.append("name="); + result.append(getName()); + result.append("; "); + } + result.append("foreign table="); + result.append(getForeignTableName()); + result.append("; "); + result.append(getReferenceCount()); + result.append(" references]"); + + return result.toString(); + } + + /** + * Returns a verbose string representation of this foreign key. + * + * @return The string representation + */ + public String toVerboseString() + { + StringBuffer result = new StringBuffer(); + + result.append("ForeignK ky ["); + if ((getName() != null) && (getName().length() > 0)) + { + result.append("name="); + result.append(getName()); + result.append("; "); + } + result.append("foreign table="); + result.append(getForeignTableName()); + result.append("] references:"); + for (int idx = 0; idx < getReferenceCount(); idx++) + { + result.append(" "); + result.append(getReference(idx).toString()); + } + + return result.toString(); } } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Index.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Index.java?rev=357803&r1=357802&r2=357803&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Index.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Index.java Mon Dec 19 13:17:50 2005 @@ -97,4 +97,11 @@ * @param idx The position of the index column to remove */ public void removeColumn(int idx); + + /** + * Returns a verbose string representation of this index. + * + * @return The string representation + */ + public String toVerboseString(); } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/IndexColumn.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/IndexColumn.java?rev=357803&r1=357802&r2=357803&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/IndexColumn.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/IndexColumn.java Mon Dec 19 13:17:50 2005 @@ -18,6 +18,9 @@ import java.io.Serializable; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + /** * Represents a column of an index in the database model. * @@ -34,7 +37,6 @@ /** The size of the column in the index. */ protected String _size; - // TODO: Implement equals, hashCode and toString // TODO: It might be useful if the referenced column is directly acessible here ? /** @@ -82,11 +84,56 @@ */ public Object clone() throws CloneNotSupportedException { - IndexColumn result = new IndexColumn(); + IndexColumn result = (IndexColumn)super.clone(); result._name = _name; result._size = _size; return result; } -} + /** + * [EMAIL PROTECTED] + */ + public boolean equals(Object obj) + { + if (obj instanceof IndexColumn) + { + IndexColumn other = (IndexColumn)obj; + + // Note that this compares case sensitive + return new EqualsBuilder().append(_name, other._name) + .append(_size, other._size) + .isEquals(); + } + else + { + return false; + } + } + + /** + * [EMAIL PROTECTED] + */ + public int hashCode() + { + return new HashCodeBuilder(17, 37).append(_name) + .append(_size) + .toHashCode(); + } + + /** + * [EMAIL PROTECTED] + */ + public String toString() + { + StringBuffer result = new StringBuffer(); + + result.append("Index column [name="); + result.append(getName()); + result.append("; size="); + result.append(getSize()); + result.append("]"); + + return result.toString(); + } +} Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java?rev=357803&r1=357802&r2=357803&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/NonUniqueIndex.java Mon Dec 19 13:17:50 2005 @@ -18,6 +18,9 @@ import java.util.ArrayList; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + /** * Represents an index definition for a table. * @@ -123,12 +126,77 @@ /** * [EMAIL PROTECTED] */ - public Object clone() throws CloneNotSupportedException + protected Object clone() throws CloneNotSupportedException { - NonUniqueIndex result = new NonUniqueIndex(); + NonUniqueIndex result = (NonUniqueIndex)super.clone(); result._name = _name; result._columns = (ArrayList)_columns.clone(); + return result; + } + + /** + * [EMAIL PROTECTED] + */ + public boolean equals(Object obj) + { + if (obj instanceof NonUniqueIndex) + { + NonUniqueIndex other = (NonUniqueIndex)obj; + + return new EqualsBuilder().append(_name, other._name) + .append(_columns, other._columns) + .isEquals(); + } + else + { + return false; + } + } + + /** + * [EMAIL PROTECTED] + */ + public int hashCode() + { + return new HashCodeBuilder(17, 37).append(_name) + .append(_columns) + .toHashCode(); + } + + /** + * [EMAIL PROTECTED] + */ + public String toString() + { + StringBuffer result = new StringBuffer(); + + result.append("Index [name="); + result.append(getName()); + result.append("; "); + result.append(getColumnCount()); + result.append(" columns]"); + + return result.toString(); + } + + /** + * [EMAIL PROTECTED] + */ + public String toVerboseString() + { + StringBuffer result = new StringBuffer(); + + result.append("Index ["); + result.append(getName()); + result.append("] columns:"); + for (int idx = 0; idx < getColumnCount(); idx++) + { + result.append(" "); + result.append(getColumn(idx).toString()); + } + + return result.toString(); } } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Reference.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Reference.java?rev=357803&r1=357802&r2=357803&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Reference.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Reference.java Mon Dec 19 13:17:50 2005 @@ -1,5 +1,8 @@ package org.apache.ddlutils.model; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + /* * Copyright 1999-2005 The Apache Software Foundation. * @@ -22,7 +25,7 @@ * @author Thomas Dudziak * @version $Revision$ */ -public class Reference implements Cloneable, Comparable +public class Reference implements Cloneable { /** The local column. */ private Column _localColumn; @@ -146,43 +149,43 @@ /** * [EMAIL PROTECTED] */ - public Object clone() throws CloneNotSupportedException + protected Object clone() throws CloneNotSupportedException { - return new Reference(getLocalColumn(), getForeignColumn()); + Reference result = (Reference)super.clone(); + + result._localColumnName = _localColumnName; + result._foreignColumnName = _foreignColumnName; + + return result; } /** * [EMAIL PROTECTED] */ - public int compareTo(Object other) + public boolean equals(Object obj) { - Reference ref = (Reference)other; - - int result = getLocalColumnName().compareTo(ref.getLocalColumnName()); + if (obj instanceof Reference) + { + Reference other = (Reference)obj; - if (result == 0) + return new EqualsBuilder().append(_localColumnName, other._localColumnName) + .append(_foreignColumnName, other._foreignColumnName) + .isEquals(); + } + else { - result = getForeignColumnName().compareTo(ref.getForeignColumnName()); + return false; } - return result; } /** * [EMAIL PROTECTED] */ - public boolean equals(Object other) + public int hashCode() { - boolean result = (other != null) && getClass().equals(other.getClass()); - - if (result) - { - Reference ref = (Reference) other; - - // TODO: Compare the columns, not their names - result = getLocalColumnName().equals(ref.getLocalColumnName()) && - getForeignColumnName().equals(ref.getForeignColumnName()); - } - return result; + return new HashCodeBuilder(17, 37).append(_localColumnName) + .append(_foreignColumnName) + .toHashCode(); } /** @@ -190,6 +193,12 @@ */ public String toString() { - return "Reference[" + getLocalColumnName() + " to " + getForeignColumnName() + "]"; + StringBuffer result = new StringBuffer(); + + result.append(getLocalColumnName()); + result.append(" -> "); + result.append(getForeignColumnName()); + + return result.toString(); } } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Table.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Table.java?rev=357803&r1=357802&r2=357803&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Table.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Table.java Mon Dec 19 13:17:50 2005 @@ -23,6 +23,8 @@ import org.apache.commons.collections.Predicate; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; /** * Represents a table in the database model. @@ -597,9 +599,9 @@ /** * [EMAIL PROTECTED] */ - public Object clone() throws CloneNotSupportedException + protected Object clone() throws CloneNotSupportedException { - Table result = new Table(); + Table result = (Table)super.clone(); result._catalog = _catalog; result._schema = _schema; @@ -608,14 +610,102 @@ result._columns = (ArrayList)_columns.clone(); result._foreignKeys = (ArrayList)_foreignKeys.clone(); result._indices = (ArrayList)_indices.clone(); + return result; } /** * [EMAIL PROTECTED] */ + public boolean equals(Object obj) + { + if (obj instanceof Table) + { + Table other = (Table)obj; + + // Note that this compares case sensitive + return new EqualsBuilder().append(_catalog, other._catalog) + .append(_schema, other._schema) + .append(_name, other._name) + .append(_type, other._type) + .append(_columns, other._columns) + .append(_foreignKeys, other._foreignKeys) + .append(_indices, other._indices) + .isEquals(); + } + else + { + return false; + } + } + + /** + * [EMAIL PROTECTED] + */ + public int hashCode() + { + return new HashCodeBuilder(17, 37).append(_catalog) + .append(_schema) + .append(_name) + .append(_type) + .append(_columns) + .append(_foreignKeys) + .append(_indices) + .toHashCode(); + } + + /** + * [EMAIL PROTECTED] + */ public String toString() { - return "Table " + _name + " [" + _columns.size() + " columns]"; + StringBuffer result = new StringBuffer(); + + result.append("Table [name="); + result.append(getName()); + result.append("; "); + result.append(getColumnCount()); + result.append(" columns]"); + + return result.toString(); + } + + /** + * Returns a verbose string representation of this table. + * + * @return The string representation + */ + public String toVerboseString() + { + StringBuffer result = new StringBuffer(); + + result.append("Table [name="); + result.append(getName()); + result.append("; catalog="); + result.append(getCatalog()); + result.append("; schema="); + result.append(getCatalog()); + result.append("; type="); + result.append(getType()); + result.append("] columns:"); + for (int idx = 0; idx < getColumnCount(); idx++) + { + result.append(" "); + result.append(getColumn(idx).toVerboseString()); + } + result.append("; indices:"); + for (int idx = 0; idx < getIndexCount(); idx++) + { + result.append(" "); + result.append(getIndex(idx).toVerboseString()); + } + result.append("; foreign keys:"); + for (int idx = 0; idx < getForeignKeyCount(); idx++) + { + result.append(" "); + result.append(getForeignKey(idx).toVerboseString()); + } + + return result.toString(); } } Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java?rev=357803&r1=357802&r2=357803&view=diff ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/UniqueIndex.java Mon Dec 19 13:17:50 2005 @@ -18,6 +18,9 @@ import java.util.ArrayList; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + /** * Provides compatibility with Torque-style xml with separate <index> and * <unique> tags, but adds no functionality. All indexes are treated the @@ -42,12 +45,77 @@ /** * [EMAIL PROTECTED] */ - public Object clone() throws CloneNotSupportedException + protected Object clone() throws CloneNotSupportedException { - UniqueIndex result = new UniqueIndex(); + UniqueIndex result = (UniqueIndex)super.clone(); result._name = _name; result._columns = (ArrayList)_columns.clone(); + return result; + } + + /** + * [EMAIL PROTECTED] + */ + public boolean equals(Object obj) + { + if (obj instanceof UniqueIndex) + { + UniqueIndex other = (UniqueIndex)obj; + + return new EqualsBuilder().append(_name, other._name) + .append(_columns, other._columns) + .isEquals(); + } + else + { + return false; + } + } + + /** + * [EMAIL PROTECTED] + */ + public int hashCode() + { + return new HashCodeBuilder(17, 37).append(_name) + .append(_columns) + .toHashCode(); + } + + /** + * [EMAIL PROTECTED] + */ + public String toString() + { + StringBuffer result = new StringBuffer(); + + result.append("Unique index [name="); + result.append(getName()); + result.append("; "); + result.append(getColumnCount()); + result.append(" columns]"); + + return result.toString(); + } + + /** + * [EMAIL PROTECTED] + */ + public String toVerboseString() + { + StringBuffer result = new StringBuffer(); + + result.append("Unique index ["); + result.append(getName()); + result.append("] columns:"); + for (int idx = 0; idx < getColumnCount(); idx++) + { + result.append(" "); + result.append(getColumn(idx).toString()); + } + + return result.toString(); } }