001: package xdoclet.modules.ojb.model;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.ArrayList;
019:
020: /**
021: * Definition of a foreignkey for a torque table.
022: *
023: * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
024: */
025: public class ForeignkeyDef extends DefBase {
026: /** The local columns */
027: private ArrayList _localColumns = new ArrayList();
028: /** The remote columns */
029: private ArrayList _remoteColumns = new ArrayList();
030:
031: /**
032: * Creates a new foreignkey definition object.
033: *
034: * @param name The name of the foreignkey element
035: * @param tableName The name of the remote table
036: */
037: public ForeignkeyDef(String name, String tableName) {
038: super (name == null ? "" : name);
039: setProperty(PropertyHelper.TORQUE_PROPERTY_FOREIGNTABLE,
040: tableName);
041: }
042:
043: /**
044: * Returns the name of the referenced table.
045: *
046: * @return The table name
047: */
048: public String getTableName() {
049: return getProperty(PropertyHelper.TORQUE_PROPERTY_FOREIGNTABLE);
050: }
051:
052: /**
053: * Adds a column pair to this foreignkey.
054: *
055: * @param localColumn The column in the local table
056: * @param remoteColumn The column in the remote table
057: */
058: public void addColumnPair(String localColumn, String remoteColumn) {
059: if (!_localColumns.contains(localColumn)) {
060: _localColumns.add(localColumn);
061: }
062: if (!_remoteColumns.contains(remoteColumn)) {
063: _remoteColumns.add(remoteColumn);
064: }
065: }
066:
067: /**
068: * Returns the number of column pairs of this foreignkey definition.
069: *
070: * @return The number of pairs
071: */
072: public int getNumColumnPairs() {
073: return _localColumns.size();
074: }
075:
076: /**
077: * Returns the local column of the specified pair.
078: *
079: * @param idx Specifies the pair
080: * @return The local column of the pair
081: */
082: public String getLocalColumn(int idx) {
083: return (String) _localColumns.get(idx);
084: }
085:
086: /**
087: * Returns the remote column of the specified pair.
088: *
089: * @param idx Specifies the pair
090: * @return The remote column of the pair
091: */
092: public String getRemoteColumn(int idx) {
093: return (String) _remoteColumns.get(idx);
094: }
095:
096: /* (non-Javadoc)
097: * @see java.lang.Object#equals(java.lang.Object)
098: */
099: public boolean equals(Object obj) {
100: if (!(obj instanceof ForeignkeyDef)) {
101: return false;
102: }
103:
104: ForeignkeyDef otherForeignkeyDef = (ForeignkeyDef) obj;
105:
106: if (!getTableName().equals(otherForeignkeyDef.getTableName())) {
107: return false;
108: }
109: if (!_localColumns.equals(otherForeignkeyDef._localColumns)) {
110: return false;
111: }
112: if (!_remoteColumns.equals(otherForeignkeyDef._remoteColumns)) {
113: return false;
114: }
115: return true;
116: }
117:
118: /* (non-Javadoc)
119: * @see java.lang.Object#hashCode()
120: */
121: public int hashCode() {
122: StringBuffer textRep = new StringBuffer();
123:
124: textRep.append(getTableName());
125: textRep.append(" ");
126: textRep.append(_localColumns.toString());
127: textRep.append(" ");
128: textRep.append(_remoteColumns.toString());
129:
130: return textRep.toString().hashCode();
131: }
132: }
|