001    /**
002     * 
003     */
004    package org.wdssii.core;
005    
006    import java.util.ArrayList;
007    import java.util.Date;
008    import java.util.List;
009    
010    /**
011     * @author lakshman
012     *
013     */
014    public class DataTable extends DataType {
015    
016            public static class Column {
017                    private final String name;
018                    private final String unit;
019                    private final List<String> values;        
020                    public Column(String name, String unit, List<String> values) {
021                            this.name = name;
022                            this.unit = unit;
023                            this.values = values;
024                    }
025                    public Column(String name, String unit) {
026                            this.name = name;
027                            this.unit = unit;
028                            this.values = new ArrayList<String>();
029                    }
030                    /** copies from the master. The master can change without affecting this object. */
031                    public Column(Column master){
032                            this.name = master.name;
033                            this.unit = master.unit;
034                            this.values = new ArrayList<String>(master.values); // String is immutable
035                    }
036                    public void addValue(String value){
037                            values.add(value);
038                    }
039                    public String getName() {
040                            return name;
041                    }
042                    public String getUnit() {
043                            return unit;
044                    }
045                    public List<String> getValues() {
046                            return values;
047                    }
048                    public int getNumRows(){
049                            return values.size();
050                    }
051            }
052            
053            private final Column[] columns;
054    
055            /** 
056             * A tabular product.
057             * 
058             * @param originLoc  An arbitrary location representative of this table. All items in
059             *                   this table are assumed to be at this location unless there is
060             *                   a column named Latitude and/or Longitude and/or Height
061             * @param startTime  An arbitrary time representative of this table. All items in this
062             *                   table are assumed to be at this time.  To include items at different
063             *                   times, create different tables.
064             * @param typeName
065             * @param columns    Provide the populated columns here or start off using the Column(name,unit)
066             *                   constructor, then call addRow() multiple times.
067             *                   If providing populated columns, make sure they're all the same size!
068             */
069            public DataTable(Location originLoc, Date startTime, String typeName, Column[] columns) {
070                    super(originLoc, startTime, typeName);
071                    this.columns = columns;
072            }
073    
074            /**
075             * copies all the attributes, etc. from the master. The master can change
076             * without affecting this object
077             */
078            public DataTable(DataTable master) {
079                    super(master);
080                    this.columns = new Column[master.columns.length];
081                    for (int i=0; i < columns.length; ++i){
082                            this.columns[i] = new Column(master.columns[i]);
083                    }
084            }
085    
086            public Column[] getColumns() {
087                    return columns;
088            }
089    
090            /** Make sure to provide the column values in the right order! */
091            public void addRow(String[] values){
092                    if ( values.length != columns.length ){
093                            throw new IllegalArgumentException("The row should have " + columns.length + " columns");
094                    }
095                    for (int i=0; i < values.length; ++i){
096                            columns[i].addValue(values[i]);
097                    }
098            }
099            
100            public int getNumRows(){
101                    return ( columns.length > 0)? columns[0].getNumRows() : 0;
102            }
103    }