commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1454840 - in /commons/proper/math/trunk/src: changes/changes.xml main/java/org/apache/commons/math3/distribution/DiscreteDistribution.java test/java/org/apache/commons/math3/distribution/DiscreteRealDistributionTest.java
Date Sun, 10 Mar 2013 12:01:00 GMT
Author: luc
Date: Sun Mar 10 12:01:00 2013
New Revision: 1454840

URL: http://svn.apache.org/r1454840
Log:
Fixed creation of generic array.

Thanks to Piotr Wydrych.

JIRA: MATH-942

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/DiscreteDistribution.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/DiscreteRealDistributionTest.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1454840&r1=1454839&r2=1454840&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Sun Mar 10 12:01:00 2013
@@ -55,7 +55,10 @@ This is a minor release: It combines bug
   Changes to existing features were made in a backwards-compatible
   way such as to allow drop-in replacement of the v3.1[.1] JAR file.
 ">
-      <action dev="luc" type="add" issue="MATH-914" >
+      <action dev="luc" type="fix" issue="MATH-942" due-to="Piotr Wydrych" >
+        Fixed creation of generic array.
+      </action>
+        <action dev="luc" type="add" issue="MATH-914" >
         Check bounds in multi-start vector optimizers.
       </action>
       <action dev="luc" type="add" issue="MATH-941" due-to="Piotr Wydrych" >

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/DiscreteDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/DiscreteDistribution.java?rev=1454840&r1=1454839&r2=1454840&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/DiscreteDistribution.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/DiscreteDistribution.java
Sun Mar 10 12:01:00 2013
@@ -16,9 +16,9 @@
  */
 package org.apache.commons.math3.distribution;
 
-import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.List;
+
 import org.apache.commons.math3.exception.MathArithmeticException;
 import org.apache.commons.math3.exception.MathIllegalArgumentException;
 import org.apache.commons.math3.exception.NotPositiveException;
@@ -178,13 +178,13 @@ public class DiscreteDistribution<T> {
      * @throws NotStrictlyPositiveException if {@code sampleSize} is not
      * positive.
      */
-    public T[] sample(int sampleSize) throws NotStrictlyPositiveException {
+    public Object[] sample(int sampleSize) throws NotStrictlyPositiveException {
         if (sampleSize <= 0) {
             throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                     sampleSize);
         }
-        @SuppressWarnings("unchecked")
-        final T[]out = (T[]) Array.newInstance(singletons.get(0).getClass(), sampleSize);
+
+        final Object[] out = new Object[sampleSize];
 
         for (int i = 0; i < sampleSize; i++) {
             out[i] = sample();

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/DiscreteRealDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/DiscreteRealDistributionTest.java?rev=1454840&r1=1454839&r2=1454840&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/DiscreteRealDistributionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/DiscreteRealDistributionTest.java
Sun Mar 10 12:01:00 2013
@@ -16,11 +16,15 @@
  */
 package org.apache.commons.math3.distribution;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.exception.MathArithmeticException;
 import org.apache.commons.math3.exception.MathIllegalArgumentException;
 import org.apache.commons.math3.exception.NotPositiveException;
 import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.Pair;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -199,4 +203,13 @@ public class DiscreteRealDistributionTes
         Assert.assertEquals(testDistribution.getNumericalVariance(),
                 sumOfSquares / n - FastMath.pow(sum / n, 2), 1e-2);
     }
+
+    @Test
+    public void testIssue942() {
+        List<Pair<Object,Double>> list = new ArrayList<Pair<Object, Double>>();
+        list.add(new Pair<Object, Double>(new Object() {}, new Double(0)));
+        list.add(new Pair<Object, Double>(new Object() {}, new Double(1)));
+        Assert.assertEquals(1, new DiscreteDistribution<Object>(list).sample(1).length);
+    }
+
 }



Mime
View raw message