01: package jimm.datavision.source;
02:
03: import jimm.datavision.Identity;
04: import jimm.datavision.Nameable;
05: import java.util.TreeMap;
06: import java.util.Iterator;
07:
08: /**
09: * Represents a table that contains columns. Not all data sources will
10: * use tables. For those that don't, their columns' <code>getTable</code>
11: * method will return <code>null</code>.
12: *
13: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
14: * @see Column
15: * @see DataSource
16: */
17: public class Table implements Identity, Nameable {
18:
19: protected DataSource dataSource;
20: protected String name;
21: protected TreeMap columns;
22:
23: /**
24: * Given a column id, returns the column that has that id. If no column
25: * with the specified id exists, returns <code>null</code>.
26: *
27: * @return a column, or <code>null</code> if no column with the specified
28: * id exists
29: */
30: public Column findColumn(Object id) {
31: if (dataSource.getReport().caseSensitiveDatabaseNames())
32: return (Column) columns.get(id.toString());
33:
34: // Case-insensitive match
35: String target = id.toString().toLowerCase();
36: for (Iterator iter = columns.keySet().iterator(); iter
37: .hasNext();) {
38: String key = (String) iter.next();
39: if (target.equals(key.toLowerCase()))
40: return (Column) columns.get(key);
41: }
42: return null;
43: }
44:
45: /**
46: * Constructor.
47: *
48: * @param dataSource the data source in which this table resides
49: * @param name the table's name
50: */
51: public Table(DataSource dataSource, String name) {
52: this .dataSource = dataSource;
53: this .name = name;
54: columns = new TreeMap();
55: }
56:
57: /**
58: * Returns the table id. By default, it's the same as the table name.
59: *
60: * @return the table id
61: */
62: public Object getId() {
63: return name;
64: }
65:
66: /**
67: * Returns the table name.
68: *
69: * @return the table name
70: */
71: public String getName() {
72: return name;
73: }
74:
75: /** A table's name is immutable. */
76: public void setName(String name) {
77: }
78:
79: /**
80: * Adds a column to the collection, using the column's id as the key.
81: *
82: * @param col a column
83: */
84: public void addColumn(Column col) {
85: columns.put(col.getId().toString(), col);
86: }
87:
88: /**
89: * Returns an iterator over the columns in this table.
90: *
91: * @return an iterator over the columns in this table
92: */
93: public Iterator columns() {
94: return columns.values().iterator();
95: }
96:
97: }
|