KickJava   Java API By Example, From Geeks To Geeks.

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


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.Column;
24 import org.apache.ws.jaxme.sqls.Index;
25 import org.apache.ws.jaxme.sqls.Table;
26
27
28 /**
29  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
30  */

31 public class IndexImpl extends ColumnSetImpl implements Index {
32   public static class NameImpl extends SQLFactoryImpl.IdentImpl implements Index.Name {
33     public NameImpl(String JavaDoc pName) {
34       super(pName);
35     }
36   }
37
38   private final List JavaDoc columns = new ArrayList JavaDoc();
39   private final boolean unique;
40   private final boolean primaryKey;
41   private Index.Name name;
42
43     IndexImpl(Table pTable, boolean pUnique, boolean pPrimaryKey) {
44       super(pTable);
45       unique = pUnique;
46       primaryKey = pPrimaryKey;
47       if (primaryKey && !unique) {
48          throw new IllegalArgumentException JavaDoc("A primary key must be unique.");
49       }
50     setName(pTable.getName() + "_" + "I" + ((TableImpl) pTable).indexNameCounter++);
51   }
52
53   public void setName(Index.Name pName) {
54     if (pName == null) {
55       throw new NullPointerException JavaDoc("An index name must not be null.");
56     }
57     name = pName;
58   }
59
60   public void setName(String JavaDoc pName) {
61     if (pName == null) {
62       throw new NullPointerException JavaDoc("An index name must not be null.");
63     }
64     setName(new IndexImpl.NameImpl(pName));
65   }
66
67   public Index.Name getName() {
68     return name;
69     }
70
71     public void addColumn(Column pColumn) {
72       if (columns.contains(pColumn)) {
73          throw new IllegalStateException JavaDoc("The column " + pColumn.getName() + " was already added to the key.");
74       }
75       columns.add(pColumn);
76     }
77
78    public void addColumn(Column.Name pName) {
79       Column column = getTable().getColumn(pName);
80       if (column == null) {
81          throw new NullPointerException JavaDoc("The table " + getTable().getName() + " doesn't contain a column " + pName);
82       }
83       addColumn(column);
84    }
85
86    public void addColumn(String JavaDoc pName) {
87       addColumn(new ColumnImpl.NameImpl(pName));
88    }
89
90     public boolean isUnique() {
91       return unique;
92     }
93
94     public boolean isPrimaryKey() {
95       return primaryKey;
96     }
97
98    public Iterator JavaDoc getColumns() {
99       return columns.iterator();
100    }
101 }
102
Popular Tags