From 94ed46d6216e3b3fd195144660815396e6c42683 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Wed, 12 Apr 2017 14:42:11 +0100 Subject: [PATCH] JAL-2403 ensure array held in Matrix is immutable --- src/jalview/math/Matrix.java | 30 +++++++++++++++++++++++------- test/jalview/math/MatrixTest.java | 17 +++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/jalview/math/Matrix.java b/src/jalview/math/Matrix.java index 821fc66..b39d3c9 100755 --- a/src/jalview/math/Matrix.java +++ b/src/jalview/math/Matrix.java @@ -65,7 +65,8 @@ public class Matrix implements MatrixI } /** - * Creates a new Matrix object. For example + * Creates a new Matrix object containing a copy of the supplied array values. + * For example * *
    *   new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
@@ -85,13 +86,27 @@ public class Matrix implements MatrixI
   {
     this.rows = values.length;
     this.cols = this.rows == 0 ? 0 : values[0].length;
-    this.value = values;
+
+    /*
+     * make a copy of the values array, for immutability
+     */
+    this.value = new double[rows][];
+    int i = 0;
+    for (double[] row : values)
+    {
+      if (row != null)
+      {
+        value[i] = new double[row.length];
+        System.arraycopy(row, 0, value[i], 0, row.length);
+      }
+      i++;
+    }
   }
 
   /**
    * Returns a new matrix which is the transpose of this one
    * 
-   * @return DOCUMENT ME!
+   * @return
    */
   @Override
   public MatrixI transpose()
@@ -959,11 +974,12 @@ public class Matrix implements MatrixI
   }
 
   /**
-   * Multiply every entry in the matrix by the given value. This method is not
-   * thread-safe.
+   * Multiplies every entry in the matrix by the given value.
+   * 
+   * @param
    */
   @Override
-  public void multiply(double d)
+  public void multiply(double by)
   {
     for (double[] row : value)
     {
@@ -971,7 +987,7 @@ public class Matrix implements MatrixI
       {
         for (int i = 0; i < row.length; i++)
         {
-          row[i] *= d;
+          row[i] *= by;
         }
       }
     }
diff --git a/test/jalview/math/MatrixTest.java b/test/jalview/math/MatrixTest.java
index bd4108c..97ded5a 100644
--- a/test/jalview/math/MatrixTest.java
+++ b/test/jalview/math/MatrixTest.java
@@ -1,6 +1,7 @@
 package jalview.math;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotSame;
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
@@ -188,6 +189,7 @@ public class MatrixTest
     }
     Matrix m1 = new Matrix(in);
     Matrix m2 = (Matrix) m1.copy();
+    assertNotSame(m1, m2);
     assertTrue(matrixEquals(m1, m2));
   }
 
@@ -514,4 +516,19 @@ public class MatrixTest
     assertEquals(m.getValue(1, 1), 8d, DELTA);
     assertEquals(m.getValue(1, 2), 30d, DELTA);
   }
+
+  @Test(groups = "Functional")
+  public void testConstructor()
+  {
+    double[][] values = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
+    Matrix m = new Matrix(values);
+    assertEquals(m.getValue(0, 0), 1d, DELTA);
+
+    /*
+     * verify the matrix has a copy of the original array
+     */
+    assertNotSame(values[0], m.getRow(0));
+    values[0][0] = -1d;
+    assertEquals(m.getValue(0, 0), 1d, DELTA); // unchanged
+  }
 }
-- 
1.7.10.2