/* * * SchemaCrawler * http://sourceforge.net/projects/schemacrawler * Copyright (c) 2000-2011, Sualeh Fatehi. * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. * */ package schemacrawler.build; import schemacrawler.schema.Column; import schemacrawler.schema.ColumnMap; /** * Represents a single column mapping from a primary key column to a * foreign key column. * * @author Sualeh Fatehi */ public abstract class BaseColumnMap implements ColumnMap, Comparable<ColumnMap> { public abstract static class BaseColumnMapBuilder { private Column foreignKeyColumn; private Column primaryKeyColumn; public Column getForeignKeyColumn() { return foreignKeyColumn; } public Column getPrimaryKeyColumn() { return primaryKeyColumn; } public void setForeignKeyColumn(final Column foreignKeyColumn) { this.foreignKeyColumn = foreignKeyColumn; } public void setPrimaryKeyColumn(final Column primaryKeyColumn) { this.primaryKeyColumn = primaryKeyColumn; } } private static final long serialVersionUID = -4411771492159843382L; private final Column foreignKeyColumn; private final Column primaryKeyColumn; protected BaseColumnMap(final BaseColumnMapBuilder builder) { this.primaryKeyColumn = builder.primaryKeyColumn; this.foreignKeyColumn = builder.foreignKeyColumn; } public int compareTo(final ColumnMap o) { int compare = 0; if (compare == 0) { compare = primaryKeyColumn.compareTo(o.getPrimaryKeyColumn()); } if (compare == 0) { compare = foreignKeyColumn.compareTo(o.getForeignKeyColumn()); } return compare; } @Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final BaseColumnMap other = (BaseColumnMap) obj; if (foreignKeyColumn == null) { if (other.foreignKeyColumn != null) { return false; } } else if (!foreignKeyColumn.equals(other.foreignKeyColumn)) { return false; } if (primaryKeyColumn == null) { if (other.primaryKeyColumn != null) { return false; } } else if (!primaryKeyColumn.equals(other.primaryKeyColumn)) { return false; } return true; } /** * {@inheritDoc} * * @see schemacrawler.schema.ForeignKeyColumnMap#getForeignKeyColumn() */ public Column getForeignKeyColumn() { return foreignKeyColumn; } /** * {@inheritDoc} * * @see schemacrawler.schema.ForeignKeyColumnMap#getPrimaryKeyColumn() */ public Column getPrimaryKeyColumn() { return primaryKeyColumn; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (foreignKeyColumn == null? 0: foreignKeyColumn.hashCode()); result = prime * result + (primaryKeyColumn == null? 0: primaryKeyColumn.hashCode()); return result; } @Override public String toString() { return primaryKeyColumn + " --> " + foreignKeyColumn; } }