commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [1/8] commons-rng git commit: Replace inner class "RandomSource.State" with "RandomProviderDefaultState".
Date Tue, 01 Nov 2016 23:03:16 GMT
Repository: commons-rng
Updated Branches:
  refs/heads/multimodule acda9a359 -> 651c21da3


Replace inner class "RandomSource.State" with "RandomProviderDefaultState".


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

Branch: refs/heads/multimodule
Commit: b40e97935a64af1eb30022a4f282c44ba2a627a7
Parents: acda9a3
Author: Gilles <erans@apache.org>
Authored: Fri Oct 28 16:51:25 2016 +0200
Committer: Gilles <erans@apache.org>
Committed: Fri Oct 28 16:51:25 2016 +0200

----------------------------------------------------------------------
 .../org/apache/commons/rng/RandomSource.java    | 36 -------------
 .../commons/rng/internal/BaseProvider.java      |  6 +--
 .../internal/RandomProviderDefaultState.java    | 57 ++++++++++++++++++++
 .../rng/ProvidersCommonParametricTest.java      |  6 ++-
 .../commons/rng/internal/BaseProviderTest.java  |  3 +-
 .../RandomProviderDefaultStateTest.java         | 35 ++++++++++++
 6 files changed, 100 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/b40e9793/commons-rng-core/src/main/java/org/apache/commons/rng/RandomSource.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/RandomSource.java b/commons-rng-core/src/main/java/org/apache/commons/rng/RandomSource.java
index bab6224..35b4c64 100644
--- a/commons-rng-core/src/main/java/org/apache/commons/rng/RandomSource.java
+++ b/commons-rng-core/src/main/java/org/apache/commons/rng/RandomSource.java
@@ -16,8 +16,6 @@
  */
 package org.apache.commons.rng;
 
-import java.util.Arrays;
-
 import org.apache.commons.rng.internal.ProviderBuilder;
 import org.apache.commons.rng.internal.util.SeedFactory;
 
