KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > Key


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: Key.java,v 1.4 2003/04/16 03:26:58 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import javax.jdo.JDOFatalInternalException;
19
20
21 abstract class Key
22 {
23     protected BaseTable table;
24     protected ArrayList JavaDoc columns = new ArrayList JavaDoc();
25
26
27     protected Key(BaseTable table)
28     {
29         this.table = table;
30     }
31
32
33     protected void assertSameTable(Column col)
34     {
35         if (!table.equals(col.getTable()))
36             throw new JDOFatalInternalException("Cannot add " + col + " as key column for " + table);
37     }
38
39
40     public BaseTable getTable()
41     {
42         return table;
43     }
44
45
46     public List JavaDoc getColumns()
47     {
48         return Collections.unmodifiableList(columns);
49     }
50
51
52     public String JavaDoc getColumnList()
53     {
54         return getColumnList(columns);
55     }
56
57
58     public boolean startsWith(Key k)
59     {
60         int kSize = k.columns.size();
61
62         return kSize <= columns.size() && k.columns.equals(columns.subList(0, kSize));
63     }
64
65
66     protected static void setMinSize(List JavaDoc list, int size)
67     {
68         while (list.size() < size)
69             list.add(null);
70     }
71
72
73     public static String JavaDoc getColumnList(Collection JavaDoc cols)
74     {
75         StringBuffer JavaDoc s = new StringBuffer JavaDoc("(");
76         Iterator JavaDoc i = cols.iterator();
77
78         while (i.hasNext())
79         {
80             Column col = (Column)i.next();
81
82             if (col == null)
83                 s.append('?');
84             else
85                 s.append(col.getName());
86
87             if (i.hasNext())
88                 s.append(',');
89         }
90
91         s.append(')');
92
93         return s.toString();
94     }
95 }
96
Popular Tags