KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > engine > database > model > Index


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

18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.apache.torque.engine.EngineException;
27
28 import org.xml.sax.Attributes JavaDoc;
29
30 /**
31  * Information about indices of a table.
32  *
33  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
34  * @author <a HREF="mailto:dlr@finemaltcoding.com>Daniel Rall</a>
35  * @version $Id: Index.java,v 1.6 2004/08/23 00:30:10 seade Exp $
36  */

37 public class Index
38 {
39     /** Logging class from commons.logging */
40     private static Log log = LogFactory.getLog(Index.class);
41     /** name of the index */
42     private String JavaDoc indexName;
43     /** table */
44     private Table parentTable;
45     /** columns */
46     private List JavaDoc indexColumns;
47
48     /**
49      * Creates a new instance with default characteristics (no name or
50      * parent table, small column list size allocation, non-unique).
51      */

52     public Index()
53     {
54         indexColumns = new ArrayList JavaDoc(3);
55     }
56
57     /**
58      * Creates a new instance for the list of columns composing an
59      * index. Otherwise performs as {@link #Index()}.
60      *
61      * @param table The table this index is associated with.
62      * @param indexColumns The list of {@link
63      * org.apache.torque.engine.database.model.Column} objects which
64      * make up this index. Cannot be empty.
65      * @exception EngineException Error generating name.
66      * @see #Index()
67      */

68     protected Index(Table table, List JavaDoc indexColumns)
69         throws EngineException
70     {
71         this();
72         setTable(table);
73         if (!indexColumns.isEmpty())
74         {
75             this.indexColumns = indexColumns;
76
77             if (log.isDebugEnabled())
78             {
79                 log.debug("Created Index named " + getName()
80                         + " with " + indexColumns.size() + " columns");
81             }
82         }
83         else
84         {
85             throw new EngineException("Cannot create a new Index using an "
86                     + "empty list Column object");
87         }
88     }
89
90     /**
91      * Imports index from an XML specification
92      *
93      * @param attrib the xml attributes
94      */

95     public void loadFromXML(Attributes JavaDoc attrib)
96     {
97         indexName = attrib.getValue("name");
98     }
99
100     /**
101      * Returns the uniqueness of this index.
102      *
103      * @return the uniqueness of this index
104      */

105     public boolean isUnique()
106     {
107         return false;
108     }
109
110     /**
111      * Gets the name of this index.
112      *
113      * @return the name of this index
114      */

115     public String JavaDoc getName()
116     {
117         return indexName;
118     }
119
120     /**
121      * Set the name of this index.
122      *
123      * @param name the name of this index
124      */

125     public void setName(String JavaDoc name)
126     {
127         this.indexName = name;
128     }
129
130     /**
131      * Set the parent Table of the index
132      *
133      * @param parent the table
134      */

135     public void setTable(Table parent)
136     {
137         parentTable = parent;
138     }
139
140     /**
141      * Get the parent Table of the index
142      *
143      * @return the table
144      */

145     public Table getTable()
146     {
147         return parentTable;
148     }
149
150     /**
151      * Returns the Name of the table the index is in
152      *
153      * @return the name of the table
154      */

155     public String JavaDoc getTableName()
156     {
157         return parentTable.getName();
158     }
159
160     /**
161      * Adds a new column to an index.
162      *
163      * @param attrib xml attributes for the column
164      */

165     public void addColumn(Attributes JavaDoc attrib)
166     {
167         indexColumns.add(attrib.getValue("name"));
168     }
169
170     /**
171      * Return a comma delimited string of the columns which compose this index.
172      *
173      * @return a list of column names
174      */

175     public String JavaDoc getColumnList()
176     {
177         return Column.makeList(getColumns());
178     }
179
180     /**
181      * Return the list of local columns. You should not edit this list.
182      *
183      * @return a list of columns
184      */

185     public List JavaDoc getColumns()
186     {
187         return indexColumns;
188     }
189
190     /**
191      * Returns the list of names of the columns referenced by this
192      * index. Slightly over-allocates the list's buffer (just in case
193      * more elements are going to be added, such as when a name is
194      * being generated). Feel free to modify this list.
195      *
196      * @return a list of column names
197      */

198     protected List JavaDoc getColumnNames()
199     {
200         List JavaDoc names = new ArrayList JavaDoc(indexColumns.size() + 2);
201         Iterator JavaDoc i = getColumns().iterator();
202         while (i.hasNext())
203         {
204             Column c = (Column) i.next();
205             names.add(c.getName());
206         }
207         return names;
208     }
209
210     /**
211      * String representation of the index. This is an xml representation.
212      *
213      * @return a xml representation
214      */

215     public String JavaDoc toString()
216     {
217         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
218         result.append(" <index name=\"")
219               .append(getName())
220               .append("\"");
221
222         result.append(">\n");
223
224         for (int i = 0; i < indexColumns.size(); i++)
225         {
226             result.append(" <index-column name=\"")
227                 .append(indexColumns.get(i))
228                 .append("\"/>\n");
229         }
230         result.append(" </index>\n");
231         return result.toString();
232     }
233 }
234
Popular Tags