KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > sqls > impl > SchemaImpl


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

17 package org.apache.ws.jaxme.sqls.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.ws.jaxme.sqls.SQLFactory;
24 import org.apache.ws.jaxme.sqls.Schema;
25 import org.apache.ws.jaxme.sqls.Table;
26
27
28 /** <p>Implementation of a schema.</p>
29  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
30  */

31 public class SchemaImpl implements Schema {
32    public static class NameImpl extends SQLFactoryImpl.IdentImpl implements Schema.Name {
33       NameImpl(String JavaDoc pName) {
34          super(pName);
35       }
36       public boolean equals(Object JavaDoc o) {
37          if (o == null || !(o instanceof Schema.Name)) {
38             return false;
39          }
40          return super.equals(o);
41       }
42    }
43
44    private SQLFactory sqlFactory;
45    private Schema.Name name;
46    private List JavaDoc tables = new ArrayList JavaDoc();
47
48    /** <p>Creates a new instance of SchemaImpl.</p>
49     * @param pFactory The {@link org.apache.ws.jaxme.sqls.SQLFactory}
50     * creating this instance.
51     * @param pName The schema name.
52     */

53     protected SchemaImpl(SQLFactory pFactory, Schema.Name pName) {
54       sqlFactory = pFactory;
55       name = pName;
56     }
57
58    public SQLFactory getSQLFactory() {
59       return sqlFactory;
60    }
61
62    public Schema.Name getName() {
63       return name;
64    }
65
66    public Table newTable(String JavaDoc pName) {
67       return newTable(new TableImpl.NameImpl(pName));
68    }
69
70     public Table newTable(Table.Name pName) {
71       if (pName == null) {
72          throw new NullPointerException JavaDoc("A table name must not be null.");
73       }
74       Integer JavaDoc maxLength = getSQLFactory().getMaxTableNameLength();
75       if (maxLength != null && pName.getName().length() > maxLength.intValue()) {
76          throw new IllegalArgumentException JavaDoc("The length of the table name " + pName +
77                                              " exceeds the maximum length of " + maxLength);
78       }
79       Table table = getTable(pName);
80       if (table != null) {
81          throw new IllegalStateException JavaDoc("A table named " + table.getName() +
82                                           " already exists in the schema " + getName());
83       }
84       table = ((SQLFactoryImpl) getSQLFactory()).newTableImpl(this, pName);
85       tables.add(table);
86       return table;
87    }
88
89     public Table getTable(Table.Name pName) {
90       if (pName == null) {
91          throw new NullPointerException JavaDoc("A table name must not be null.");
92       }
93       for (Iterator JavaDoc iter = getTables(); iter.hasNext(); ) {
94          Table table = (Table) iter.next();
95          if (getSQLFactory().isTableNameCaseSensitive()) {
96             if (pName.getName().equalsIgnoreCase(table.getName().getName())) {
97                return table;
98             }
99          } else {
100             if (pName.equals(table.getName())) {
101                return table;
102             }
103          }
104       }
105       return null;
106     }
107
108    public Table getTable(String JavaDoc pName) {
109       return getTable(new TableImpl.NameImpl(pName));
110    }
111
112     public Iterator JavaDoc getTables() {
113       return tables.iterator();
114     }
115
116    public boolean equals(Object JavaDoc o) {
117       if (o == null || !(o instanceof Schema)) {
118          return false;
119       }
120       Schema other = (Schema) o;
121       if (getName() == null) {
122          return other.getName() == null;
123       } else {
124          return getName().equals(other.getName());
125       }
126    }
127
128    public int hashCode() {
129       return getName() == null ? 0 : getName().hashCode();
130    }
131 }
132
Popular Tags