From 4e50bbcb90a44c3921dbe9cfea32fde4a83399a2 Mon Sep 17 00:00:00 2001
From: Luc Maisonobe
Date: Sun, 18 May 2008 19:37:23 +0000
Subject: [PATCH] Replaced Raw types by Parameterized types where appropriate.
The various ArrayList, HashSet and HashMap used in math have be restricted to
the content they are intended for. This removes lots of warnings that have
appeared after the switch to Java 5, and improves safety (for example the
categories used by OneWayAnova are guaranteed to be double arrays at compile
time). Two difficult cases where not handled here: the Frequency class and
the Fitness interface. The first one mixes types and needs to be studied
before any change is attempted. The second one generates some side effects on
the overall package which is still under development.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_0@657612 13f79535-47bb-0310-9956-ffa450edef68
---
.../estimation/SimpleEstimationProblem.java | 14 +++----
.../commons/math/fraction/Fraction.java | 9 ++---
.../math/ode/AdaptiveStepsizeIntegrator.java | 4 +-
.../math/ode/ContinuousOutputModel.java | 40 ++++++++-----------
.../math/ode/FirstOrderIntegrator.java | 2 +-
.../math/ode/RungeKuttaIntegrator.java | 4 +-
.../math/ode/SwitchingFunctionsHandler.java | 34 ++++++++--------
.../optimization/DirectSearchOptimizer.java | 9 ++---
.../math/random/EmpiricalDistribution.java | 3 +-
.../random/EmpiricalDistributionImpl.java | 24 +++++------
.../commons/math/random/RandomData.java | 2 +-
.../commons/math/random/RandomDataImpl.java | 2 +-
.../math/stat/inference/OneWayAnova.java | 6 +--
.../math/stat/inference/OneWayAnovaImpl.java | 30 +++++---------
.../math/stat/inference/TestUtils.java | 6 +--
.../commons/math/util/TransformerMap.java | 22 +++++-----
.../estimation/GaussNewtonEstimatorTest.java | 33 +++++++--------
.../LevenbergMarquardtEstimatorTest.java | 35 +++++++---------
.../random/EmpiricalDistributionTest.java | 13 +++---
.../commons/math/random/RandomDataTest.java | 15 +++----
.../stat/data/CertifiedDataAbstractTest.java | 14 +++----
.../stat/descriptive/ListUnivariateImpl.java | 8 ++--
.../descriptive/ListUnivariateImplTest.java | 6 +--
.../MixedListUnivariateImplTest.java | 8 ++--
.../math/stat/inference/OneWayAnovaTest.java | 28 ++++---------
.../math/stat/inference/TestUtilsTest.java | 2 +-
26 files changed, 160 insertions(+), 213 deletions(-)
diff --git a/src/java/org/apache/commons/math/estimation/SimpleEstimationProblem.java b/src/java/org/apache/commons/math/estimation/SimpleEstimationProblem.java
index 304203e21..1f32600f3 100644
--- a/src/java/org/apache/commons/math/estimation/SimpleEstimationProblem.java
+++ b/src/java/org/apache/commons/math/estimation/SimpleEstimationProblem.java
@@ -18,7 +18,6 @@
package org.apache.commons.math.estimation;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.List;
/**
@@ -46,8 +45,8 @@ public class SimpleEstimationProblem implements EstimationProblem {
* Build an empty instance without parameters nor measurements.
*/
public SimpleEstimationProblem() {
- parameters = new ArrayList();
- measurements = new ArrayList();
+ parameters = new ArrayList();
+ measurements = new ArrayList();
}
/**
@@ -65,9 +64,8 @@ public class SimpleEstimationProblem implements EstimationProblem {
public EstimatedParameter[] getUnboundParameters() {
// filter the unbound parameters
- List unbound = new ArrayList(parameters.size());
- for (Iterator iterator = parameters.iterator(); iterator.hasNext();) {
- EstimatedParameter p = (EstimatedParameter) iterator.next();
+ List unbound = new ArrayList(parameters.size());
+ for (EstimatedParameter p : parameters) {
if (! p.isBound()) {
unbound.add(p);
}
@@ -102,9 +100,9 @@ public class SimpleEstimationProblem implements EstimationProblem {
}
/** Estimated parameters. */
- private final List parameters;
+ private final List parameters;
/** Measurements. */
- private final List measurements;
+ private final List measurements;
}
diff --git a/src/java/org/apache/commons/math/fraction/Fraction.java b/src/java/org/apache/commons/math/fraction/Fraction.java
index 4f9015cd9..678d9de50 100644
--- a/src/java/org/apache/commons/math/fraction/Fraction.java
+++ b/src/java/org/apache/commons/math/fraction/Fraction.java
@@ -25,7 +25,7 @@ import org.apache.commons.math.util.MathUtils;
* @since 1.1
* @version $Revision$ $Date$
*/
-public class Fraction extends Number implements Comparable {
+public class Fraction extends Number implements Comparable {
/** A fraction representing "1 / 1". */
public static final Fraction ONE = new Fraction(1, 1);
@@ -34,7 +34,7 @@ public class Fraction extends Number implements Comparable {
public static final Fraction ZERO = new Fraction(0, 1);
/** Serializable version identifier */
- private static final long serialVersionUID = -8958519416450949235L;
+ private static final long serialVersionUID = -5731055832688548463L;
/** The denominator. */
private final int denominator;
@@ -246,13 +246,12 @@ public class Fraction extends Number implements Comparable {
* @return -1 if this is less than object, +1 if this is greater
* than object, 0 if they are equal.
*/
- public int compareTo(Object object) {
+ public int compareTo(Fraction object) {
int ret = 0;
if (this != object) {
- Fraction other = (Fraction)object;
double first = doubleValue();
- double second = other.doubleValue();
+ double second = object.doubleValue();
if (first < second) {
ret = -1;
diff --git a/src/java/org/apache/commons/math/ode/AdaptiveStepsizeIntegrator.java b/src/java/org/apache/commons/math/ode/AdaptiveStepsizeIntegrator.java
index 2f42334d0..ca2aa0f4d 100644
--- a/src/java/org/apache/commons/math/ode/AdaptiveStepsizeIntegrator.java
+++ b/src/java/org/apache/commons/math/ode/AdaptiveStepsizeIntegrator.java
@@ -163,7 +163,7 @@ public abstract class AdaptiveStepsizeIntegrator
double maxCheckInterval,
double convergence,
int maxIterationCount) {
- switchesHandler.add(function, maxCheckInterval, convergence, maxIterationCount);
+ switchesHandler.addSwitchingFunction(function, maxCheckInterval, convergence, maxIterationCount);
}
/** Get all the switching functions that have been added to the integrator.
@@ -171,7 +171,7 @@ public abstract class AdaptiveStepsizeIntegrator
* @see #addSwitchingFunction(SwitchingFunction, double, double, int)
* @see #clearSwitchingFunctions()
*/
- public Collection getSwitchingFunctions() {
+ public Collection getSwitchingFunctions() {
return switchesHandler.getSwitchingFunctions();
}
diff --git a/src/java/org/apache/commons/math/ode/ContinuousOutputModel.java b/src/java/org/apache/commons/math/ode/ContinuousOutputModel.java
index 7fa22fec4..d1bb7d7ab 100644
--- a/src/java/org/apache/commons/math/ode/ContinuousOutputModel.java
+++ b/src/java/org/apache/commons/math/ode/ContinuousOutputModel.java
@@ -18,7 +18,6 @@
package org.apache.commons.math.ode;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.List;
import java.io.Serializable;
@@ -45,14 +44,14 @@ import java.io.Serializable;
* get the model value at any time or to navigate through the
* data).
*
- *
If problem modelization is done with several separate
+ *
*/
- public double anovaFValue(Collection categoryData)
+ public double anovaFValue(Collection categoryData)
throws IllegalArgumentException, MathException {
AnovaStats a = anovaStats(categoryData);
return a.F;
@@ -81,7 +79,7 @@ public class OneWayAnovaImpl implements OneWayAnova {
* where F is the F value and cumulativeProbability
* is the commons-math implementation of the F distribution.
*/
- public double anovaPValue(Collection categoryData)
+ public double anovaPValue(Collection categoryData)
throws IllegalArgumentException, MathException {
AnovaStats a = anovaStats(categoryData);
FDistribution fdist = new FDistributionImpl(a.dfbg, a.dfwg);
@@ -99,7 +97,7 @@ public class OneWayAnovaImpl implements OneWayAnova {
* is the commons-math implementation of the F distribution.
*
True is returned iff the estimated p-value is less than alpha.
*/
- public boolean anovaTest(Collection categoryData, double alpha)
+ public boolean anovaTest(Collection categoryData, double alpha)
throws IllegalArgumentException, MathException {
if ((alpha <= 0) || (alpha > 0.5)) {
throw new IllegalArgumentException("bad significance level: " + alpha);
@@ -118,7 +116,7 @@ public class OneWayAnovaImpl implements OneWayAnova {
* preconditions specified in the interface definition
* @throws MathException if an error occurs computing the Anova stats
*/
- private AnovaStats anovaStats(Collection categoryData)
+ private AnovaStats anovaStats(Collection categoryData)
throws IllegalArgumentException, MathException {
// check if we have enough categories
@@ -128,14 +126,7 @@ public class OneWayAnovaImpl implements OneWayAnova {
}
// check if each category has enough data and all is double[]
- for (Iterator iterator = categoryData.iterator(); iterator.hasNext();) {
- double[] array;
- try {
- array = (double[])iterator.next();
- } catch (ClassCastException ex) {
- throw new IllegalArgumentException(
- "ANOVA: categoryData contains non-double[] elements.");
- }
+ for (double[] array : categoryData) {
if (array.length <= 1) {
throw new IllegalArgumentException(
"ANOVA: one element of categoryData has fewer than 2 values.");
@@ -148,8 +139,7 @@ public class OneWayAnovaImpl implements OneWayAnova {
SumOfSquares totsumsq = new SumOfSquares();
int totnum = 0;
- for (Iterator iterator = categoryData.iterator(); iterator.hasNext();) {
- double[] data = (double[])iterator.next();
+ for (double[] data : categoryData) {
Sum sum = new Sum();
SumOfSquares sumsq = new SumOfSquares();
diff --git a/src/java/org/apache/commons/math/stat/inference/TestUtils.java b/src/java/org/apache/commons/math/stat/inference/TestUtils.java
index 18a900156..e5178095f 100644
--- a/src/java/org/apache/commons/math/stat/inference/TestUtils.java
+++ b/src/java/org/apache/commons/math/stat/inference/TestUtils.java
@@ -382,7 +382,7 @@ public class TestUtils {
*
* @since 1.2
*/
- public static double oneWayAnovaFValue(Collection categoryData)
+ public static double oneWayAnovaFValue(Collection categoryData)
throws IllegalArgumentException, MathException {
return oneWayAnova.anovaFValue(categoryData);
}
@@ -392,7 +392,7 @@ public class TestUtils {
*
* @since 1.2
*/
- public static double oneWayAnovaPValue(Collection categoryData)
+ public static double oneWayAnovaPValue(Collection categoryData)
throws IllegalArgumentException, MathException {
return oneWayAnova.anovaPValue(categoryData);
}
@@ -402,7 +402,7 @@ public class TestUtils {
*
* @since 1.2
*/
- public static boolean oneWayAnovaTest(Collection categoryData, double alpha)
+ public static boolean oneWayAnovaTest(Collection categoryData, double alpha)
throws IllegalArgumentException, MathException {
return oneWayAnova.anovaTest(categoryData, alpha);
}
diff --git a/src/java/org/apache/commons/math/util/TransformerMap.java b/src/java/org/apache/commons/math/util/TransformerMap.java
index c28fee3f3..2c8ea5371 100644
--- a/src/java/org/apache/commons/math/util/TransformerMap.java
+++ b/src/java/org/apache/commons/math/util/TransformerMap.java
@@ -34,8 +34,8 @@ import org.apache.commons.math.MathException;
public class TransformerMap implements NumberTransformer, Serializable {
/** Serializable version identifier */
- private static final long serialVersionUID = -942772950698439883L;
-
+ private static final long serialVersionUID = 4605318041528645258L;
+
/**
* A default Number Transformer for Numbers and numeric Strings.
*/
@@ -44,13 +44,13 @@ public class TransformerMap implements NumberTransformer, Serializable {
/**
* The internal Map.
*/
- private Map map = null;
+ private Map, NumberTransformer> map = null;
/**
- *
+ * Build a map containing only the default transformer.
*/
public TransformerMap() {
- map = new HashMap();
+ map = new HashMap, NumberTransformer>();
defaultTransformer = new DefaultTransformer();
}
@@ -59,7 +59,7 @@ public class TransformerMap implements NumberTransformer, Serializable {
* @param key Class to check
* @return true|false
*/
- public boolean containsClass(Class key) {
+ public boolean containsClass(Class> key) {
return map.containsKey(key);
}
@@ -78,7 +78,7 @@ public class TransformerMap implements NumberTransformer, Serializable {
* @param key The Class of the object
* @return the mapped NumberTransformer or null.
*/
- public NumberTransformer getTransformer(Class key) {
+ public NumberTransformer getTransformer(Class> key) {
return (NumberTransformer) map.get(key);
}
@@ -90,7 +90,7 @@ public class TransformerMap implements NumberTransformer, Serializable {
* @param transformer The NumberTransformer
* @return the replaced transformer if one is present
*/
- public Object putTransformer(Class key, NumberTransformer transformer) {
+ public NumberTransformer putTransformer(Class> key, NumberTransformer transformer) {
return map.put(key, transformer);
}
@@ -100,7 +100,7 @@ public class TransformerMap implements NumberTransformer, Serializable {
* @return the removed transformer if one is present or
* null if none was present.
*/
- public Object removeTransformer(Class key) {
+ public NumberTransformer removeTransformer(Class> key) {
return map.remove(key);
}
@@ -115,7 +115,7 @@ public class TransformerMap implements NumberTransformer, Serializable {
* Returns the Set of Classes used as keys in the map.
* @return Set of Classes
*/
- public Set classes() {
+ public Set> classes() {
return map.keySet();
}
@@ -124,7 +124,7 @@ public class TransformerMap implements NumberTransformer, Serializable {
* in the map.
* @return Set of NumberTransformers
*/
- public Collection transformers() {
+ public Collection transformers() {
return map.values();
}
diff --git a/src/test/org/apache/commons/math/estimation/GaussNewtonEstimatorTest.java b/src/test/org/apache/commons/math/estimation/GaussNewtonEstimatorTest.java
index 24f0f507b..9442bcddc 100644
--- a/src/test/org/apache/commons/math/estimation/GaussNewtonEstimatorTest.java
+++ b/src/test/org/apache/commons/math/estimation/GaussNewtonEstimatorTest.java
@@ -19,15 +19,10 @@ package org.apache.commons.math.estimation;
import java.util.ArrayList;
import java.util.HashSet;
-import java.util.Iterator;
-import org.apache.commons.math.estimation.EstimatedParameter;
-import org.apache.commons.math.estimation.EstimationException;
-import org.apache.commons.math.estimation.EstimationProblem;
-import org.apache.commons.math.estimation.GaussNewtonEstimator;
-import org.apache.commons.math.estimation.WeightedMeasurement;
-
-import junit.framework.*;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
/**
*