This is an automated email from the ASF dual-hosted git repository. erans pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-math.git
commit b5d21665ff0c3a420564537cd1da88c8f77ab5a5 Author: Gilles Sadowski <[email protected]> AuthorDate: Wed Aug 4 06:47:27 2021 +0200 MATH-1621: Override "equals" and "toString". --- .../commons/math4/legacy/optim/PointValuePair.java | 20 ++++++++++++++++++++ .../math4/legacy/optim/PointValuePairTest.java | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/PointValuePair.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/PointValuePair.java index 6019e46..35f883c 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/PointValuePair.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/PointValuePair.java @@ -16,6 +16,7 @@ */ package org.apache.commons.math4.legacy.optim; +import java.util.Arrays; import java.io.Serializable; import org.apache.commons.math4.legacy.core.Pair; @@ -80,6 +81,25 @@ public class PointValuePair extends Pair<double[], Double> implements Serializab return getKey(); } + @Override + public boolean equals(Object o) { + if (o instanceof PointValuePair) { + final PointValuePair other = (PointValuePair) o; + + return getValue().equals(other.getValue()) ? + Arrays.equals(getPointRef(), + other.getPointRef()) : + false; + } + + return false; + } + + @Override + public String toString() { + return "[" + Arrays.toString(getPointRef()) + ", " + getValue() + "]"; + } + /** * Replace the instance with a data transfer object for serialization. * @return data transfer object that will be serialized diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/PointValuePairTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/PointValuePairTest.java index 8d0db0f..23a68db 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/PointValuePairTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/PointValuePairTest.java @@ -32,4 +32,23 @@ public class PointValuePairTest { Assert.assertEquals(pv1.getValue(), pv2.getValue(), 1.0e-15); } + @Test + public void testEquals() { + final double[] p1 = new double[] { 1 }; + final PointValuePair pv1 = new PointValuePair(p1, 2); + Assert.assertFalse(pv1.equals(null)); + + final PointValuePair pv2 = new PointValuePair(pv1.getPointRef(), 3); + // Same array reference, different objective values. + Assert.assertFalse(pv1.equals(pv2)); + + final PointValuePair pv3 = new PointValuePair(pv2.getPoint(), pv2.getValue()); + // Different array reference, same array values, same objective values. + Assert.assertTrue(pv2.equals(pv3)); + + final double[] p2 = new double[] { p1[0] + 1 }; + final PointValuePair pv4 = new PointValuePair(p2, pv2.getValue()); + // Different array values, same objective values. + Assert.assertFalse(pv2.equals(pv4)); + } }