@@ -297,40 +295,6 @@ public enum RandomSource {
     private final ProviderBuilder.RandomSourceInternal internalIdentifier;
 
     /**
-     * Wraps the internal state of the {@link RestorableUniformRandomProvider}
-     * instances created by this factory.
-     * Its purpose is to store all the data needed to recover the same
-     * state in order to restart a sequence where it left off.
-     * External code should not try to modify the data contained in instances
-     * of this class.
-     */
-    public static class State implements RandomProviderState {
-        /** Internal state. */
-        private final byte[] state;
-
-        /**
-         * Initializes an instance.
-         * The contents of the {@code state} argument is unspecified, and is
-         * guaranteed to be valid only if it was generated by implementations
-         * provided by this library.
-         *
-         * @param state Mapping of all the data which an implementation of
-         * {@link UniformRandomProvider} needs in order to reset its internal
-         * state.
-         */
-        public State(byte[] state) {
-            this.state = Arrays.copyOf(state, state.length);
-        }
-
-        /**
-         * @return the internal state.
-         */
-        public byte[] getState() {
-            return Arrays.copyOf(state, state.length);
-        }
-    }
-
-    /**
      * @param id Internal identifier.
      */
     RandomSource(ProviderBuilder.RandomSourceInternal id) {

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/b40e9793/commons-rng-core/src/main/java/org/apache/commons/rng/internal/BaseProvider.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/BaseProvider.java
b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/BaseProvider.java
index 9a6eadd..70f1585 100644
--- a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/BaseProvider.java
+++ b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/BaseProvider.java
@@ -62,14 +62,14 @@ public abstract class BaseProvider
     /** {@inheritDoc} */
     @Override
     public RandomProviderState saveState() {
-        return new RandomSource.State(getStateInternal());
+        return new RandomProviderDefaultState(getStateInternal());
     }
 
     /** {@inheritDoc} */
     @Override
     public void restoreState(RandomProviderState state) {
-        if (state instanceof RandomSource.State) {
-            setStateInternal(((RandomSource.State) state).getState());
+        if (state instanceof RandomProviderDefaultState) {
+            setStateInternal(((RandomProviderDefaultState) state).getState());
         } else {
             throw new IllegalArgumentException("Foreign instance");
         }

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/b40e9793/commons-rng-core/src/main/java/org/apache/commons/rng/internal/RandomProviderDefaultState.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/internal/RandomProviderDefaultState.java
b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/RandomProviderDefaultState.java
new file mode 100644
index 0000000..2b882b3
--- /dev/null
+++ b/commons-rng-core/src/main/java/org/apache/commons/rng/internal/RandomProviderDefaultState.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.rng.internal;
+
+import java.util.Arrays;
+
+import org.apache.commons.rng.RandomProviderState;
+
+/**
+ * Wraps the internal state of the {@link RestorableUniformRandomProvider}
+ * instances created by this factory.
+ * Its purpose is to store all the data needed to recover the same
+ * state in order to restart a sequence where it left off.
+ * External code should not try to modify the data contained in instances
+ * of this class.
+ *
+ * @since 1.0
+ */
+public class RandomProviderDefaultState implements RandomProviderState {
+    /** Internal state. */
+    private final byte[] state;
+
+    /**
+     * Initializes an instance.
+     * The contents of the {@code state} argument is unspecified, and is
+     * guaranteed to be valid only if it was generated by implementations
+     * provided by this library.
+     *
+     * @param state Mapping of all the data which an implementation of
+     * {@link org.apache.commons.rng.UniformRandomProvider} needs in order
+     * to reset its internal state.
+     */
+    public RandomProviderDefaultState(byte[] state) {
+        this.state = Arrays.copyOf(state, state.length);
+    }
+
+    /**
+     * @return the internal state.
+     */
+    public byte[] getState() {
+        return Arrays.copyOf(state, state.length);
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/b40e9793/commons-rng-core/src/test/java/org/apache/commons/rng/ProvidersCommonParametricTest.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/ProvidersCommonParametricTest.java
b/commons-rng-core/src/test/java/org/apache/commons/rng/ProvidersCommonParametricTest.java
index c759600..5e0aa02 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/ProvidersCommonParametricTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/ProvidersCommonParametricTest.java
@@ -34,6 +34,8 @@ import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 import org.junit.runners.Parameterized.Parameters;
 
+import org.apache.commons.rng.internal.RandomProviderDefaultState;
+
 /**
  * Tests which all generators must pass.
  */
@@ -369,7 +371,7 @@ public class ProvidersCommonParametricTest {
         // Serialize.
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         ObjectOutputStream oos = new ObjectOutputStream(bos);
-        oos.writeObject(((RandomSource.State) stateOrig).getState());
+        oos.writeObject(((RandomProviderDefaultState) stateOrig).getState());
 
         // Store some values.
         final List<Number> listOrig = makeList(n);
@@ -382,7 +384,7 @@ public class ProvidersCommonParametricTest {
         // Retrieve from serialized stream.
         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
         ObjectInputStream ois = new ObjectInputStream(bis);
-        final RandomProviderState stateNew = new RandomSource.State((byte[]) ois.readObject());
+        final RandomProviderState stateNew = new RandomProviderDefaultState((byte[]) ois.readObject());
 
         Assert.assertTrue(stateOrig != stateNew);
 

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/b40e9793/commons-rng-core/src/test/java/org/apache/commons/rng/internal/BaseProviderTest.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/internal/BaseProviderTest.java
b/commons-rng-core/src/test/java/org/apache/commons/rng/internal/BaseProviderTest.java
index a1e05c9..0b73b0d 100644
--- a/commons-rng-core/src/test/java/org/apache/commons/rng/internal/BaseProviderTest.java
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/internal/BaseProviderTest.java
@@ -19,7 +19,6 @@ package org.apache.commons.rng.internal;
 import org.junit.Test;
 
 import org.apache.commons.rng.RestorableUniformRandomProvider;
-import org.apache.commons.rng.RandomSource;
 
 /**
  * Tests for {@link BaseProvider}.
@@ -39,7 +38,7 @@ public class BaseProviderTest {
     @Test(expected=UnsupportedOperationException.class)
     public void testMissingSetStateInternal() {
         final RestorableUniformRandomProvider rng = new DummyGenerator();
-        rng.restoreState(new RandomSource.State(new byte[1]));
+        rng.restoreState(new RandomProviderDefaultState(new byte[1]));
     }
 }
 

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/b40e9793/commons-rng-core/src/test/java/org/apache/commons/rng/internal/RandomProviderDefaultStateTest.java
----------------------------------------------------------------------
diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/internal/RandomProviderDefaultStateTest.java
b/commons-rng-core/src/test/java/org/apache/commons/rng/internal/RandomProviderDefaultStateTest.java
new file mode 100644
index 0000000..a4f8b69
--- /dev/null
+++ b/commons-rng-core/src/test/java/org/apache/commons/rng/internal/RandomProviderDefaultStateTest.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.rng.internal;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+import org.junit.Assert;
+
+/**
+ * Tests for {@link RandomProviderDefaultState}.
+ */
+public class RandomProviderDefaultStateTest {
+    @Test
+    public void testConsistency() {
+        final byte[] internalState = { 1, 0, -23, 67, -128, 54, 100, 127 };
+        final RandomProviderDefaultState state = new RandomProviderDefaultState(internalState);
+
+        Assert.assertTrue(Arrays.equals(internalState, state.getState()));
+    }
+}


Mime
View raw message