KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > ColumnOrdering


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.ColumnOrdering
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.sql.compile.RowOrdering;
25 import org.apache.derby.iapi.sql.compile.Optimizable;
26
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28
29 import java.util.Vector JavaDoc;
30
31 class ColumnOrdering {
32
33     /* See RowOrdering for possible values */
34     int myDirection;
35
36     /* A vector of column numbers (Integers) */
37     Vector JavaDoc columns = new Vector JavaDoc();
38
39     /*
40     ** A vector of table numbers (Integers), corresponding to the column
41     ** vector by position.
42     */

43     Vector JavaDoc tables = new Vector JavaDoc();
44
45     /**
46      * @param direction See RowOrdering for possible values
47      */

48     ColumnOrdering(int direction) {
49         myDirection = direction;
50     }
51
52     /**
53      * Does this ColumnOrdering contain the given column in the given table
54      * in the right direction?
55      *
56      * @param direction See RowOrdering for possible values
57      * @param tableNumber The number of the table in question
58      * @param columnNumber The column number in the table (one-based)
59      *
60      * @return true if the column is found here in the right direction
61      */

62     boolean ordered(int direction, int tableNumber, int columnNumber) {
63         /*
64         ** Check the direction only if the direction isn't DONTCARE
65         */

66         if (direction != RowOrdering.DONTCARE) {
67             if (direction != myDirection)
68                 return false;
69         }
70
71         /* The direction matches - see if the column is in this ordering */
72         return contains(tableNumber, columnNumber);
73     }
74
75     /**
76      * Does this ColumnOrdering contain the given column?
77      *
78      * @param tableNumber The number of table in question
79      * @param columnNumber The column number in the table (one-based)
80      *
81      * @return true if the column is found here in the right direction
82      */

83     boolean contains(int tableNumber, int columnNumber)
84     {
85         for (int i = 0; i < columns.size(); i++) {
86             Integer JavaDoc col = (Integer JavaDoc) columns.elementAt(i);
87             Integer JavaDoc tab = (Integer JavaDoc) tables.elementAt(i);
88
89             if (tab.intValue() == tableNumber &&
90                 col.intValue() == columnNumber) {
91
92                 return true;
93             }
94         }
95
96         return false;
97     }
98
99     /**
100      * Get the direction of this ColumnOrdering
101      */

102     int direction()
103     {
104         return myDirection;
105     }
106
107     /**
108      * Add a column in a table to this ColumnOrdering
109      *
110      * @param tableNumber The number of table in question
111      * @param columnNumber The column number in the table (one-based)
112      */

113     void addColumn(int tableNumber, int columnNumber)
114     {
115         tables.addElement(new Integer JavaDoc(tableNumber));
116         columns.addElement(new Integer JavaDoc(columnNumber));
117     }
118
119     /**
120      * Remove all columns with the given table number
121      */

122     void removeColumns(int tableNumber)
123     {
124         /*
125         ** Walk the list backwards, so we can remove elements
126         ** by position.
127         */

128         for (int i = tables.size() - 1; i >= 0; i--)
129         {
130             Integer JavaDoc tab = (Integer JavaDoc) tables.elementAt(i);
131             if (tab.intValue() == tableNumber)
132             {
133                 tables.removeElementAt(i);
134                 columns.removeElementAt(i);
135             }
136         }
137     }
138
139     /**
140      * Tell whether this ColumnOrdering has no elements.
141      */

142     boolean empty()
143     {
144         return (tables.size() == 0);
145     }
146
147     /** Return a clone of this ColumnOrdering */
148     ColumnOrdering cloneMe() {
149         ColumnOrdering retval = new ColumnOrdering(myDirection);
150
151         for (int i = 0; i < columns.size(); i++) {
152             /* Integers are immutable, so just copy the pointers */
153             retval.columns.addElement(columns.elementAt(i));
154             retval.tables.addElement(tables.elementAt(i));
155         }
156
157         return retval;
158     }
159
160     /** Is the given table number in this ColumnOrdering? */
161     boolean hasTable(int tableNumber) {
162         if (tables.size() == 0)
163             return false;
164
165         for (int i = 0; i < tables.size(); i++) {
166             Integer JavaDoc tab = (Integer JavaDoc) tables.elementAt(i);
167             
168             if (tab.intValue() == tableNumber)
169                 return true;
170         }
171
172         return false;
173     }
174
175     /** Is there any table other than the given one in this ColumnOrdering? */
176     boolean hasAnyOtherTable(int tableNumber) {
177         if (tables.size() == 0)
178             return false;
179
180         for (int i = 0; i < tables.size(); i++) {
181             Integer JavaDoc tab = (Integer JavaDoc) tables.elementAt(i);
182             
183             if (tab.intValue() != tableNumber)
184                 return true;
185         }
186
187         return false;
188     }
189
190     public String JavaDoc toString() {
191         String JavaDoc retval = "";
192
193         if (SanityManager.DEBUG) {
194             retval += "Direction: " + myDirection;
195
196             for (int i = 0; i < columns.size(); i++) {
197                 retval += " Table " + tables.elementAt(i) +
198                             ", Column " + columns.elementAt(i);
199             }
200         }
201
202         return retval;
203     }
204 }
205
Popular Tags