commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1428822 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/random/BitsStreamGenerator.java test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java
Date Fri, 04 Jan 2013 12:28:44 GMT
Author: erans
Date: Fri Jan  4 12:28:44 2013
New Revision: 1428822

URL: http://svn.apache.org/viewvc?rev=1428822&view=rev
Log:
MATH-927
Made "BitsStreamGenerator" class "Serializable" to allow cloning of
subclasses. Added cloning test for all "RealDistribution" classes.
Thanks to Dennis Hendriks.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/BitsStreamGenerator.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/BitsStreamGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/BitsStreamGenerator.java?rev=1428822&r1=1428821&r2=1428822&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/BitsStreamGenerator.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/random/BitsStreamGenerator.java
Fri Jan  4 12:28:44 2013
@@ -16,21 +16,26 @@
  */
 package org.apache.commons.math3.random;
 
+import java.io.Serializable;
+
 import org.apache.commons.math3.exception.NotStrictlyPositiveException;
 import org.apache.commons.math3.util.FastMath;
 
 /** Base class for random number generators that generates bits streams.
-
+ *
  * @version $Id$
  * @since 2.0
-
  */
-public abstract class BitsStreamGenerator implements RandomGenerator {
-
+public abstract class BitsStreamGenerator
+    implements RandomGenerator,
+               Serializable {
+    /** Serializable version identifier */
+    private static final long serialVersionUID = 20130104L;
     /** Next gaussian. */
     private double nextGaussian;
 
-    /** Creates a new random number generator.
+    /**
+     * Creates a new random number generator.
      */
     public BitsStreamGenerator() {
         nextGaussian = Double.NaN;

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java?rev=1428822&r1=1428821&r2=1428822&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.java
Fri Jan  4 12:28:44 2013
@@ -19,6 +19,11 @@ package org.apache.commons.math3.distrib
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 
 import org.apache.commons.math3.TestUtils;
 import org.apache.commons.math3.util.FastMath;
@@ -371,6 +376,24 @@ public abstract class RealDistributionAb
          
     }
 
+    @Test
+    public void testDistributionClone()
+        throws IOException,
+               ClassNotFoundException {
+        // Construct a distribution and initialize its internal random
+        // generator, using a fixed seed for deterministic results.
+        distribution.reseedRandomGenerator(123);
+        distribution.sample();
+
+        // Clone the distribution.
+        final RealDistribution cloned = deepClone();
+
+        // Make sure they still produce the same samples.
+        final double s1 = distribution.sample();
+        final double s2 = cloned.sample();
+        Assert.assertEquals(s1, s2, 0d);
+    }
+
     //------------------ Getters / Setters for test instance data -----------
     /**
      * @return Returns the cumulativeTestPoints.
@@ -464,4 +487,24 @@ public abstract class RealDistributionAb
         this.tolerance = tolerance;
     }
 
+    /**
+     * Serialization and deserialization loop of the {@link #distribution}.
+     */
+    private RealDistribution deepClone()
+        throws IOException,
+               ClassNotFoundException {
+        // Serialize to byte array.
+        final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+        final ObjectOutputStream oOut = new ObjectOutputStream(bOut);
+        oOut.writeObject(distribution);
+        final byte[] data = bOut.toByteArray();
+
+        // Deserialize from byte array.
+        final ByteArrayInputStream bIn = new ByteArrayInputStream(data);
+        final ObjectInputStream oIn = new ObjectInputStream(bIn);
+        final Object clone = oIn.readObject();
+        oIn.close();
+
+        return (RealDistribution) clone;
+    }
 }



Mime
View raw message