KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > expressions > TableAliasLookup


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.expressions;
23
24 import java.io.*;
25 import oracle.toplink.essentials.internal.helper.*;
26
27 /**
28  * INTERNAL:
29  * Represents the aliased tables an ObjectExpression will be translated to,
30  * along with any of its derived TableExpressions.
31  * For bug 2778339 / CR 2456, this Lookup also represents identity. Two
32  * expressions with the same Lookup will be translated to the same table(s).
33  */

34 public class TableAliasLookup implements Serializable {// CR#3718, implements Serializable
35
protected DatabaseTable[] keys;
36     protected DatabaseTable[] values;
37     protected int lastUsed;
38
39     /* Have these aliases already been added to a FROM clause? */
40     protected boolean haveBeenAddedToStatement;
41
42     /**
43      * TableAliasLookup constructor comment.
44      */

45     public TableAliasLookup() {
46         super();
47         keys = new DatabaseTable[5];
48         values = new DatabaseTable[5];
49         lastUsed = 0;
50     }
51
52     /**
53      * TableAliasLookup constructor comment.
54      */

55     public TableAliasLookup(int initialSize) {
56         super();
57         keys = new DatabaseTable[initialSize];
58         values = new DatabaseTable[initialSize];
59         lastUsed = 0;
60     }
61
62     // Add all of our values to the hashtable
63
public void addToHashtable(java.util.Hashtable JavaDoc aHashTable) {
64         for (int i = 0; i < lastUsed; i++) {
65             aHashTable.put(keys[i], values[i]);
66         }
67     }
68
69     public DatabaseTable get(DatabaseTable key) {
70         int index = lookupIndexOf(key);
71         if (index == -1) {
72             return null;
73         }
74         return values[index];
75     }
76
77     private void grow() {
78         DatabaseTable[] newKeys = new DatabaseTable[(lastUsed * 2)];
79         DatabaseTable[] newValues = new DatabaseTable[(lastUsed * 2)];
80
81         for (int i = 0; i < lastUsed; i++) {
82             newKeys[i] = keys[i];
83             newValues[i] = values[i];
84         }
85         keys = newKeys;
86         values = newValues;
87     }
88
89     /**
90      * INTERNAL:
91      * Answers if the aliases have already been added to a statement.
92      * This insures that a subselect will not re-add aliases already
93      * in a parent FROM clause.
94      * For CR#4223
95      */

96     public boolean haveBeenAddedToStatement() {
97         return haveBeenAddedToStatement;
98     }
99
100     /**
101      * isEmpty method comment.
102      */

103     public boolean isEmpty() {
104         return keys[0] == null;
105     }
106
107     public DatabaseTable keyAtValue(DatabaseTable value) {
108         int index = lookupValueIndexOf(value);
109         if (index == -1) {
110             return null;
111         }
112         return keys[index];
113     }
114
115     public DatabaseTable[] keys() {
116         return keys;
117     }
118
119     private int lookupIndexOf(DatabaseTable table) {
120         for (int i = 0; i < lastUsed; i++) {
121             if (keys[i].equals(table)) {
122                 return i;
123             }
124         }
125         return -1;
126     }
127
128     private int lookupValueIndexOf(DatabaseTable table) {
129         for (int i = 0; i < lastUsed; i++) {
130             if (values[i].equals(table)) {
131                 return i;
132             }
133         }
134         return -1;
135     }
136
137     /**
138      * put method comment.
139      */

140     public DatabaseTable put(DatabaseTable key, DatabaseTable value) {
141         int index = lookupIndexOf(key);
142         if (index == -1) {
143             keys[lastUsed] = key;
144             values[lastUsed++] = value;
145             if (lastUsed >= keys.length) {
146                 grow();
147             }
148         } else {
149             values[index] = value;
150         }
151         return value;
152     }
153
154     /**
155      * INTERNAL:
156      * Called when aliases are added to a statement.
157      * This insures that a subselect will not re-add aliases already
158      * in a parent FROM clause.
159      * For CR#4223
160      */

161     public void setHaveBeenAddedToStatement(boolean value) {
162         haveBeenAddedToStatement = value;
163     }
164
165     /**
166      * size method comment.
167      */

168     public int size() {
169         return lastUsed;
170     }
171
172     public String JavaDoc toString() {
173         int max = size() - 1;
174         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
175         buf.append("{");
176
177         for (int i = 0; i <= max; i++) {
178             String JavaDoc s1 = keys[i].toString();
179             String JavaDoc s2 = values[i].toString();
180             buf.append(s1 + "=" + s2);
181             if (i < max) {
182                 buf.append(", ");
183             }
184         }
185         buf.append("}");
186         return buf.toString();
187     }
188
189     public DatabaseTable[] values() {
190         return values;
191     }
192 }
193
Popular Tags