KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > source > Table


1 package jimm.datavision.source;
2 import jimm.datavision.Identity;
3 import jimm.datavision.Nameable;
4 import java.util.TreeMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 /**
8  * Represents a table that contains columns. Not all data sources will
9  * use tables. For those that don't, their columns' <code>getTable</code>
10  * method will return <code>null</code>.
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  * @see Column
14  * @see DataSource
15  */

16 public class Table implements Identity, Nameable {
17
18 protected DataSource dataSource;
19 protected String JavaDoc name;
20 protected TreeMap JavaDoc columns;
21
22 /**
23  * Given a column id, returns the column that has that id. If no column
24  * with the specified id exists, returns <code>null</code>.
25  *
26  * @return a column, or <code>null</code> if no column with the specified
27  * id exists
28  */

29 public Column findColumn(Object JavaDoc id) {
30     if (dataSource.getReport().caseSensitiveDatabaseNames())
31     return (Column)columns.get(id.toString());
32
33     // Case-insensitive match
34
String JavaDoc target = id.toString().toLowerCase();
35     for (Iterator JavaDoc iter = columns.keySet().iterator(); iter.hasNext(); ) {
36     String JavaDoc key = (String JavaDoc)iter.next();
37     if (target.equals(key.toLowerCase()))
38         return (Column)columns.get(key);
39     }
40     return null;
41 }
42
43 /**
44  * Constructor.
45  *
46  * @param dataSource the data source in which this table resides
47  * @param name the table's name
48  */

49 public Table(DataSource dataSource, String JavaDoc name) {
50     this.dataSource = dataSource;
51     this.name = name;
52     columns = new TreeMap JavaDoc();
53 }
54
55 /**
56  * Returns the table id. By default, it's the same as the table name.
57  *
58  * @return the table id
59  */

60 public Object JavaDoc getId() { return name; }
61
62 /**
63  * Returns the table name.
64  *
65  * @return the table name
66  */

67 public String JavaDoc getName() { return name; }
68
69 /** A table's name is immutable. */
70 public void setName(String JavaDoc name) { }
71
72 /**
73  * Adds a column to the collection, using the column's id as the key.
74  *
75  * @param col a column
76  */

77 public void addColumn(Column col) {
78     columns.put(col.getId().toString(), col);
79 }
80
81 /**
82  * Returns an iterator over the columns in this table.
83  *
84  * @return an iterator over the columns in this table
85  */

86 public Iterator JavaDoc columns() { return columns.values().iterator(); }
87
88 }
89
Popular Tags