1 17 package org.apache.ws.jaxme.sqls.impl; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 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 31 public class IndexImpl extends ColumnSetImpl implements Index { 32 public static class NameImpl extends SQLFactoryImpl.IdentImpl implements Index.Name { 33 public NameImpl(String pName) { 34 super(pName); 35 } 36 } 37 38 private final List columns = new ArrayList (); 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 ("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 ("An index name must not be null."); 56 } 57 name = pName; 58 } 59 60 public void setName(String pName) { 61 if (pName == null) { 62 throw new NullPointerException ("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 ("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 ("The table " + getTable().getName() + " doesn't contain a column " + pName); 82 } 83 addColumn(column); 84 } 85 86 public void addColumn(String 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 getColumns() { 99 return columns.iterator(); 100 } 101 } 102 | Popular Tags |