001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/model/table/DefaultTable.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044: package org.deegree.model.table;
045:
046: import java.util.ArrayList;
047: import java.util.HashMap;
048:
049: /**
050: *
051: *
052: *
053: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
054: * @author last edited by: $Author: mschneider $
055: *
056: * @version $Revision: 10547 $, $Date: 2008-03-11 01:40:28 -0700 (Tue, 11 Mar 2008) $
057: */
058: public class DefaultTable implements Table {
059:
060: private String tableName = "";
061:
062: private ArrayList<ArrayList<Object>> rows = null;
063:
064: private String[] columnNames = null;
065:
066: private int[] columnTypes = null;
067:
068: private HashMap<String, Integer> columnNamesMap = new HashMap<String, Integer>();
069:
070: /**
071: * @param tableName
072: * @param columnNames
073: * @param columnTypes
074: * @throws TableException
075: */
076: public DefaultTable(String tableName, String[] columnNames,
077: int[] columnTypes) throws TableException {
078: setTableName(tableName);
079:
080: if (columnTypes == null)
081: throw new TableException(
082: "Invalid column types. Column types = null");
083:
084: this .columnTypes = columnTypes;
085:
086: if (columnNames == null) {
087: this .columnNames = new String[columnTypes.length];
088: for (int i = 0; i < this .columnNames.length; i++) {
089: this .columnNames[i] = "";
090: }
091: } else {
092: this .columnNames = new String[columnNames.length];
093: for (int i = 0; i < columnNames.length; i++) {
094: this .columnNames[i] = columnNames[i].toUpperCase();
095: columnNamesMap.put(this .columnNames[i], new Integer(i));
096: }
097: }
098:
099: if (columnTypes.length != this .columnNames.length) {
100: throw new TableException(
101: "column names and types are not of the "
102: + "same length");
103: }
104:
105: rows = new ArrayList<ArrayList<Object>>(1000);
106: }
107:
108: /**
109: * @param tableName
110: * @param columnNames
111: * @param columnTypes
112: * @param data
113: * @throws TableException
114: */
115: public DefaultTable(String tableName, String[] columnNames,
116: int[] columnTypes, Object[][] data) throws TableException {
117: this (tableName, columnNames, columnTypes);
118:
119: rows = new ArrayList<ArrayList<Object>>(data.length);
120: for (int i = 0; i < data.length; i++) {
121: appendRow(data[i]);
122: }
123: }
124:
125: /**
126: * @param tableName
127: * @param columnNames
128: * @param columnTypes
129: * @param initialCapacity
130: * @throws TableException
131: */
132: public DefaultTable(String tableName, String[] columnNames,
133: int[] columnTypes, int initialCapacity)
134: throws TableException {
135: this (tableName, columnNames, columnTypes);
136: rows.ensureCapacity(initialCapacity);
137: }
138:
139: /**
140: * returns the name of the table. If the table hasn't a name an empty string ("") will be
141: * returned.
142: *
143: */
144: public String getTableName() {
145: return tableName;
146: }
147:
148: /**
149: * @see org.deegree.model.table.DefaultTable#getTableName()
150: *
151: */
152: public void setTableName(String tableName) {
153: this .tableName = tableName;
154: }
155:
156: /**
157: * returns the value of the table field indexed by <tt>row</tt> and <tt>col</tt>
158: */
159: public Object getValueAt(int row, int col) {
160: ArrayList tmp = rows.get(row);
161: return tmp.get(col);
162: }
163:
164: /**
165: * set a value at the table field indexed by <tt>row</tt> and <tt>col</tt>
166: */
167: public void setValueAt(Object value, int row, int col) {
168: ArrayList<Object> tmp = rows.get(row);
169: tmp.set(col, value);
170: }
171:
172: /**
173: * returns the data of the row'th row of the table
174: */
175: public Object[] getRow(int row) {
176: ArrayList<Object> tmp = rows.get(row);
177: return tmp.toArray();
178: }
179:
180: /**
181: * sets the data of the row'th row
182: */
183: public void setRow(Object[] data, int row) throws TableException {
184: if (this .getColumnCount() != data.length) {
185: throw new TableException(
186: "submitted row doesn't have the same length"
187: + " as the table has columns.");
188: }
189: ArrayList<Object> tmp = rows.get(row);
190:
191: for (int i = 0; i < data.length; i++) {
192: tmp.set(i, data[i]);
193: }
194: }
195:
196: /**
197: * appends a row to the table and sets its data
198: */
199: public void appendRow(Object[] data) throws TableException {
200: if (this .getColumnCount() != data.length) {
201: throw new TableException(
202: "submitted row doesn't have the same length"
203: + " as the table has columns.");
204: }
205: ArrayList<Object> tmp = new ArrayList<Object>(data.length);
206: for (int i = 0; i < data.length; i++) {
207: tmp.add(data[i]);
208: }
209: rows.add(tmp);
210: }
211:
212: /**
213: * returns the number rows of the table
214: */
215: public int getRowCount() {
216: return rows.size();
217: }
218:
219: /**
220: * adds a new column to the table. for this a computional expensive operation this method should
221: * be used with care.
222: */
223: public void addColumn(String name, int type) {
224: String[] s1 = new String[columnNames.length + 1];
225: int[] s2 = new int[columnNames.length + 1];
226: for (int i = 0; i < columnNames.length; i++) {
227: s1[i] = columnNames[i];
228: s2[i] = columnTypes[i];
229: }
230: s1[s1.length - 1] = name;
231: s2[s2.length - 1] = type;
232: columnNames = s1;
233: columnTypes = s2;
234:
235: for (int i = 0; i < rows.size(); i++) {
236: ArrayList<Object> tmp = rows.get(i);
237: tmp.add("");
238: }
239:
240: }
241:
242: /**
243: * returns the number columns of the table
244: */
245: public int getColumnCount() {
246: return columnNames.length;
247: }
248:
249: /**
250: * returns the names of all table columns. If a column hasn't a name a empty String ("") will be
251: * returned.
252: *
253: */
254: public String[] getColumnNames() {
255: return columnNames;
256: }
257:
258: /**
259: * returns the name of the specified column. If a column hasn't a name a empty String ("") will
260: * be returned.
261: */
262: public String getColumnName(int col) {
263: return columnNames[col];
264: }
265:
266: /**
267: * returns the names of all column types. For each column a type (name of a java class) has to
268: * be defined.
269: *
270: */
271: public int[] getColumnTypes() {
272: return columnTypes;
273: }
274:
275: /**
276: * returns the name of the type of the specifies column. For each column a type (name of a java
277: * class) has to be defined.
278: */
279: public int getColumnType(int col) {
280: return columnTypes[col];
281: }
282:
283: /**
284: * sets the type of a column.
285: */
286: public void setColumnType(int col, int type) throws TableException {
287: columnTypes[col] = type;
288: }
289:
290: /**
291: * sets the name of a column.
292: */
293: public void setColumnName(int col, String name) {
294: columnNames[col] = name;
295: }
296:
297: /**
298: * removes a row from the table
299: *
300: * @param index
301: * @return removed row
302: */
303: public Object[] removeRow(int index) {
304: ArrayList<Object> list = rows.remove(index);
305: return list.toArray();
306: }
307:
308: /**
309: * returns the index of the submitted columns name. If no column with that name if present -1
310: * will be returned. the test is not case sensitive
311: *
312: * @param columnName
313: */
314: public int getColumnIndex(String columnName) {
315: Integer index = columnNamesMap.get(columnName.toUpperCase());
316: return index.intValue();
317: }
318:
319: /**
320: * @return string representation
321: */
322: public String toString() {
323: StringBuffer sb = new StringBuffer(100000);
324: for (int i = 0; i < getRowCount(); i++) {
325: sb.append("row: " + i);
326: for (int c = 0; c < getColumnCount(); c++) {
327: sb.append(getColumnName(c) + ": " + getValueAt(i, c));
328: }
329: }
330: return sb.toString();
331: }
332: }
|