KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.TableName 09 Mar 2001
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  * A name of a table and any associated referencing information. This object
29  * is immutable.
30  *
31  * @author Tobias Downer
32  */

33
34 public final class TableName implements Comparable JavaDoc, java.io.Serializable JavaDoc {
35
36   static final long serialVersionUID = 6527135256976754916L;
37
38   /**
39    * The constant 'schema_name' that defines a schema that is unknown.
40    */

41   private static final String JavaDoc UNKNOWN_SCHEMA_NAME = "##UNKNOWN_SCHEMA##";
42
43   /**
44    * The name of the schema of the table. This value can be 'null' which
45    * means the schema is currently unknown.
46    */

47   private final String JavaDoc schema_name;
48
49   /**
50    * The name of the table.
51    */

52   private final String JavaDoc table_name;
53
54   /**
55    * Constructs the name.
56    */

57   public TableName(String JavaDoc schema_name, String JavaDoc table_name) {
58     if (table_name == null) {
59       throw new NullPointerException JavaDoc("'name' can not be null.");
60     }
61     if (schema_name == null) {
62       schema_name = UNKNOWN_SCHEMA_NAME;
63     }
64
65     this.schema_name = schema_name;
66     this.table_name = table_name;
67   }
68
69   public TableName(String JavaDoc table_name) {
70     this(UNKNOWN_SCHEMA_NAME, table_name);
71   }
72
73   /**
74    * Returns the schema name or null if the schema name is unknown.
75    */

76   public String JavaDoc getSchema() {
77     if (schema_name.equals(UNKNOWN_SCHEMA_NAME)) {
78       return null;
79     }
80     else {
81       return schema_name;
82     }
83   }
84
85   /**
86    * Returns the table name.
87    */

88   public String JavaDoc getName() {
89     return table_name;
90   }
91
92   /**
93    * Resolves a schema reference in a table name. If the schema in this
94    * table is 'null' (which means the schema is unknown) then it is set to the
95    * given schema argument.
96    */

97   public TableName resolveSchema(String JavaDoc scheman) {
98     if (schema_name.equals(UNKNOWN_SCHEMA_NAME)) {
99       return new TableName(scheman, getName());
100     }
101     return this;
102   }
103
104   /**
105    * Resolves a [schema name].[table name] type syntax to a TableName
106    * object. Uses 'schemav' only if there is no schema name explicitely
107    * specified.
108    */

109   public static TableName resolve(String JavaDoc schemav, String JavaDoc namev) {
110     int i = namev.indexOf('.');
111     if (i == -1) {
112       return new TableName(schemav, namev);
113     }
114     else {
115       return new TableName(namev.substring(0, i), namev.substring(i + 1));
116     }
117   }
118
119   /**
120    * Resolves a [schema name].[table name] type syntax to a TableName
121    * object.
122    */

123   public static TableName resolve(String JavaDoc namev) {
124     return resolve(UNKNOWN_SCHEMA_NAME, namev);
125   }
126
127   // ----
128

129   /**
130    * To string.
131    */

132   public String JavaDoc toString() {
133     if (getSchema() != null) {
134       return getSchema() + "." + getName();
135     }
136     return getName();
137   }
138
139   /**
140    * Equality.
141    */

142   public boolean equals(Object JavaDoc ob) {
143     TableName tn = (TableName) ob;
144     return tn.schema_name.equals(schema_name) &&
145            tn.table_name.equals(table_name);
146   }
147
148   /**
149    * Equality but ignore the case.
150    */

151   public boolean equalsIgnoreCase(TableName tn) {
152     return tn.schema_name.equalsIgnoreCase(schema_name) &&
153            tn.table_name.equalsIgnoreCase(table_name);
154   }
155
156   /**
157    * Comparable.
158    */

159   public int compareTo(Object JavaDoc ob) {
160     TableName tn = (TableName) ob;
161     int v = schema_name.compareTo(tn.schema_name);
162     if (v == 0) {
163       return table_name.compareTo(tn.table_name);
164     }
165     return v;
166   }
167
168   /**
169    * Hash code.
170    */

171   public int hashCode() {
172     return schema_name.hashCode() + table_name.hashCode();
173   }
174
175 }
176
Popular Tags