rio, lca, refactoring
[jalview.git] / forester / java / src / org / forester / util / BasicTable.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org
25
26 package org.forester.util;
27
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 public class BasicTable<E> {
33
34     private Map<String, Map<String, E>> _rows;
35     private int                         _max_row;
36     private int                         _max_col;
37
38     public BasicTable() {
39         init();
40     }
41
42     public Map<String, E> getColumnsAsMap( final int key_col, final int value_col ) throws IllegalArgumentException {
43         final Map<String, E> map = new HashMap<String, E>();
44         for( int row = 0; row < getNumberOfRows(); ++row ) {
45             final String key = ( String ) getValue( key_col, row );
46             final E value = getValue( value_col, row );
47             if ( ( key != null ) && ( value != null ) ) {
48                 if ( map.containsKey( key ) ) {
49                     throw new IllegalArgumentException( "attempt to use non-unique table value as key [" + key + "]" );
50                 }
51                 map.put( key, value );
52             }
53         }
54         return map;
55     }
56
57     public Map<String, Double> getColumnsAsMapDouble( final int key_col, final int value_col )
58             throws IllegalArgumentException, IOException {
59         final Map<String, Double> map = new HashMap<String, Double>();
60         for( int row = 0; row < getNumberOfRows(); ++row ) {
61             final String key = ( String ) getValue( key_col, row );
62             double value = 0;
63             try {
64                 value = Double.parseDouble( getValueAsString( value_col, row ) );
65             }
66             catch ( final NumberFormatException e ) {
67                 throw new IOException( e );
68             }
69             if ( key != null ) {
70                 if ( map.containsKey( key ) ) {
71                     throw new IllegalArgumentException( "attempt to use non-unique table value as key [" + key + "]" );
72                 }
73                 map.put( key, value );
74             }
75         }
76         return map;
77     }
78
79     // Returns -1 if not found, IllegalArgumentException if not unique.
80     public int findRow( final String first_col_value ) throws IllegalArgumentException {
81         int result = -1;
82         for( int i = 0; i < this.getNumberOfRows(); ++i ) {
83             if ( getValueAsString( 0, i ).equals( first_col_value ) ) {
84                 if ( result >= 0 ) {
85                     throw new IllegalArgumentException( "\"" + first_col_value + "\" is not unique" );
86                 }
87                 result = i;
88             }
89         }
90         return result;
91     }
92
93     public int getNumberOfColumns() {
94         return _max_col + 1;
95     }
96
97     public int getNumberOfRows() {
98         return _max_row + 1;
99     }
100
101     private Map<String, E> getRow( final int row ) {
102         return getRows().get( "" + row );
103     }
104
105     private Map<String, Map<String, E>> getRows() {
106         return _rows;
107     }
108
109     public E getValue( final int col, final int row ) throws IllegalArgumentException {
110         if ( ( row > ( getNumberOfRows() - 1 ) ) || ( row < 0 ) ) {
111             throw new IllegalArgumentException( "value for row (" + row + ") is out of range [number of rows: "
112                     + getNumberOfRows() + "]" );
113         }
114         else if ( ( col >= getNumberOfColumns() ) || ( row < 0 ) ) {
115             throw new IllegalArgumentException( "value for column (" + col + ") is out of range [number of columns: "
116                     + getNumberOfColumns() + "]" );
117         }
118         final Map<String, E> row_map = getRow( row );
119         if ( ( row_map == null ) || ( row_map.size() < 1 ) ) {
120             return null;
121         }
122         return row_map.get( "" + col );
123     }
124
125     public String getValueAsString( final int col, final int row ) throws IllegalArgumentException {
126         if ( getValue( col, row ) != null ) {
127             return getValue( col, row ).toString();
128         }
129         return null;
130     }
131
132     private void init() {
133         _rows = new HashMap<String, Map<String, E>>();
134         setMaxCol( -1 );
135         setMaxRow( -1 );
136     }
137
138     public boolean isEmpty() {
139         return getNumberOfRows() <= 0;
140     }
141
142     private void setMaxCol( final int max_col ) {
143         _max_col = max_col;
144     }
145
146     private void setMaxRow( final int max_row ) {
147         _max_row = max_row;
148     }
149
150     public void setValue( final int col, final int row, final E value ) {
151         if ( ( row < 0 ) || ( col < 0 ) ) {
152             throw new IllegalArgumentException( "attempt to use negative values for row or column" );
153         }
154         if ( row > ( getNumberOfRows() - 1 ) ) {
155             setMaxRow( row );
156         }
157         if ( col > ( getNumberOfColumns() - 1 ) ) {
158             setMaxCol( col );
159         }
160         final String row_key = "" + row;
161         Map<String, E> row_map = null;
162         if ( getRows().containsKey( row_key ) ) {
163             row_map = getRows().get( row_key );
164         }
165         else {
166             row_map = new HashMap<String, E>();
167             getRows().put( row_key, row_map );
168         }
169         row_map.put( "" + col, value );
170     }
171
172     @Override
173     public String toString() {
174         final StringBuffer sb = new StringBuffer();
175         for( int row = 0; row < getNumberOfRows(); ++row ) {
176             for( int col = 0; col < getNumberOfColumns(); ++col ) {
177                 sb.append( getValue( col, row ) );
178                 if ( col < ( getNumberOfColumns() - 1 ) ) {
179                     sb.append( " " );
180                 }
181             }
182             if ( row < ( getNumberOfRows() - 1 ) ) {
183                 sb.append( ForesterUtil.LINE_SEPARATOR );
184             }
185         }
186         return sb.toString();
187     }
188 }