commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1032010 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/util/MathUtils.java main/java/org/apache/commons/math/util/Pair.java test/java/org/apache/commons/math/util/PairTest.java
Date Sat, 06 Nov 2010 11:38:08 GMT
Author: erans
Date: Sat Nov  6 11:38:08 2010
New Revision: 1032010

URL: http://svn.apache.org/viewvc?rev=1032010&view=rev
Log:
MATH-432
Made "Pair" immutable.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Pair.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/PairTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java?rev=1032010&r1=1032009&r2=1032010&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java Sat
Nov  6 11:38:08 2010
@@ -1993,8 +1993,8 @@ public final class MathUtils {
         }
 
         final int len = x.length;
-        final List<Map.Entry<Double, double[]>> list
-            = new ArrayList<Map.Entry<Double, double[]>>(len);
+        final List<Pair<Double, double[]>> list
+            = new ArrayList<Pair<Double, double[]>>(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<Double, double[]>(x[i], yValues));
         }
 
-        final Comparator<Map.Entry<Double, double[]>> comp
-            = new Comparator<Map.Entry<Double, double[]>>() {
-            public int compare(Map.Entry<Double, double[]> o1,
-                               Map.Entry<Double, double[]> o2) {
+        final Comparator<Pair<Double, double[]>> comp
+            = new Comparator<Pair<Double, double[]>>() {
+            public int compare(Pair<Double, double[]> o1,
+                               Pair<Double, double[]> 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<Double, double[]> e = list.get(i);
+            final Pair<Double, double[]> e = list.get(i);
             x[i] = e.getKey();
             final double[] yValues = e.getValue();
             for (int j = 0; j < yListLen; j++) {

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Pair.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Pair.java?rev=1032010&r1=1032009&r2=1032010&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Pair.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Pair.java Sat Nov
 6 11:38:08 2010
@@ -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 <K> Key type.
  * @param <V> Value type.
@@ -30,11 +29,11 @@ import java.util.Map;
  * @version $Revision$ $Date$
  * @since 3.0
  */
-public class Pair<K, V> implements Map.Entry<K, V> {
+public class Pair<K, V> {
     /** 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<K, V> implements Map.E
      *
      * @param entry Entry to copy.
      */
-    Pair(Map.Entry<? extends K, ? extends V> entry) {
+    Pair(Pair<? extends K, ? extends V> entry) {
         key = entry.getKey();
         value = entry.getValue();
     }
@@ -77,18 +76,6 @@ public class Pair<K, V> implements Map.E
     }
 
     /**
-     * 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.
      *
      * @param o Object.
@@ -99,17 +86,17 @@ public class Pair<K, V> implements Map.E
         if (o == null) {
             return false;
         }
-        if (!(o instanceof Map.Entry)) {
+        if (!(o instanceof Pair)) {
             return false;
         } else {
-            Map.Entry<? extends K, ? extends V> ome
-                = (Map.Entry<? extends K, ? extends V>) o;
+            Pair<? extends K, ? extends V> oP
+                = (Pair<? extends K, ? extends V>) 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()));
         }
     }
 

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/PairTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/PairTest.java?rev=1032010&r1=1032009&r2=1032010&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/PairTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/PairTest.java Sat
Nov  6 11:38:08 2010
@@ -27,10 +27,6 @@ public class PairTest {
             = new Pair<Integer, Double>(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<Integer, Double>(new Integer(1), new Double(2));
         Assert.assertFalse(p1.equals(p2));
 
-        Pair<Integer, Number> p3 = new Pair<Integer, Number>(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<Integer, Double>(new Integer(1), new Double(2));
+        Assert.assertTrue(p1.equals(p2));
     }
 }



Mime
View raw message