diff --git a/src/main/java/org/apache/commons/math/util/MathUtils.java b/src/main/java/org/apache/commons/math/util/MathUtils.java index 051e19f3c..cf2d861d5 100644 --- a/src/main/java/org/apache/commons/math/util/MathUtils.java +++ b/src/main/java/org/apache/commons/math/util/MathUtils.java @@ -1993,8 +1993,8 @@ public final class MathUtils { } final int len = x.length; - final List> list - = new ArrayList>(len); + final List> list + = new ArrayList>(len); final int yListLen = yList.length; for (int i = 0; i < len; i++) { @@ -2009,10 +2009,10 @@ public final class MathUtils { list.add(new Pair(x[i], yValues)); } - final Comparator> comp - = new Comparator>() { - public int compare(Map.Entry o1, - Map.Entry o2) { + final Comparator> comp + = new Comparator>() { + public int compare(Pair o1, + Pair o2) { int val; switch (dir) { case INCREASING: @@ -2032,7 +2032,7 @@ public final class MathUtils { Collections.sort(list, comp); for (int i = 0; i < len; i++) { - final Map.Entry e = list.get(i); + final Pair e = list.get(i); x[i] = e.getKey(); final double[] yValues = e.getValue(); for (int j = 0; j < yListLen; j++) { diff --git a/src/main/java/org/apache/commons/math/util/Pair.java b/src/main/java/org/apache/commons/math/util/Pair.java index 03423c42f..dcaa5127a 100644 --- a/src/main/java/org/apache/commons/math/util/Pair.java +++ b/src/main/java/org/apache/commons/math/util/Pair.java @@ -16,13 +16,12 @@ */ package org.apache.commons.math.util; -import java.util.Map; - /** * Generic pair. * It is provided as a replacement for the standard * {@code AbstractMap.SimpleEntry} that is available only in Java 1.6 * and later. + * Immutable class. * * @param Key type. * @param Value type. @@ -30,11 +29,11 @@ import java.util.Map; * @version $Revision$ $Date$ * @since 3.0 */ -public class Pair implements Map.Entry { +public class Pair { /** Key. */ - private K key; + private final K key; /** Value. */ - private V value; + private final V value; /** * Create an entry representing a mapping from the specified key to the @@ -53,7 +52,7 @@ public class Pair implements Map.Entry { * * @param entry Entry to copy. */ - Pair(Map.Entry entry) { + Pair(Pair entry) { key = entry.getKey(); value = entry.getValue(); } @@ -76,18 +75,6 @@ public class Pair implements Map.Entry { return value; } - /** - * Set the value. - * - * @param v Value to be stored. - * @return the old value. - */ - public V setValue(V v) { - V old = value; - value = v; - return old; - } - /** * Compare the specified object with this entry for equality. * @@ -99,17 +86,17 @@ public class Pair implements Map.Entry { if (o == null) { return false; } - if (!(o instanceof Map.Entry)) { + if (!(o instanceof Pair)) { return false; } else { - Map.Entry ome - = (Map.Entry) o; + Pair oP + = (Pair) o; return (key == null ? - ome.getKey() == null : - key.equals(ome.getKey())) && + oP.getKey() == null : + key.equals(oP.getKey())) && (value == null ? - ome.getValue() == null : - value.equals(ome.getValue())); + oP.getValue() == null : + value.equals(oP.getValue())); } } diff --git a/src/test/java/org/apache/commons/math/util/PairTest.java b/src/test/java/org/apache/commons/math/util/PairTest.java index 9f5fee614..6582a1a0c 100644 --- a/src/test/java/org/apache/commons/math/util/PairTest.java +++ b/src/test/java/org/apache/commons/math/util/PairTest.java @@ -27,10 +27,6 @@ public class PairTest { = new Pair(new Integer(1), new Double(2)); Assert.assertEquals(new Integer(1), p.getKey()); Assert.assertEquals(new Double(2), p.getValue(), Math.ulp(1d)); - - final Double old = p.setValue(new Double(3)); - Assert.assertEquals(new Double(2), old, Math.ulp(1d)); - Assert.assertEquals(new Double(3), p.getValue(), Math.ulp(1d)); } @Test @@ -44,11 +40,7 @@ public class PairTest { p1 = new Pair(new Integer(1), new Double(2)); Assert.assertFalse(p1.equals(p2)); - Pair p3 = new Pair(new Integer(1), null); - Assert.assertFalse(p1.equals(p3)); - p3.setValue(new Double(3)); - Assert.assertFalse(p1.equals(p3)); - p3.setValue(new Double(2)); - Assert.assertTrue(p1.equals(p3)); + p2 = new Pair(new Integer(1), new Double(2)); + Assert.assertTrue(p1.equals(p2)); } }