commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1031574 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/util/Pair.java site/xdoc/changes.xml test/java/org/apache/commons/math/util/PairTest.java
Date Fri, 05 Nov 2010 14:13:41 GMT
Author: erans
Date: Fri Nov  5 14:13:41 2010
New Revision: 1031574

URL: http://svn.apache.org/viewvc?rev=1031574&view=rev
Log:
MATH-432
New class "Pair" as a replacement for the standard class
"AbstractMap.SimpleEntry" (available in Java 1.6).

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Pair.java   (with
props)
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/PairTest.java   (with
props)
Modified:
    commons/proper/math/trunk/src/site/xdoc/changes.xml

Added: 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=1031574&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Pair.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Pair.java Fri Nov
 5 14:13:41 2010
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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.
+ *
+ * @param <K> Key type.
+ * @param <V> Value type.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public class Pair<K, V> implements Map.Entry<K, V> {
+    /** Key. */
+    private K key;
+    /** Value. */
+    private V value;
+
+    /**
+     * Create an entry representing a mapping from the specified key to the
+     * specified value.
+     *
+     * @param k Key.
+     * @param v Value.
+     */
+    public Pair(K k, V v) {
+        key = k;
+        value = v;
+    }
+
+    /**
+     * Create an entry representing the same mapping as the specified entry.
+     *
+     * @param entry Entry to copy.
+     */
+    Pair(Map.Entry<? extends K, ? extends V> entry) {
+        key = entry.getKey();
+        value = entry.getValue();
+    }
+
+    /**
+     * Get the key.
+     *
+     * @return the key.
+     */
+    public K getKey() {
+        return key;
+    }
+
+    /**
+     * Get the value.
+     *
+     * @return the key.
+     */
+    public V getValue() {
+        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.
+     *
+     * @param o Object.
+     * @return {@code true} if the given object is also a map entry and
+     * the two entries represent the same mapping.
+     */
+    public boolean equals(Object o) {
+        if (o == null) {
+            return false;
+        }
+        if (!(o instanceof Map.Entry)) {
+            return false;
+        } else {
+            Map.Entry<? extends K, ? extends V> ome
+                = (Map.Entry<? extends K, ? extends V>) o;
+            return (key == null ?
+                    ome.getKey() == null :
+                    key.equals(ome.getKey())) &&
+                (value == null ?
+                 ome.getValue() == null :
+                 value.equals(ome.getValue()));
+        }
+    }
+
+    /**
+     * Compute a hash code.
+     *
+     * @return the hash code value.
+     */
+    public int hashCode() {
+        return (key == null ? 0 : key.hashCode()) ^
+            (value == null ? 0 : value.hashCode());
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Pair.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1031574&r1=1031573&r2=1031574&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri Nov  5 14:13:41 2010
@@ -52,6 +52,10 @@ The <action> type attribute can be add,u
     If the output is not quite correct, check for invisible trailing spaces!
      -->
     <release version="3.0" date="TBD" description="TBD">
+      <action dev="erans" type="fix" issue="MATH-432">
+        Created a generic "Pair" class to replace the "AbstractMap.SimpleEntry"
+        that is only available in Java 6 and later.
+      </action>
       <action dev="erans" type="fix" issue="MATH-428">
         Class "DirectSearchOptimizer" (and subclasses "NelderMead"
         and "MultiDirectional") was refactored into new classes:

Added: 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=1031574&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/PairTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/PairTest.java Fri
Nov  5 14:13:41 2010
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
+ * or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.apache.commons.math.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test for {@link Pair}.
+ */
+public class PairTest {
+
+    @Test
+    public void testAccessor() {
+        final Pair<Integer, Double> p
+            = 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
+    public void testEquals() {
+        Pair<Integer, Double> p1 = new Pair<Integer, Double>(null, null);
+        Assert.assertFalse(p1.equals(null));
+
+        Pair<Integer, Double> p2 = new Pair<Integer, Double>(null, null);
+        Assert.assertTrue(p1.equals(p2));
+
+        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));
+    }
+}

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/PairTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message