X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Futil%2FBasicTable.java;h=485848c2ac651d473691e4794ddeb08dfb9beddc;hb=50c2ff100214fbb04b5f9665466e6d98d26b051d;hp=6f6d0a8a76ee59875933657bf8e8757e3c39f9a2;hpb=eee996a6476a1e3d84c07f8f690dcde3ff4b2ef5;p=jalview.git diff --git a/forester/java/src/org/forester/util/BasicTable.java b/forester/java/src/org/forester/util/BasicTable.java index 6f6d0a8..485848c 100644 --- a/forester/java/src/org/forester/util/BasicTable.java +++ b/forester/java/src/org/forester/util/BasicTable.java @@ -28,19 +28,35 @@ package org.forester.util; import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; public class BasicTable { - private Map> _rows; - private int _max_row; private int _max_col; + private int _max_row; + private Map> _rows; public BasicTable() { init(); } - public Map getColumnsAsMap( final int key_col, final int value_col ) throws IllegalArgumentException { - final Map map = new HashMap(); + // 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 SortedMap getColumnsAsMap( final int key_col, final int value_col ) throws IllegalArgumentException { + final SortedMap map = new TreeMap(); for( int row = 0; row < getNumberOfRows(); ++row ) { final String key = ( String ) getValue( key_col, row ); final E value = getValue( value_col, row ); @@ -76,20 +92,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,16 +100,19 @@ 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 { - if ( ( row > getNumberOfRows() - 1 ) || ( row < 0 ) ) { + if ( ( row > ( getNumberOfRows() - 1 ) ) || ( row < 0 ) ) { throw new IllegalArgumentException( "value for row (" + row + ") is out of range [number of rows: " + getNumberOfRows() + "]" ); } @@ -129,32 +134,18 @@ 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" ); } - if ( row > getNumberOfRows() - 1 ) { + if ( row > ( getNumberOfRows() - 1 ) ) { setMaxRow( row ); } - if ( col > getNumberOfColumns() - 1 ) { + if ( col > ( getNumberOfColumns() - 1 ) ) { setMaxCol( col ); } final String row_key = "" + row; @@ -171,18 +162,40 @@ 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 ) ); - if ( col < getNumberOfColumns() - 1 ) { + if ( col < ( getNumberOfColumns() - 1 ) ) { sb.append( " " ); } } - if ( row < getNumberOfRows() - 1 ) { + if ( row < ( getNumberOfRows() - 1 ) ) { sb.append( ForesterUtil.LINE_SEPARATOR ); } } 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; + } }