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