001    /*
002     * Created on Jul 31, 2005
003     *
004     */
005    package aima.util;
006    
007    import java.util.Hashtable;
008    import java.util.List;
009    
010    /**
011     * @author Ravi Mohan
012     * 
013     */
014    public class Table<RowHeaderType, ColumnHeaderType, ValueType> {
015            private List<RowHeaderType> rowHeaders;
016            private List<ColumnHeaderType> columnHeaders;
017            private Hashtable<RowHeaderType, Hashtable<ColumnHeaderType, ValueType>> rows;
018    
019            public Table(List<RowHeaderType> rowHeaders,
020                            List<ColumnHeaderType> columnHeaders) {
021    
022                    this.rowHeaders = rowHeaders;
023                    this.columnHeaders = columnHeaders;
024                    this.rows = new Hashtable<RowHeaderType, Hashtable<ColumnHeaderType, ValueType>>();
025                    for (RowHeaderType rowHeader : rowHeaders) {
026                            rows.put(rowHeader, new Hashtable<ColumnHeaderType, ValueType>());
027                    }
028            }
029    
030            public void set(RowHeaderType r, ColumnHeaderType c, ValueType v) {
031                    rows.get(r).put(c, v);
032            }
033    
034            public ValueType get(RowHeaderType r, ColumnHeaderType c) {
035                    Hashtable<ColumnHeaderType, ValueType> rowValues = rows.get(r);
036                    return rowValues == null ? null : rowValues.get(c);
037    
038            }
039    
040            @Override
041            public String toString() {
042                    StringBuffer buf = new StringBuffer();
043                    for (RowHeaderType r : rowHeaders) {
044                            Hashtable<ColumnHeaderType, ValueType> rowValues = rows.get(r);
045                            for (ColumnHeaderType c : columnHeaders) {
046                                    buf.append(get(r, c).toString());
047                                    buf.append(" ");
048                            }
049                            buf.append("\n");
050                    }
051                    return buf.toString();
052            }
053    
054            class Row<R> {
055                    private Hashtable<ColumnHeaderType, ValueType> cells;
056    
057                    public Row() {
058    
059                            this.cells = new Hashtable<ColumnHeaderType, ValueType>();
060                    }
061    
062                    public Hashtable<ColumnHeaderType, ValueType> cells() {
063                            return this.cells;
064                    }
065    
066            }
067    
068            class Cell<ValueHeaderType> {
069                    private ValueHeaderType value;
070    
071                    public Cell() {
072                            value = null;
073                    }
074    
075                    public Cell(ValueHeaderType value) {
076                            this.value = value;
077                    }
078    
079                    public void set(ValueHeaderType value) {
080                            this.value = value;
081                    }
082    
083                    public ValueHeaderType value() {
084                            return value;
085                    }
086    
087            }
088    
089    }