[ 
https://issues.apache.org/jira/browse/RNG-142?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17358196#comment-17358196
 ] 

Gilles Sadowski commented on RNG-142:
-------------------------------------

{quote}Running clirr:check with the same change also reports binary 
incompatibility.
{quote}
The return type is part of a function's signature (in the bytecode too).
{quote}Add a note that the call to withUniformRandomProvider returns a 
SharedStateContinuousSampler but it can be cast to the original Gaussian 
sampler type.
{quote}
I think that we should not advertise the use of casting.

I'd consider that {{SharedStateContinuousSampler}} is an implementation detail 
which the caller should not have to be aware of; hence the expectation that the 
fluent API returns an object of the same type.
{quote}
{code:java}
SharedStateContinuousSampler g = GaussianSampler.of(algorithm, 0.43, 2.1)
    .withUniformRandomProvider(RandomSource.create(RandomSource.JSF_64));
{code}
{quote}
It's fine. Thanks.

Could we perhaps go one step further in encapsulating "implementation details", 
and use an "enum" when there are several alternatives for some functionality 
(like sampling from a Gaussian distribution)? E.g. (untested):
{code:java}
public class GaussianSampler {
    private final ContinuousSampler delegate;

    // ...

    public enum Variant {
        ZIGGURAT,
        MARSAGLIA,
        BOX_MULLER
    }

    private GaussianSampler(final NormalizedGaussianSampler n, final double 
mean, final double stdev) {
         if (mean == 0d && stdev == 1d) { // Will displease "SonarCloud" ;-)
             delegate = n;
         } else {
             delegate = new ContinuousSampler() {
                 public double sample() {
                     return mean + n.sample() * stdev;
                 }
             }
         }
    }

    /** Request a specific implementation */
    public static GaussianSampler of(Variant v, double mean, double stdev) {
        switch (v) {
        case ZIGGURAT:
             return new GaussianSampler(new ZigguratNormalizedSampler(), mean, 
stdev);
             // ...
        }
    }
    /** Default implementation */
    public static GaussianSampler of(double mean, double stdev) {
        return of(Variant.ZIGGURAT, mean, stdev);
    }

    public double sample() {
        return delegate.sample();
    }
}
{code}

{{NormalizedGaussianSampler}} thus becomes a "low-level" type (for the library 
to ensure its internal consistency) and the public API is just 
{{GaussianSampler}}.

> Return type of method "withUniformRandomProvider"
> -------------------------------------------------
>
>                 Key: RNG-142
>                 URL: https://issues.apache.org/jira/browse/RNG-142
>             Project: Commons RNG
>          Issue Type: Improvement
>          Components: sampling
>            Reporter: Gilles Sadowski
>            Priority: Major
>
> Expected usage:
> {code:java}
> NormalizedGaussianSampler n01 = 
> ZigguratNormalizedGaussianSampler.of(RandomSource.create(RandomSource.KISS));
> GaussianSampler g = 
> GaussianSampler.of(n01.withUniformRandomProvider(RandomSource.create(RandomSource.JSF_64)),
>  0.43, 2.1);
> {code}
> Code doesn't compile: Method {{withUniformRandomProvider}} returns a 
> {{SharedStateContinuousSampler}} whereas a {{NormalizedGaussianSampler}} is 
> required.
> Am I missing something?



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to