2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org
26 package org.forester.util;
28 import java.text.NumberFormat;
29 import java.util.HashMap;
31 import java.util.SortedSet;
32 import java.util.TreeSet;
34 public class GeneralTable<IDENTIFIER_TYPE, VALUE_TYPE> {
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;
40 public GeneralTable() {
44 public SortedSet<IDENTIFIER_TYPE> getColumnIdentifiers() {
45 return _column_identifiers;
48 private Map<IDENTIFIER_TYPE, VALUE_TYPE> getRow( final IDENTIFIER_TYPE row ) {
49 return getRows().get( row );
52 public SortedSet<IDENTIFIER_TYPE> getRowIdentifiers() {
53 return _row_identifiers;
56 private Map<IDENTIFIER_TYPE, Map<IDENTIFIER_TYPE, VALUE_TYPE>> getRows() {
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 ) ) {
65 return row_map.get( col );
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() );
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>();
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 );
88 row_map = new HashMap<IDENTIFIER_TYPE, VALUE_TYPE>();
89 getRows().put( row, row_map );
91 row_map.put( col, value );
95 public String toString() {
96 final StringBuilder sb = new StringBuilder();
98 for( final IDENTIFIER_TYPE col : getColumnIdentifiers() ) {
99 sb.append( col.toString() );
102 sb.append( ForesterUtil.LINE_SEPARATOR );
103 for( final IDENTIFIER_TYPE row : getRowIdentifiers() ) {
104 sb.append( row.toString() );
106 for( final IDENTIFIER_TYPE col : getColumnIdentifiers() ) {
107 sb.append( getValueAsString( col, row ) );
110 sb.append( ForesterUtil.LINE_SEPARATOR );
112 return sb.toString();
115 public String toString( final NumberFormat number_format ) {
116 final StringBuilder sb = new StringBuilder();
118 for( final IDENTIFIER_TYPE col : getColumnIdentifiers() ) {
119 sb.append( col.toString() );
122 sb.append( ForesterUtil.LINE_SEPARATOR );
123 for( final IDENTIFIER_TYPE row : getRowIdentifiers() ) {
124 sb.append( row.toString() );
126 for( final IDENTIFIER_TYPE col : getColumnIdentifiers() ) {
128 sb.append( number_format.format( getValue( col, row ) ) );
130 catch ( final IllegalArgumentException e ) {
131 sb.append( getValueAsString( col, row ) );
135 sb.append( ForesterUtil.LINE_SEPARATOR );
137 return sb.toString();