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 int len = x.length;
|
||||||
final List<Map.Entry<Double, double[]>> list
|
final List<Pair<Double, double[]>> list
|
||||||
= new ArrayList<Map.Entry<Double, double[]>>(len);
|
= new ArrayList<Pair<Double, double[]>>(len);
|
||||||
|
|
||||||
final int yListLen = yList.length;
|
final int yListLen = yList.length;
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
|
@ -2009,10 +2009,10 @@ public final class MathUtils {
|
||||||
list.add(new Pair<Double, double[]>(x[i], yValues));
|
list.add(new Pair<Double, double[]>(x[i], yValues));
|
||||||
}
|
}
|
||||||
|
|
||||||
final Comparator<Map.Entry<Double, double[]>> comp
|
final Comparator<Pair<Double, double[]>> comp
|
||||||
= new Comparator<Map.Entry<Double, double[]>>() {
|
= new Comparator<Pair<Double, double[]>>() {
|
||||||
public int compare(Map.Entry<Double, double[]> o1,
|
public int compare(Pair<Double, double[]> o1,
|
||||||
Map.Entry<Double, double[]> o2) {
|
Pair<Double, double[]> o2) {
|
||||||
int val;
|
int val;
|
||||||
switch (dir) {
|
switch (dir) {
|
||||||
case INCREASING:
|
case INCREASING:
|
||||||
|
@ -2032,7 +2032,7 @@ public final class MathUtils {
|
||||||
Collections.sort(list, comp);
|
Collections.sort(list, comp);
|
||||||
|
|
||||||
for (int i = 0; i < len; i++) {
|
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();
|
x[i] = e.getKey();
|
||||||
final double[] yValues = e.getValue();
|
final double[] yValues = e.getValue();
|
||||||
for (int j = 0; j < yListLen; j++) {
|
for (int j = 0; j < yListLen; j++) {
|
||||||
|
|
|
@ -16,13 +16,12 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.math.util;
|
package org.apache.commons.math.util;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic pair.
|
* Generic pair.
|
||||||
* It is provided as a replacement for the standard
|
* It is provided as a replacement for the standard
|
||||||
* {@code AbstractMap.SimpleEntry} that is available only in Java 1.6
|
* {@code AbstractMap.SimpleEntry} that is available only in Java 1.6
|
||||||
* and later.
|
* and later.
|
||||||
|
* Immutable class.
|
||||||
*
|
*
|
||||||
* @param <K> Key type.
|
* @param <K> Key type.
|
||||||
* @param <V> Value type.
|
* @param <V> Value type.
|
||||||
|
@ -30,11 +29,11 @@ import java.util.Map;
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class Pair<K, V> implements Map.Entry<K, V> {
|
public class Pair<K, V> {
|
||||||
/** Key. */
|
/** Key. */
|
||||||
private K key;
|
private final K key;
|
||||||
/** Value. */
|
/** Value. */
|
||||||
private V value;
|
private final V value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an entry representing a mapping from the specified key to the
|
* 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.
|
* @param entry Entry to copy.
|
||||||
*/
|
*/
|
||||||
Pair(Map.Entry<? extends K, ? extends V> entry) {
|
Pair(Pair<? extends K, ? extends V> entry) {
|
||||||
key = entry.getKey();
|
key = entry.getKey();
|
||||||
value = entry.getValue();
|
value = entry.getValue();
|
||||||
}
|
}
|
||||||
|
@ -76,18 +75,6 @@ public class Pair<K, V> implements Map.Entry<K, V> {
|
||||||
return value;
|
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.
|
* 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) {
|
if (o == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!(o instanceof Map.Entry)) {
|
if (!(o instanceof Pair)) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
Map.Entry<? extends K, ? extends V> ome
|
Pair<? extends K, ? extends V> oP
|
||||||
= (Map.Entry<? extends K, ? extends V>) o;
|
= (Pair<? extends K, ? extends V>) o;
|
||||||
return (key == null ?
|
return (key == null ?
|
||||||
ome.getKey() == null :
|
oP.getKey() == null :
|
||||||
key.equals(ome.getKey())) &&
|
key.equals(oP.getKey())) &&
|
||||||
(value == null ?
|
(value == null ?
|
||||||
ome.getValue() == null :
|
oP.getValue() == null :
|
||||||
value.equals(ome.getValue()));
|
value.equals(oP.getValue()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,10 +27,6 @@ public class PairTest {
|
||||||
= new Pair<Integer, Double>(new Integer(1), new Double(2));
|
= new Pair<Integer, Double>(new Integer(1), new Double(2));
|
||||||
Assert.assertEquals(new Integer(1), p.getKey());
|
Assert.assertEquals(new Integer(1), p.getKey());
|
||||||
Assert.assertEquals(new Double(2), p.getValue(), Math.ulp(1d));
|
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
|
@Test
|
||||||
|
@ -44,11 +40,7 @@ public class PairTest {
|
||||||
p1 = new Pair<Integer, Double>(new Integer(1), new Double(2));
|
p1 = new Pair<Integer, Double>(new Integer(1), new Double(2));
|
||||||
Assert.assertFalse(p1.equals(p2));
|
Assert.assertFalse(p1.equals(p2));
|
||||||
|
|
||||||
Pair<Integer, Number> p3 = new Pair<Integer, Number>(new Integer(1), null);
|
p2 = new Pair<Integer, Double>(new Integer(1), new Double(2));
|
||||||
Assert.assertFalse(p1.equals(p3));
|
Assert.assertTrue(p1.equals(p2));
|
||||||
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