clean up
[jalview.git] / forester / java / src / org / forester / util / GeneralTable.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.text.NumberFormat;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.SortedSet;
32 import java.util.TreeSet;
33
34 public class GeneralTable<IDENTIFIER_TYPE, VALUE_TYPE> {
35
36     private Map<IDENTIFIER_TYPE, Map<IDENTIFIER_TYPE, VALUE_TYPE>> _rows;
37     private SortedSet<IDENTIFIER_TYPE>                             _row_identifiers;
38     private SortedSet<IDENTIFIER_TYPE>                             _column_identifiers;
39
40     public GeneralTable() {
41         init();
42     }
43
44     public SortedSet<IDENTIFIER_TYPE> getColumnIdentifiers() {
45         return _column_identifiers;
46     }
47
48     private Map<IDENTIFIER_TYPE, VALUE_TYPE> getRow( final IDENTIFIER_TYPE row ) {
49         return getRows().get( row );
50     }
51
52     public SortedSet<IDENTIFIER_TYPE> getRowIdentifiers() {
53         return _row_identifiers;
54     }
55
56     private Map<IDENTIFIER_TYPE, Map<IDENTIFIER_TYPE, VALUE_TYPE>> getRows() {
57         return _rows;
58     }
59
60     public VALUE_TYPE getValue( final IDENTIFIER_TYPE col, final IDENTIFIER_TYPE row ) throws IllegalArgumentException {
61         final Map<IDENTIFIER_TYPE, VALUE_TYPE> row_map = getRow( row );
62         if ( ( row_map == null ) || ( row_map.size() < 1 ) ) {
63             return null;
64         }
65         return row_map.get( col );
66     }
67
68     public String getValueAsString( final IDENTIFIER_TYPE col, final IDENTIFIER_TYPE row )
69             throws IllegalArgumentException {
70         final VALUE_TYPE value = getValue( col, row );
71         return ( value == null ? "" : getValue( col, row ).toString() );
72     }
73
74     private void init() {
75         _rows = new HashMap<IDENTIFIER_TYPE, Map<IDENTIFIER_TYPE, VALUE_TYPE>>();
76         _row_identifiers = new TreeSet<IDENTIFIER_TYPE>();
77         _column_identifiers = new TreeSet<IDENTIFIER_TYPE>();
78     }
79
80     public void setValue( final IDENTIFIER_TYPE col, final IDENTIFIER_TYPE row, final VALUE_TYPE value ) {
81         getColumnIdentifiers().add( col );
82         getRowIdentifiers().add( row );
83         Map<IDENTIFIER_TYPE, VALUE_TYPE> row_map = null;
84         if ( getRows().containsKey( row ) ) {
85             row_map = getRows().get( row );
86         }
87         else {
88             row_map = new HashMap<IDENTIFIER_TYPE, VALUE_TYPE>();
89             getRows().put( row, row_map );
90         }
91         row_map.put( col, value );
92     }
93
94     @Override
95     public String toString() {
96         final StringBuilder sb = new StringBuilder();
97         sb.append( "\t" );
98         for( final IDENTIFIER_TYPE col : getColumnIdentifiers() ) {
99             sb.append( col.toString() );
100             sb.append( "\t" );
101         }
102         sb.append( ForesterUtil.LINE_SEPARATOR );
103         for( final IDENTIFIER_TYPE row : getRowIdentifiers() ) {
104             sb.append( row.toString() );
105             sb.append( "\t" );
106             for( final IDENTIFIER_TYPE col : getColumnIdentifiers() ) {
107                 sb.append( getValueAsString( col, row ) );
108                 sb.append( "\t" );
109             }
110             sb.append( ForesterUtil.LINE_SEPARATOR );
111         }
112         return sb.toString();
113     }
114
115     public String toString( final NumberFormat number_format ) {
116         final StringBuilder sb = new StringBuilder();
117         sb.append( "\t" );
118         for( final IDENTIFIER_TYPE col : getColumnIdentifiers() ) {
119             sb.append( col.toString() );
120             sb.append( "\t" );
121         }
122         sb.append( ForesterUtil.LINE_SEPARATOR );
123         for( final IDENTIFIER_TYPE row : getRowIdentifiers() ) {
124             sb.append( row.toString() );
125             sb.append( "\t" );
126             for( final IDENTIFIER_TYPE col : getColumnIdentifiers() ) {
127                 try {
128                     sb.append( number_format.format( getValue( col, row ) ) );
129                 }
130                 catch ( final IllegalArgumentException e ) {
131                     sb.append( getValueAsString( col, row ) );
132                 }
133                 sb.append( "\t" );
134             }
135             sb.append( ForesterUtil.LINE_SEPARATOR );
136         }
137         return sb.toString();
138     }
139 }