MATH-432
Made "Pair" immutable. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1032010 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
cfce6350fe
commit
cf7000a85c
|
@ -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++) {
|
||||
|
|
|
@ -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.Entry<K, V> {
|
|||
*
|
||||
* @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();
|
||||
}
|
||||
|
@ -76,18 +75,6 @@ public class Pair<K, V> implements Map.Entry<K, V> {
|
|||
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<K, V> implements Map.Entry<K, V> {
|
|||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue