01: /*
02: * Copyright 2002 (C) TJDO.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the TJDO License version 1.0.
06: * See the terms of the TJDO License in the documentation provided with this software.
07: *
08: * $Id: Key.java,v 1.4 2003/04/16 03:26:58 jackknifebarber Exp $
09: */
10:
11: package com.triactive.jdo.store;
12:
13: import java.util.ArrayList;
14: import java.util.Collection;
15: import java.util.Collections;
16: import java.util.Iterator;
17: import java.util.List;
18: import javax.jdo.JDOFatalInternalException;
19:
20: abstract class Key {
21: protected BaseTable table;
22: protected ArrayList columns = new ArrayList();
23:
24: protected Key(BaseTable table) {
25: this .table = table;
26: }
27:
28: protected void assertSameTable(Column col) {
29: if (!table.equals(col.getTable()))
30: throw new JDOFatalInternalException("Cannot add " + col
31: + " as key column for " + table);
32: }
33:
34: public BaseTable getTable() {
35: return table;
36: }
37:
38: public List getColumns() {
39: return Collections.unmodifiableList(columns);
40: }
41:
42: public String getColumnList() {
43: return getColumnList(columns);
44: }
45:
46: public boolean startsWith(Key k) {
47: int kSize = k.columns.size();
48:
49: return kSize <= columns.size()
50: && k.columns.equals(columns.subList(0, kSize));
51: }
52:
53: protected static void setMinSize(List list, int size) {
54: while (list.size() < size)
55: list.add(null);
56: }
57:
58: public static String getColumnList(Collection cols) {
59: StringBuffer s = new StringBuffer("(");
60: Iterator i = cols.iterator();
61:
62: while (i.hasNext()) {
63: Column col = (Column) i.next();
64:
65: if (col == null)
66: s.append('?');
67: else
68: s.append(col.getName());
69:
70: if (i.hasNext())
71: s.append(',');
72: }
73:
74: s.append(')');
75:
76: return s.toString();
77: }
78: }
|