KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > model > ForeignkeyDef


1 package xdoclet.modules.ojb.model;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.ArrayList JavaDoc;
19
20 /**
21  * Definition of a foreignkey for a torque table.
22  *
23  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
24  */

25 public class ForeignkeyDef extends DefBase
26 {
27     /** The local columns */
28     private ArrayList JavaDoc _localColumns = new ArrayList JavaDoc();
29     /** The remote columns */
30     private ArrayList JavaDoc _remoteColumns = new ArrayList JavaDoc();
31     
32     /**
33      * Creates a new foreignkey definition object.
34      *
35      * @param name The name of the foreignkey element
36      * @param tableName The name of the remote table
37      */

38     public ForeignkeyDef(String JavaDoc name, String JavaDoc tableName)
39     {
40         super(name == null ? "" : name);
41         setProperty(PropertyHelper.TORQUE_PROPERTY_FOREIGNTABLE, tableName);
42     }
43
44     /**
45      * Returns the name of the referenced table.
46      *
47      * @return The table name
48      */

49     public String JavaDoc getTableName()
50     {
51         return getProperty(PropertyHelper.TORQUE_PROPERTY_FOREIGNTABLE);
52     }
53
54     /**
55      * Adds a column pair to this foreignkey.
56      *
57      * @param localColumn The column in the local table
58      * @param remoteColumn The column in the remote table
59      */

60     public void addColumnPair(String JavaDoc localColumn, String JavaDoc remoteColumn)
61     {
62         if (!_localColumns.contains(localColumn))
63         {
64             _localColumns.add(localColumn);
65         }
66         if (!_remoteColumns.contains(remoteColumn))
67         {
68             _remoteColumns.add(remoteColumn);
69         }
70     }
71
72     /**
73      * Returns the number of column pairs of this foreignkey definition.
74      *
75      * @return The number of pairs
76      */

77     public int getNumColumnPairs()
78     {
79         return _localColumns.size();
80     }
81     
82     /**
83      * Returns the local column of the specified pair.
84      *
85      * @param idx Specifies the pair
86      * @return The local column of the pair
87      */

88     public String JavaDoc getLocalColumn(int idx)
89     {
90         return (String JavaDoc)_localColumns.get(idx);
91     }
92
93     /**
94      * Returns the remote column of the specified pair.
95      *
96      * @param idx Specifies the pair
97      * @return The remote column of the pair
98      */

99     public String JavaDoc getRemoteColumn(int idx)
100     {
101         return (String JavaDoc)_remoteColumns.get(idx);
102     }
103
104     /* (non-Javadoc)
105      * @see java.lang.Object#equals(java.lang.Object)
106      */

107     public boolean equals(Object JavaDoc obj)
108     {
109         if (!(obj instanceof ForeignkeyDef))
110         {
111             return false;
112         }
113
114         ForeignkeyDef otherForeignkeyDef = (ForeignkeyDef)obj;
115
116         if (!getTableName().equals(otherForeignkeyDef.getTableName()))
117         {
118             return false;
119         }
120         if (!_localColumns.equals(otherForeignkeyDef._localColumns))
121         {
122             return false;
123         }
124         if (!_remoteColumns.equals(otherForeignkeyDef._remoteColumns))
125         {
126             return false;
127         }
128         return true;
129     }
130
131     /* (non-Javadoc)
132      * @see java.lang.Object#hashCode()
133      */

134     public int hashCode()
135     {
136         StringBuffer JavaDoc textRep = new StringBuffer JavaDoc();
137
138         textRep.append(getTableName());
139         textRep.append(" ");
140         textRep.append(_localColumns.toString());
141         textRep.append(" ");
142         textRep.append(_remoteColumns.toString());
143
144         return textRep.toString().hashCode();
145     }
146 }
147
Popular Tags