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 f1ddb11  Simplify shared state implementations using 
NormalizedGaussianSampler.
f1ddb11 is described below

commit f1ddb11e5d5dee7e2657102d90ae681ba2aea958
Author: aherbert <aherb...@apache.org>
AuthorDate: Wed Jul 31 16:08:08 2019 +0100

    Simplify shared state implementations using NormalizedGaussianSampler.
    
    Throw an UnsupportedOperationException if the NormalizedGaussianSampler
    cannot shared state.
---
 .../rng/sampling/distribution/GaussianSampler.java | 12 ++++------
 .../rng/sampling/distribution/InternalUtils.java   | 28 ++++++++++++++++++++++
 .../sampling/distribution/LogNormalSampler.java    | 12 ++++------
 .../sampling/distribution/GaussianSamplerTest.java |  2 +-
 .../distribution/LogNormalSamplerTest.java         |  2 +-
 5 files changed, 38 insertions(+), 18 deletions(-)

diff --git 
a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GaussianSampler.java
 
b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GaussianSampler.java
index e8cb095..8362387 100644
--- 
a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GaussianSampler.java
+++ 
b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/GaussianSampler.java
@@ -57,13 +57,9 @@ public class GaussianSampler implements ContinuousSampler, 
SharedStateSampler<Ga
      */
     private GaussianSampler(UniformRandomProvider rng,
                             GaussianSampler source) {
-        if (!(source.normalized instanceof SharedStateSampler<?>)) {
-            throw new UnsupportedOperationException("The underlying sampler is 
not a SharedStateSampler");
-        }
         this.mean = source.mean;
         this.standardDeviation = source.standardDeviation;
-        this.normalized = (NormalizedGaussianSampler)
-            
((SharedStateSampler<?>)source.normalized).withUniformRandomProvider(rng);
+        this.normalized = 
InternalUtils.newNormalizedGaussianSampler(source.normalized, rng);
     }
 
     /** {@inheritDoc} */
@@ -84,9 +80,9 @@ public class GaussianSampler implements ContinuousSampler, 
SharedStateSampler<Ga
      * <p>Note: This function is available if the underlying {@link 
NormalizedGaussianSampler}
      * is a {@link SharedStateSampler}. Otherwise a run-time exception is 
thrown.</p>
      *
-     * @throws UnsupportedOperationException if the underlying sampler is not 
a {@link SharedStateSampler}.
-     * @throws ClassCastException if the underlying {@link SharedStateSampler} 
does not return a
-     * {@link NormalizedGaussianSampler}.
+     * @throws UnsupportedOperationException if the underlying sampler is not a
+     * {@link SharedStateSampler} or does not return a {@link 
NormalizedGaussianSampler} when
+     * sharing state.
      */
     @Override
     public GaussianSampler withUniformRandomProvider(UniformRandomProvider 
rng) {
diff --git 
a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InternalUtils.java
 
b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InternalUtils.java
index b6ec3df..3807001 100644
--- 
a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InternalUtils.java
+++ 
b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/InternalUtils.java
@@ -17,6 +17,9 @@
 
 package org.apache.commons.rng.sampling.distribution;
 
+import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.sampling.SharedStateSampler;
+
 /**
  * Functions used by some of the samplers.
  * This class is not part of the public API, as it would be
@@ -91,6 +94,31 @@ final class InternalUtils { // Class is package-private on 
purpose; do not make
     }
 
     /**
+     * Create a new instance of the given sampler using
+     * {@link 
SharedStateSampler#withUniformRandomProvider(UniformRandomProvider)}.
+     *
+     * @param sampler Source sampler.
+     * @param rng Generator of uniformly distributed random numbers.
+     * @return the new sampler
+     * @throws UnsupportedOperationException if the underlying sampler is not a
+     * {@link SharedStateSampler} or does not return a {@link 
NormalizedGaussianSampler} when
+     * sharing state.
+     */
+    static NormalizedGaussianSampler newNormalizedGaussianSampler(
+            NormalizedGaussianSampler sampler,
+            UniformRandomProvider rng) {
+        if (!(sampler instanceof SharedStateSampler<?>)) {
+            throw new UnsupportedOperationException("The underlying sampler 
cannot share state");
+        }
+        final Object newSampler = ((SharedStateSampler<?>) 
sampler).withUniformRandomProvider(rng);
+        if (!(newSampler instanceof NormalizedGaussianSampler)) {
+            throw new UnsupportedOperationException(
+                "The underlying sampler did not create a normalized Gaussian 
sampler");
+        }
+        return (NormalizedGaussianSampler) newSampler;
+    }
+
+    /**
      * Class for computing the natural logarithm of the factorial of {@code n}.
      * It allows to allocate a cache of precomputed values.
      * In case of cache miss, computation is performed by a call to
diff --git 
a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/LogNormalSampler.java
 
b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/LogNormalSampler.java
index 3879cbc..55dab9e 100644
--- 
a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/LogNormalSampler.java
+++ 
b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/LogNormalSampler.java
@@ -58,13 +58,9 @@ public class LogNormalSampler implements ContinuousSampler, 
SharedStateSampler<L
      */
     private LogNormalSampler(UniformRandomProvider rng,
                              LogNormalSampler source) {
-        if (!(source.gaussian instanceof SharedStateSampler<?>)) {
-            throw new UnsupportedOperationException("The underlying sampler is 
not a SharedStateSampler");
-        }
         this.scale = source.scale;
         this.shape = source.shape;
-        this.gaussian = (NormalizedGaussianSampler)
-            
((SharedStateSampler<?>)source.gaussian).withUniformRandomProvider(rng);
+        this.gaussian = 
InternalUtils.newNormalizedGaussianSampler(source.gaussian, rng);
     }
 
     /** {@inheritDoc} */
@@ -85,9 +81,9 @@ public class LogNormalSampler implements ContinuousSampler, 
SharedStateSampler<L
      * <p>Note: This function is available if the underlying {@link 
NormalizedGaussianSampler}
      * is a {@link SharedStateSampler}. Otherwise a run-time exception is 
thrown.</p>
      *
-     * @throws UnsupportedOperationException if the underlying sampler is not 
a {@link SharedStateSampler}.
-     * @throws ClassCastException if the underlying {@link SharedStateSampler} 
does not return a
-     * {@link NormalizedGaussianSampler}.
+     * @throws UnsupportedOperationException if the underlying sampler is not a
+     * {@link SharedStateSampler} or does not return a {@link 
NormalizedGaussianSampler} when
+     * sharing state.
      */
     @Override
     public LogNormalSampler withUniformRandomProvider(UniformRandomProvider 
rng) {
diff --git 
a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GaussianSamplerTest.java
 
b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GaussianSamplerTest.java
index 63bbf88..f4afb63 100644
--- 
a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GaussianSamplerTest.java
+++ 
b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/GaussianSamplerTest.java
@@ -82,7 +82,7 @@ public class GaussianSamplerTest {
      * Test the SharedStateSampler implementation throws if the underlying 
sampler is
      * a SharedStateSampler that returns an incorrect type.
      */
-    @Test(expected = ClassCastException.class)
+    @Test(expected = UnsupportedOperationException.class)
     public void 
testSharedStateSamplerThrowsIfUnderlyingSamplerReturnsWrongSharedState() {
         final UniformRandomProvider rng2 = 
RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
         final NormalizedGaussianSampler gauss = new 
BadSharedStateNormalizedGaussianSampler();
diff --git 
a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/LogNormalSamplerTest.java
 
b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/LogNormalSamplerTest.java
index e708fc4..a8ec693 100644
--- 
a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/LogNormalSamplerTest.java
+++ 
b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/LogNormalSamplerTest.java
@@ -97,7 +97,7 @@ public class LogNormalSamplerTest {
      * Test the SharedStateSampler implementation throws if the underlying 
sampler is
      * a SharedStateSampler that returns an incorrect type.
      */
-    @Test(expected = ClassCastException.class)
+    @Test(expected = UnsupportedOperationException.class)
     public void 
testSharedStateSamplerThrowsIfUnderlyingSamplerReturnsWrongSharedState() {
         final UniformRandomProvider rng2 = 
RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
         final NormalizedGaussianSampler gauss = new 
BadSharedStateNormalizedGaussianSampler();

Reply via email to