X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Futil%2FBasicTable.java;h=cb96dcae13a63ff78697a3ba3c27af4ae073af1b;hb=a1114eb8610e592961a40e5c3d46d647c02b5108;hp=2c7afc84022484a0c2272c2226544c5c275d66c6;hpb=03e51d179caedf757b09e2872f9500318bd85a53;p=jalview.git diff --git a/forester/java/src/org/forester/util/BasicTable.java b/forester/java/src/org/forester/util/BasicTable.java index 2c7afc8..cb96dca 100644 --- a/forester/java/src/org/forester/util/BasicTable.java +++ b/forester/java/src/org/forester/util/BasicTable.java @@ -31,14 +31,28 @@ import java.util.Map; public class BasicTable { - private Map> _rows; - private int _max_row; private int _max_col; + private int _max_row; + private Map> _rows; public BasicTable() { init(); } + // Returns -1 if not found, IllegalArgumentException if not unique. + public int findRow( final String first_col_value ) throws IllegalArgumentException { + int result = -1; + for( int i = 0; i < this.getNumberOfRows(); ++i ) { + if ( getValueAsString( 0, i ).equals( first_col_value ) ) { + if ( result >= 0 ) { + throw new IllegalArgumentException( "\"" + first_col_value + "\" is not unique" ); + } + result = i; + } + } + return result; + } + public Map getColumnsAsMap( final int key_col, final int value_col ) throws IllegalArgumentException { final Map map = new HashMap(); for( int row = 0; row < getNumberOfRows(); ++row ) { @@ -76,20 +90,6 @@ public class BasicTable { return map; } - // Returns -1 if not found, IllegalArgumentException if not unique. - public int findRow( final String first_col_value ) throws IllegalArgumentException { - int result = -1; - for( int i = 0; i < this.getNumberOfRows(); ++i ) { - if ( getValueAsString( 0, i ).equals( first_col_value ) ) { - if ( result >= 0 ) { - throw new IllegalArgumentException( "\"" + first_col_value + "\" is not unique" ); - } - result = i; - } - } - return result; - } - public int getNumberOfColumns() { return _max_col + 1; } @@ -98,12 +98,15 @@ public class BasicTable { return _max_row + 1; } - private Map getRow( final int row ) { - return getRows().get( "" + row ); - } - - private Map> getRows() { - return _rows; + public final String getRowAsString( final int row, final String separator ) { + final StringBuilder sb = new StringBuilder(); + for( int col = 0; col < getNumberOfColumns(); ++col ) { + sb.append( getValue( col, row ).toString() ); + if ( col < ( getNumberOfColumns() - 1 ) ) { + sb.append( separator ); + } + } + return sb.toString(); } public E getValue( final int col, final int row ) throws IllegalArgumentException { @@ -129,24 +132,10 @@ public class BasicTable { return null; } - private void init() { - _rows = new HashMap>(); - setMaxCol( -1 ); - setMaxRow( -1 ); - } - public boolean isEmpty() { return getNumberOfRows() <= 0; } - private void setMaxCol( final int max_col ) { - _max_col = max_col; - } - - private void setMaxRow( final int max_row ) { - _max_row = max_row; - } - public void setValue( final int col, final int row, final E value ) { if ( ( row < 0 ) || ( col < 0 ) ) { throw new IllegalArgumentException( "attempt to use negative values for row or column" ); @@ -171,7 +160,7 @@ public class BasicTable { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); + final StringBuilder sb = new StringBuilder(); for( int row = 0; row < getNumberOfRows(); ++row ) { for( int col = 0; col < getNumberOfColumns(); ++col ) { sb.append( getValue( col, row ) ); @@ -185,4 +174,26 @@ public class BasicTable { } return sb.toString(); } + + private Map getRow( final int row ) { + return getRows().get( "" + row ); + } + + private Map> getRows() { + return _rows; + } + + private void init() { + _rows = new HashMap>(); + setMaxCol( -1 ); + setMaxRow( -1 ); + } + + private void setMaxCol( final int max_col ) { + _max_col = max_col; + } + + private void setMaxRow( final int max_row ) { + _max_row = max_row; + } }