inprogress
[jalview.git] / forester / java / src / org / forester / evoinference / matrix / distance / BasicSymmetricalDistanceMatrix.java
index 85f9184..fdf2ec4 100644 (file)
@@ -22,7 +22,7 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.evoinference.matrix.distance;
 
@@ -35,9 +35,9 @@ import java.util.StringTokenizer;
 import org.forester.util.ForesterUtil;
 import org.forester.util.IllegalFormatUseException;
 
-public class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
+public final class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
 
-    NumberFormat                      nf1              = NumberFormat.getInstance();
+    // NumberFormat                      nf1              = NumberFormat.getInstance();
     private final static NumberFormat PHYLIP_FORMATTER = new DecimalFormat( "0.000000" );
     final double[][]                  _values;
     final String[]                    _identifiers;
@@ -47,13 +47,17 @@ public class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
         _identifiers = new String[ size ];
     }
 
+    public final double[][] getValues() {
+        return _values;
+    }
+
     @Override
-    public String getIdentifier( final int i ) {
+    public final String getIdentifier( final int i ) {
         return _identifiers[ i ];
     }
 
     @Override
-    public int getIndex( final String identifier ) {
+    public final int getIndex( final String identifier ) {
         for( int i = 0; i < _identifiers.length; i++ ) {
             if ( getIdentifier( i ).equals( identifier ) ) {
                 return i;
@@ -63,14 +67,14 @@ public class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
     }
 
     @Override
-    public int getSize() {
+    public final int getSize() {
         return _values.length;
     }
 
     @Override
-    public double getValue( final int col, final int row ) {
+    public final double getValue( final int col, final int row ) {
         if ( col == row ) {
-            if ( col >= getSize() ) {
+            if ( col >= _values.length ) {
                 throw new IndexOutOfBoundsException( "" );
             }
             return 0.0;
@@ -81,7 +85,7 @@ public class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
         return _values[ col ][ row ];
     }
 
-    public void randomize( final long seed ) {
+    public final void randomize( final long seed ) {
         final java.util.Random r = new java.util.Random( seed );
         for( int j = 0; j < getSize(); ++j ) {
             for( int i = 0; i < j; ++i ) {
@@ -91,11 +95,11 @@ public class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
     }
 
     @Override
-    public void setIdentifier( final int i, final String identifier ) {
+    public final void setIdentifier( final int i, final String identifier ) {
         _identifiers[ i ] = identifier;
     }
 
-    public void setRow( final String s, final int row ) {
+    public final void setRow( final String s, final int row ) {
         final StringTokenizer tk = new StringTokenizer( s );
         int i = 0;
         while ( tk.hasMoreElements() ) {
@@ -105,7 +109,10 @@ public class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
     }
 
     @Override
-    public void setValue( final int col, final int row, final double d ) {
+    public final void setValue( final int col, final int row, final double d ) {
+        if ( d < 0 ) {
+            throw new IllegalArgumentException( "negative distance value" );
+        }
         if ( ( col == row ) && ( d != 0.0 ) ) {
             throw new IllegalArgumentException( "attempt to set a non-zero value on the diagonal of a symmetrical distance matrix" );
         }
@@ -115,7 +122,7 @@ public class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
         _values[ col ][ row ] = d;
     }
 
-    public void write( final Writer w ) throws IOException {
+    public final void write( final Writer w ) throws IOException {
         w.write( "    " );
         w.write( getSize() + "" );
         w.write( ForesterUtil.LINE_SEPARATOR );
@@ -130,18 +137,18 @@ public class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
             }
             for( int col = 0; col < getSize(); ++col ) {
                 w.write( PHYLIP_FORMATTER.format( getValue( col, row ) ) );
-                if ( col < getSize() - 1 ) {
+                if ( col < ( getSize() - 1 ) ) {
                     w.write( ' ' );
                     w.write( ' ' );
                 }
             }
-            if ( row < getSize() - 1 ) {
+            if ( row < ( getSize() - 1 ) ) {
                 w.write( ForesterUtil.LINE_SEPARATOR );
             }
         }
     }
 
-    private StringBuffer toPhylip() {
+    private final StringBuffer toPhylip() {
         final StringBuffer sb = new StringBuffer();
         sb.append( ' ' );
         sb.append( ' ' );
@@ -161,12 +168,12 @@ public class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
             //sb.append( "" );
             for( int col = 0; col < getSize(); ++col ) {
                 sb.append( PHYLIP_FORMATTER.format( getValue( col, row ) ) );
-                if ( col < getSize() - 1 ) {
+                if ( col < ( getSize() - 1 ) ) {
                     sb.append( ' ' );
                     sb.append( ' ' );
                 }
             }
-            if ( row < getSize() - 1 ) {
+            if ( row < ( getSize() - 1 ) ) {
                 sb.append( ForesterUtil.LINE_SEPARATOR );
             }
         }
@@ -174,12 +181,12 @@ public class BasicSymmetricalDistanceMatrix implements DistanceMatrix {
     }
 
     @Override
-    public String toString() {
+    public final String toString() {
         return toPhylip().toString();
     }
 
     @Override
-    public StringBuffer toStringBuffer( final Format format ) {
+    public final StringBuffer toStringBuffer( final Format format ) {
         switch ( format ) {
             case PHYLIP:
                 return toPhylip();