From 5b5e256d978715cebc8e7bc6bc9a5936558e19d0 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Fri, 5 Nov 2010 14:13:41 +0000 Subject: [PATCH] MATH-432 New class "Pair" as a replacement for the standard class "AbstractMap.SimpleEntry" (available in Java 1.6). git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1031574 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/math/util/Pair.java | 125 ++++++++++++++++++ src/site/xdoc/changes.xml | 4 + .../apache/commons/math/util/PairTest.java | 54 ++++++++ 3 files changed, 183 insertions(+) create mode 100644 src/main/java/org/apache/commons/math/util/Pair.java create mode 100644 src/test/java/org/apache/commons/math/util/PairTest.java diff --git a/src/main/java/org/apache/commons/math/util/Pair.java b/src/main/java/org/apache/commons/math/util/Pair.java new file mode 100644 index 000000000..03423c42f --- /dev/null +++ b/src/main/java/org/apache/commons/math/util/Pair.java @@ -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 Key type. + * @param Value type. + * + * @version $Revision$ $Date$ + * @since 3.0 + */ +public class Pair implements Map.Entry { + /** 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 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 ome + = (Map.Entry) 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()); + } +} diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index 77253504c..1d1882ed7 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -52,6 +52,10 @@ The type attribute can be add,update,fix,remove. If the output is not quite correct, check for invisible trailing spaces! --> + + Created a generic "Pair" class to replace the "AbstractMap.SimpleEntry" + that is only available in Java 6 and later. + Class "DirectSearchOptimizer" (and subclasses "NelderMead" and "MultiDirectional") was refactored into new classes: diff --git a/src/test/java/org/apache/commons/math/util/PairTest.java b/src/test/java/org/apache/commons/math/util/PairTest.java new file mode 100644 index 000000000..9f5fee614 --- /dev/null +++ b/src/test/java/org/apache/commons/math/util/PairTest.java @@ -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 p + = 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 + public void testEquals() { + Pair p1 = new Pair(null, null); + Assert.assertFalse(p1.equals(null)); + + Pair p2 = new Pair(null, null); + Assert.assertTrue(p1.equals(p2)); + + 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)); + } +}