KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > Variable


1 /**
2  * com.mckoi.database.Variable 11 Jul 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 /**
28  * This represents a column name that may be qualified. This object
29  * encapsulated a column name that can be fully qualified in the system. Such
30  * uses of this object would not typically be used against any context. For
31  * example, it would not be desirable to use ColumnName in DataTableDef
32  * because the column names contained in DataTableDef are within a known
33  * context. This object is intended for use within parser processes where
34  * free standing column names with potentially no context are required.
35  * <p>
36  * NOTE: This object is NOT immutable.
37  *
38  * @author Tobias Downer
39  */

40
41 public final class Variable implements java.io.Serializable JavaDoc, Cloneable JavaDoc {
42
43   static final long serialVersionUID = -8772800465139383297L;
44
45   /**
46    * Static that represents an unknown table name.
47    */

48   private static final TableName UNKNOWN_TABLE_NAME =
49                                      new TableName("##UNKNOWN_TABLE_NAME##");
50
51   /**
52    * The TableName that is the context of this column. This may be
53    * UNKNOWN_TABLE_NAME if the table name is not known.
54    */

55   private TableName table_name;
56
57   /**
58    * The column name itself.
59    */

60   private String JavaDoc column_name;
61
62   /**
63    * Constructs the ColumnName.
64    */

65   public Variable(TableName table_name, String JavaDoc column_name) {
66     if (table_name == null || column_name == null) {
67       throw new NullPointerException JavaDoc();
68     }
69     this.table_name = table_name;
70     this.column_name = column_name;
71   }
72
73   public Variable(String JavaDoc column_name) {
74     this(UNKNOWN_TABLE_NAME, column_name);
75   }
76
77   public Variable(Variable v) {
78     this.table_name = v.table_name;
79     this.column_name = v.column_name;
80   }
81
82   /**
83    * Returns the TableName context.
84    */

85   public TableName getTableName() {
86     if (!(table_name.equals(UNKNOWN_TABLE_NAME))) {
87       return table_name;
88     }
89     return null;
90   }
91
92   /**
93    * Returns the column name context.
94    */

95   public String JavaDoc getName() {
96     return column_name;
97   }
98
99   /**
100    * Attempts to resolve a string '[table_name].[column]' to a Variable
101    * instance.
102    */

103   public static Variable resolve(String JavaDoc name) {
104     int div = name.lastIndexOf(".");
105     if (div != -1) {
106       // Column represents '[something].[name]'
107
String JavaDoc column_name = name.substring(div + 1);
108       // Make the '[something]' into a TableName
109
TableName table_name = TableName.resolve(name.substring(0, div));
110       // Set the variable name
111
return new Variable(table_name, column_name);
112     }
113     else {
114       // Column represents '[something]'
115
return new Variable(name);
116     }
117   }
118
119   /**
120    * Attempts to resolve a string '[table_name].[column]' to a Variable
121    * instance. If the table name does not exist, or the table name schema is
122    * not specified, then the schema/table name is copied from the given object.
123    */

124   public static Variable resolve(TableName tname, String JavaDoc name) {
125     Variable v = resolve(name);
126     if (v.getTableName() == null) {
127       return new Variable(tname, v.getName());
128     }
129     else if (v.getTableName().getSchema() == null) {
130       return new Variable(
131           new TableName(tname.getSchema(), v.getTableName().getName()),
132                                                               v.getName());
133     }
134     return v;
135   }
136
137   /**
138    * Returns a ColumnName that is resolved against a table name context only
139    * if the ColumnName is unknown in this object.
140    */

141   public Variable resolveTableName(TableName tablen) {
142     if (table_name.equals(UNKNOWN_TABLE_NAME)) {
143       return new Variable(tablen, getName());
144     }
145     else {
146       return new Variable(table_name.resolveSchema(tablen.getSchema()),
147                           getName());
148     }
149   }
150
151   /**
152    * Sets this Variable object with information from the given Variable.
153    */

154   public Variable set(Variable from) {
155     this.table_name = from.table_name;
156     this.column_name = from.column_name;
157     return this;
158   }
159
160   /**
161    * Sets the column name of this variable. This should be used if the
162    * variable is resolved from one form to another.
163    */

164   public void setColumnName(String JavaDoc column_name) {
165     if (column_name == null) {
166       throw new NullPointerException JavaDoc();
167     }
168     this.column_name = column_name;
169   }
170
171   /**
172    * Sets the TableName of this variable.
173    */

174   public void setTableName(TableName tname) {
175     if (table_name == null) {
176       throw new NullPointerException JavaDoc();
177     }
178     this.table_name = tname;
179   }
180
181
182   // ----
183

184   /**
185    * Performs a deep clone of this object.
186    */

187   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
188     return super.clone();
189   }
190
191   /**
192    * To string.
193    */

194   public String JavaDoc toString() {
195     if (getTableName() != null) {
196       return getTableName() + "." + getName();
197     }
198     return getName();
199   }
200
201   /**
202    * To a differently formatted string.
203    */

204   public String JavaDoc toTechString() {
205     TableName tn = getTableName();
206     if (tn != null) {
207       return tn.getSchema() + "^" + tn.getName() + "^" + getName();
208     }
209     return getName();
210   }
211   
212   /**
213    * Equality.
214    */

215   public boolean equals(Object JavaDoc ob) {
216     Variable cn = (Variable) ob;
217     return cn.table_name.equals(table_name) &&
218            cn.column_name.equals(column_name);
219   }
220
221   /**
222    * Comparable.
223    */

224   public int compareTo(Object JavaDoc ob) {
225     Variable cn = (Variable) ob;
226     int v = table_name.compareTo(cn.table_name);
227     if (v == 0) {
228       return column_name.compareTo(cn.column_name);
229     }
230     return v;
231   }
232
233   /**
234    * Hash code.
235    */

236   public int hashCode() {
237     return table_name.hashCode() + column_name.hashCode();
238   }
239
240
241 // /**
242
// * The name of the variable.
243
// */
244
// private String name;
245
//
246
// /**
247
// * Constructs the variable.
248
// */
249
// public Variable(String name) {
250
// this.name = name;
251
// }
252
//
253
// /**
254
// * Renames the variable to a new name. This should only be used as part
255
// * of resolving an variable alias or lookup.
256
// */
257
// public void rename(String name) {
258
// this.name = name;
259
// }
260
//
261
// /**
262
// * Returns the name of the variable.
263
// */
264
// public String getName() {
265
// return name;
266
// }
267
//
268
//
269
//
270
// public String toString() {
271
// return name;
272
// }
273

274 }
275
Popular Tags