You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/09/25 12:12:11 UTC

[commons-rng] branch master updated: RNG-116: RandomSource to expose supported interfaces.

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git


The following commit(s) were added to refs/heads/master by this push:
     new 1f6b611  RNG-116: RandomSource to expose supported interfaces.
1f6b611 is described below

commit 1f6b6115a5706667e5b4ac9b121958e1407f2c45
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Mon Sep 23 20:55:08 2019 +0100

    RNG-116: RandomSource to expose supported interfaces.
    
    Added methods:
    
    public boolean isJumpable();
    public boolean isLongJumpable();
---
 .../apache/commons/rng/simple/RandomSource.java    | 62 ++++++++++++++++++++++
 .../rng/simple/ProvidersCommonParametricTest.java  | 11 ++++
 .../commons/rng/simple/RandomSourceTest.java       | 14 +++++
 src/changes/changes.xml                            |  4 ++
 4 files changed, 91 insertions(+)

diff --git a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/RandomSource.java b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/RandomSource.java
index 81d6c9a..62b83b3 100644
--- a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/RandomSource.java
+++ b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/RandomSource.java
@@ -490,6 +490,68 @@ public enum RandomSource {
     }
 
     /**
+     * Checks whether the implementing class represented by this random source
+     * supports the {@link org.apache.commons.rng.JumpableUniformRandomProvider
+     * JumpableUniformRandomProvider} interface. If {@code true} the instance returned
+     * by {@link #create(RandomSource)} may be cast to the interface; otherwise a class
+     * cast exception will occur.
+     *
+     * <p>Usage example:</p>
+     * <pre><code>
+     *  RandomSource source = ...;
+     *  if (source.isJumpable()) {
+     *      JumpableUniformRandomProvider rng =
+     *          (JumpableUniformRandomProvider) RandomSource.create(source);
+     *  }
+     * </code></pre>
+     *
+     * @return {@code true} if jumpable
+     *
+     * @since 1.3
+     */
+    public boolean isJumpable() {
+        return isAssignableTo(org.apache.commons.rng.JumpableUniformRandomProvider.class);
+    }
+
+    /**
+     * Checks whether the implementing class represented by this random source
+     * supports the {@link org.apache.commons.rng.LongJumpableUniformRandomProvider
+     * LongJumpableUniformRandomProvider} interface. If {@code true} the instance returned
+     * by {@link #create(RandomSource)} may be cast to the interface; otherwise a class
+     * cast exception will occur.
+     *
+     * <p>Usage example:</p>
+     * <pre><code>
+     *  RandomSource source = ...;
+     *  if (source.isJumpable()) {
+     *      LongJumpableUniformRandomProvider rng =
+     *          (LongJumpableUniformRandomProvider) RandomSource.create(source);
+     *  }
+     * </code></pre>
+     *
+     * @return {@code true} if long jumpable
+     *
+     * @since 1.3
+     */
+    public boolean isLongJumpable() {
+        return isAssignableTo(org.apache.commons.rng.LongJumpableUniformRandomProvider.class);
+    }
+
+    /**
+     * Determines if the implementing class represented by this random source is either the same
+     * as, or is a subclass or subinterface of, the class or interface represented
+     * by the specified {@code Class} parameter. It returns true if so; otherwise it returns
+     * false.
+     *
+     * @param type the {@code Class} object to be checked
+     * @return the boolean value indicating whether the class of this random source
+     * can be assigned to objects of the specified type
+     */
+    private boolean isAssignableTo(Class<?> type) {
+        return type.isAssignableFrom(internalIdentifier.getRng());
+    }
+
+    /**
      * Creates a random number generator with a random seed.
      *
      * <p>Usage example:</p>
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java
index c09e2e5..4641937 100644
--- a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java
@@ -34,6 +34,8 @@ import org.junit.runners.Parameterized;
 import org.junit.runners.Parameterized.Parameters;
 
 import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.JumpableUniformRandomProvider;
+import org.apache.commons.rng.LongJumpableUniformRandomProvider;
 import org.apache.commons.rng.RandomProviderState;
 import org.apache.commons.rng.RestorableUniformRandomProvider;
 import org.apache.commons.rng.core.RandomProviderDefaultState;
@@ -224,6 +226,15 @@ public class ProvidersCommonParametricTest {
                             RandomSource.unrestorable(generator).toString());
     }
 
+    @Test
+    public void testSupportedInterfaces() {
+        final UniformRandomProvider rng = RandomSource.create(originalSource, null, originalArgs);
+        Assert.assertEquals("isJumpable", rng instanceof JumpableUniformRandomProvider,
+                            originalSource.isJumpable());
+        Assert.assertEquals("isLongJumpable", rng instanceof LongJumpableUniformRandomProvider,
+                            originalSource.isLongJumpable());
+    }
+
     ///// Support methods below.
 
 
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomSourceTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomSourceTest.java
index b478f98..1155f05 100644
--- a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomSourceTest.java
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomSourceTest.java
@@ -66,4 +66,18 @@ public class RandomSourceTest {
             Assert.assertNotEquals(seed[i - 1], seed[i]);
         }
     }
+
+    @Test
+    public void testIsJumpable() {
+        Assert.assertFalse("JDK is not Jumpable", RandomSource.JDK.isJumpable());
+        Assert.assertTrue("XO_SHI_RO_128_SS is Jumpable", RandomSource.XO_SHI_RO_128_SS.isJumpable());
+        Assert.assertTrue("XO_SHI_RO_256_SS is Jumpable", RandomSource.XO_SHI_RO_256_SS.isJumpable());
+    }
+
+    @Test
+    public void testIsLongJumpable() {
+        Assert.assertFalse("JDK is not LongJumpable", RandomSource.JDK.isLongJumpable());
+        Assert.assertFalse("XO_SHI_RO_128_SS is not LongJumpable", RandomSource.XO_SHI_RO_128_SS.isLongJumpable());
+        Assert.assertTrue("XO_SHI_RO_256_SS is LongJumpable", RandomSource.XO_SHI_RO_256_SS.isLongJumpable());
+    }
 }
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 132a3fe..2e28cac 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -75,6 +75,10 @@ re-run tests that fail, and pass the build if they succeed
 within the allotted number of reruns (the test will be marked
 as 'flaky' in the report).
 ">
+      <action dev="aherbert" type="update" issue="RNG-116">
+        "RandomSource": Expose interfaces supported by the implementing generator class
+        with methods isJumpable() and isLongJumpable().
+      </action>
       <action dev="aherbert" type="add" issue="RNG-111">
         New "JenkinsSmallFast32" and "JenkinsSmallFast64" generators.
       </action>