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
This commit is contained in:
parent
a6a96b559d
commit
5b5e256d97
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -52,6 +52,10 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
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:
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue