001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/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: apoth $
055 *
056 * @version. $Revision: 9343 $, $Date: 2007-12-27 14:30:32 +0100 (Do, 27 Dez 2007) $
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, int[] columnTypes ) throws TableException {
077 setTableName( tableName );
078
079 if ( columnTypes == null )
080 throw new TableException( "Invalid column types. Column types = null" );
081
082 this.columnTypes = columnTypes;
083
084 if ( columnNames == null ) {
085 this.columnNames = new String[columnTypes.length];
086 for ( int i = 0; i < this.columnNames.length; i++ ) {
087 this.columnNames[i] = "";
088 }
089 } else {
090 this.columnNames = new String[columnNames.length];
091 for ( int i = 0; i < columnNames.length; i++ ) {
092 this.columnNames[i] = columnNames[i].toUpperCase();
093 columnNamesMap.put( this.columnNames[i], new Integer( i ) );
094 }
095 }
096
097 if ( columnTypes.length != this.columnNames.length ) {
098 throw new TableException( "column names and types are not of the " + "same length" );
099 }
100
101 rows = new ArrayList<ArrayList<Object>>( 1000 );
102 }
103
104 /**
105 * @param tableName
106 * @param columnNames
107 * @param columnTypes
108 * @param data
109 * @throws TableException
110 */
111 public DefaultTable( String tableName, String[] columnNames, int[] columnTypes, Object[][] data )
112 throws TableException {
113 this( tableName, columnNames, columnTypes );
114
115 rows = new ArrayList<ArrayList<Object>>( data.length );
116 for ( int i = 0; i < data.length; i++ ) {
117 appendRow( data[i] );
118 }
119 }
120
121 /**
122 * @param tableName
123 * @param columnNames
124 * @param columnTypes
125 * @param initialCapacity
126 * @throws TableException
127 */
128 public DefaultTable( String tableName, String[] columnNames, int[] columnTypes, int initialCapacity )
129 throws TableException {
130 this( tableName, columnNames, columnTypes );
131 rows.ensureCapacity( initialCapacity );
132 }
133
134 /**
135 * returns the name of the table. If the table hasn't a name an empty string ("") will be
136 * returned.
137 *
138 */
139 public String getTableName() {
140 return tableName;
141 }
142
143 /**
144 * @see org.deegree.model.table.DefaultTable#getTableName()
145 *
146 */
147 public void setTableName( String tableName ) {
148 this.tableName = tableName;
149 }
150
151 /**
152 * returns the value of the table field indexed by <tt>row</tt> and <tt>col</tt>
153 */
154 public Object getValueAt( int row, int col ) {
155 ArrayList tmp = rows.get( row );
156 return tmp.get( col );
157 }
158
159 /**
160 * set a value at the table field indexed by <tt>row</tt> and <tt>col</tt>
161 */
162 public void setValueAt( Object value, int row, int col ) {
163 ArrayList<Object> tmp = rows.get( row );
164 tmp.set( col, value );
165 }
166
167 /**
168 * returns the data of the row'th row of the table
169 */
170 public Object[] getRow( int row ) {
171 ArrayList<Object> tmp = rows.get( row );
172 return tmp.toArray();
173 }
174
175 /**
176 * sets the data of the row'th row
177 */
178 public void setRow( Object[] data, int row )
179 throws TableException {
180 if ( this.getColumnCount() != data.length ) {
181 throw new TableException( "submitted row doesn't have the same length" + " as the table has columns." );
182 }
183 ArrayList<Object> tmp = rows.get( row );
184
185 for ( int i = 0; i < data.length; i++ ) {
186 tmp.set( i, data[i] );
187 }
188 }
189
190 /**
191 * appends a row to the table and sets its data
192 */
193 public void appendRow( Object[] data )
194 throws TableException {
195 if ( this.getColumnCount() != data.length ) {
196 throw new TableException( "submitted row doesn't have the same length" + " as the table has columns." );
197 }
198 ArrayList<Object> tmp = new ArrayList<Object>( data.length );
199 for ( int i = 0; i < data.length; i++ ) {
200 tmp.add( data[i] );
201 }
202 rows.add( tmp );
203 }
204
205 /**
206 * returns the number rows of the table
207 */
208 public int getRowCount() {
209 return rows.size();
210 }
211
212 /**
213 * adds a new column to the table. for this a computional expensive operation this method should
214 * be used with care.
215 */
216 public void addColumn( String name, int type ) {
217 String[] s1 = new String[columnNames.length + 1];
218 int[] s2 = new int[columnNames.length + 1];
219 for ( int i = 0; i < columnNames.length; i++ ) {
220 s1[i] = columnNames[i];
221 s2[i] = columnTypes[i];
222 }
223 s1[s1.length - 1] = name;
224 s2[s2.length - 1] = type;
225 columnNames = s1;
226 columnTypes = s2;
227
228 for ( int i = 0; i < rows.size(); i++ ) {
229 ArrayList<Object> tmp = rows.get( i );
230 tmp.add( "" );
231 }
232
233 }
234
235 /**
236 * returns the number columns of the table
237 */
238 public int getColumnCount() {
239 return columnNames.length;
240 }
241
242 /**
243 * returns the names of all table columns. If a column hasn't a name a empty String ("") will be
244 * returned.
245 *
246 */
247 public String[] getColumnNames() {
248 return columnNames;
249 }
250
251 /**
252 * returns the name of the specified column. If a column hasn't a name a empty String ("") will
253 * be returned.
254 */
255 public String getColumnName( int col ) {
256 return columnNames[col];
257 }
258
259 /**
260 * returns the names of all column types. For each column a type (name of a java class) has to
261 * be defined.
262 *
263 */
264 public int[] getColumnTypes() {
265 return columnTypes;
266 }
267
268 /**
269 * returns the name of the type of the specifies column. For each column a type (name of a java
270 * class) has to be defined.
271 */
272 public int getColumnType( int col ) {
273 return columnTypes[col];
274 }
275
276 /**
277 * sets the type of a column.
278 */
279 public void setColumnType( int col, int type )
280 throws TableException {
281 columnTypes[col] = type;
282 }
283
284 /**
285 * sets the name of a column.
286 */
287 public void setColumnName( int col, String name ) {
288 columnNames[col] = name;
289 }
290
291 /**
292 * removes a row from the table
293 *
294 * @param index
295 * @return removed row
296 */
297 public Object[] removeRow( int index ) {
298 ArrayList<Object> list = rows.remove( index );
299 return list.toArray();
300 }
301
302 /**
303 * returns the index of the submitted columns name. If no column with that name if present -1
304 * will be returned. the test is not case sensitive
305 *
306 * @param columnName
307 */
308 public int getColumnIndex( String columnName ) {
309 Integer index = columnNamesMap.get( columnName.toUpperCase() );
310 return index.intValue();
311 }
312
313 /**
314 * @return string representation
315 */
316 public String toString() {
317 StringBuffer sb = new StringBuffer( 100000 );
318 for ( int i = 0; i < getRowCount(); i++ ) {
319 sb.append( "row: " + i );
320 for ( int c = 0; c < getColumnCount(); c++ ) {
321 sb.append( getColumnName( c ) + ": " + getValueAt( i, c ) );
322 }
323 }
324 return sb.toString();
325 }
326 }