From 86f56c7a53d72066544ff74c893188d304598bec Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 21 Feb 2017 17:01:37 +0000 Subject: [PATCH] JAL-2416 ScoreMatrix.getMatrix() returns a defensive copy to keep the class invariant --- src/jalview/analysis/scoremodels/ScoreMatrix.java | 31 +++++++++++++++++--- .../analysis/scoremodels/ScoreMatrixTest.java | 25 ++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/jalview/analysis/scoremodels/ScoreMatrix.java b/src/jalview/analysis/scoremodels/ScoreMatrix.java index 13ce6a8..9da28eb 100644 --- a/src/jalview/analysis/scoremodels/ScoreMatrix.java +++ b/src/jalview/analysis/scoremodels/ScoreMatrix.java @@ -60,6 +60,8 @@ public class ScoreMatrix implements PairwiseScoreModelI */ private boolean peptide; + private boolean symmetric; + /** * Constructor given a name, symbol alphabet, and matrix of scores for pairs * of symbols. The matrix should be square and of the same size as the @@ -161,16 +163,22 @@ public class ScoreMatrix implements PairwiseScoreModelI } /** - * Returns the score matrix as used in getPairwiseScore. If using this matrix - * directly, callers must also call getMatrixIndex in - * order to get the matrix index for each character (symbol). + * Returns a copy of the score matrix as used in getPairwiseScore. If using + * this matrix directly, callers must also call + * getMatrixIndex in order to get the matrix index for each + * character (symbol). * * @return * @see #getMatrixIndex(char) */ public float[][] getMatrix() { - return matrix; + float[][] v = new float[matrix.length][matrix.length]; + for (int i = 0; i < matrix.length; i++) + { + v[i] = Arrays.copyOf(matrix[i], matrix[i].length); + } + return v; } /** @@ -383,4 +391,19 @@ public class ScoreMatrix implements PairwiseScoreModelI } return false; } + + public boolean isSymmetric() + { + return symmetric; + } + + /** + * Returns the alphabet the matrix scores for, as a string of characters + * + * @return + */ + public String getSymbols() + { + return new String(symbols); + } } diff --git a/test/jalview/analysis/scoremodels/ScoreMatrixTest.java b/test/jalview/analysis/scoremodels/ScoreMatrixTest.java index 002084d..ae5ad5d 100644 --- a/test/jalview/analysis/scoremodels/ScoreMatrixTest.java +++ b/test/jalview/analysis/scoremodels/ScoreMatrixTest.java @@ -2,6 +2,7 @@ package jalview.analysis.scoremodels; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNotSame; import static org.testng.Assert.assertTrue; import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; @@ -12,6 +13,7 @@ import jalview.math.MatrixI; import java.io.IOException; import java.net.MalformedURLException; +import java.util.Arrays; import org.testng.annotations.Test; @@ -121,6 +123,19 @@ public class ScoreMatrixTest } @Test(groups = "Functional") + public void testGetMatrix() + { + ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); + float[][] m = sm.getMatrix(); + assertEquals(m.length, sm.getSize()); + assertEquals(m[2][4], -3f); + // verify a defensive copy is returned + float[][] m2 = sm.getMatrix(); + assertNotSame(m, m2); + assertTrue(Arrays.deepEquals(m, m2)); + } + + @Test(groups = "Functional") public void testGetMatrixIndex() { ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); @@ -219,4 +234,14 @@ public class ScoreMatrixTest assertNotNull(sm2); assertTrue(sm2.equals(sm)); } + + @Test(groups = "Functional") + public void testEauals() + { + ScoreMatrix sm = ScoreModels.getInstance().getBlosum62(); + ScoreMatrix sm2 = new ScoreMatrix(sm.getName(), sm.getSymbols() + .toCharArray(), sm.getMatrix()); + assertTrue(sm.equals(sm2)); + assertEquals(sm.hashCode(), sm2.hashCode()); + } } -- 1.7.10.2