commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [1/3] [math] MATH-1365
Date Tue, 17 May 2016 16:20:08 GMT
Repository: commons-math
Updated Branches:
  refs/heads/develop 0e89d6c31 -> daf0c68d5


MATH-1365

Class requires custom serialization after moving to the new RNG API (cf. MATH-1360).


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/36ee5222
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/36ee5222
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/36ee5222

Branch: refs/heads/develop
Commit: 36ee52223387eed452c266490a25b4c62f49abc0
Parents: 0e89d6c
Author: Gilles <erans@apache.org>
Authored: Tue May 17 18:04:14 2016 +0200
Committer: Gilles <erans@apache.org>
Committed: Tue May 17 18:04:14 2016 +0200

----------------------------------------------------------------------
 .../math4/util/RandomPivotingStrategy.java      | 59 ++++++++++++++++----
 1 file changed, 49 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/36ee5222/src/main/java/org/apache/commons/math4/util/RandomPivotingStrategy.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/util/RandomPivotingStrategy.java b/src/main/java/org/apache/commons/math4/util/RandomPivotingStrategy.java
index a13b566..b795a4d 100644
--- a/src/main/java/org/apache/commons/math4/util/RandomPivotingStrategy.java
+++ b/src/main/java/org/apache/commons/math4/util/RandomPivotingStrategy.java
@@ -17,28 +17,37 @@
 package org.apache.commons.math4.util;
 
 import java.io.Serializable;
-
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.io.ObjectInputStream;
 import org.apache.commons.math4.exception.MathIllegalArgumentException;
 import org.apache.commons.math4.rng.UniformRandomProvider;
-
+import org.apache.commons.math4.rng.RandomSource;
 
 /**
  * A strategy of selecting random index between begin and end indices.
+ *
  * @since 3.4
  */
 public class RandomPivotingStrategy implements PivotingStrategyInterface, Serializable {
-
     /** Serializable UID. */
-    private static final long serialVersionUID = 20140713L;
-
+    private static final long serialVersionUID = 20160517L;
+    /** Source of randomness. */
+    private final RandomSource randomSource;
     /** Random generator to use for selecting pivot. */
-    private final UniformRandomProvider random;
+    private transient UniformRandomProvider random;
 
-    /** Simple constructor.
-     * @param random random generator to use for selecting pivot
+    /**
+     * Simple constructor.
+     *
+     * @param random Random generator to use for selecting pivot.
+     *
+     * @since 4.0
      */
-    public RandomPivotingStrategy(final UniformRandomProvider random) {
-        this.random = random;
+    public RandomPivotingStrategy(RandomSource randomSource,
+                                  long seed) {
+        this.randomSource = randomSource;
+        random = RandomSource.create(randomSource, seed);
     }
 
     /**
@@ -55,4 +64,34 @@ public class RandomPivotingStrategy implements PivotingStrategyInterface,
Serial
         return begin + random.nextInt(end - begin - 1);
     }
 
+    /**
+     * @param out Output stream.
+     * @throws IOException if an error occurs.
+     */
+    private void writeObject(ObjectOutputStream out)
+        throws IOException {
+        // Write non-transient fields.
+        out.defaultWriteObject();
+
+        // Save current state.
+        out.writeObject(RandomSource.saveState(random));
+   }
+
+    /**
+     * @param in Input stream.
+     * @throws IOException if an error occurs.
+     * @throws ClassNotFoundException if an error occurs.
+     */
+    private void readObject(ObjectInputStream in)
+        throws IOException,
+               ClassNotFoundException {
+        // Read non-transient fields.
+        in.defaultReadObject();
+
+        // Recreate the "delegate" from serialized info.
+        random = RandomSource.create(randomSource);
+        // And restore its state.
+        final RandomSource.State state = (RandomSource.State) in.readObject();
+        RandomSource.restoreState(random, state);
+    }
 }


Mime
View raw message