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
commit 66e8b7ab9f1973d00b9ad29d503b8583737fbb5a Author: Alex Herbert <aherb...@apache.org> AuthorDate: Thu Apr 18 09:51:07 2024 +0100 Prevent partially constructed objects --- .../org/apache/commons/rng/sampling/UnitSphereSampler.java | 13 ++++++++++++- .../rng/sampling/distribution/DiscreteUniformSampler.java | 12 +++++++++++- .../distribution/RejectionInversionZipfSampler.java | 14 +++++++++++--- src/main/resources/spotbugs/spotbugs-exclude-filter.xml | 1 + 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitSphereSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitSphereSampler.java index 787bcb63..2fb7dd43 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitSphereSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/UnitSphereSampler.java @@ -223,7 +223,18 @@ public class UnitSphereSampler implements SharedStateObjectSampler<double[]> { @Deprecated public UnitSphereSampler(int dimension, UniformRandomProvider rng) { - delegate = of(rng, dimension); + this(of(rng, dimension)); + } + + /** + * Private constructor used by deprecated constructor used to prevent partially + * initialized object if the construction of the delegate throws. + * In future versions the public constructor should be removed and the class made abstract. + * + * @param delegate Delegate. + */ + private UnitSphereSampler(UnitSphereSampler delegate) { + this.delegate = delegate; } /** diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DiscreteUniformSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DiscreteUniformSampler.java index 6f35cff5..10934c0e 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DiscreteUniformSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/DiscreteUniformSampler.java @@ -333,8 +333,18 @@ public class DiscreteUniformSampler public DiscreteUniformSampler(UniformRandomProvider rng, int lower, int upper) { + this(of(rng, lower, upper)); + } + + /** + * Private constructor used by to prevent partially initialized object if the construction + * of the delegate throws. In future versions the public constructor should be removed. + * + * @param delegate Delegate. + */ + public DiscreteUniformSampler(SharedStateDiscreteSampler delegate) { super(null); - delegate = of(rng, lower, upper); + this.delegate = delegate; } /** {@inheritDoc} */ diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/RejectionInversionZipfSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/RejectionInversionZipfSampler.java index 3d74d59b..9cd69b6d 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/RejectionInversionZipfSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/RejectionInversionZipfSampler.java @@ -276,10 +276,18 @@ public class RejectionInversionZipfSampler public RejectionInversionZipfSampler(UniformRandomProvider rng, int numberOfElements, double exponent) { - super(null); + this(of(rng, numberOfElements, exponent)); + } - // Delegate all work to specialised samplers. - this.delegate = of(rng, numberOfElements, exponent); + /** + * Private constructor used by to prevent partially initialized object if the construction + * of the delegate throws. In future versions the public constructor should be removed. + * + * @param delegate Delegate. + */ + public RejectionInversionZipfSampler(SharedStateDiscreteSampler delegate) { + super(null); + this.delegate = delegate; } /** diff --git a/src/main/resources/spotbugs/spotbugs-exclude-filter.xml b/src/main/resources/spotbugs/spotbugs-exclude-filter.xml index b2ff2378..e5abf238 100644 --- a/src/main/resources/spotbugs/spotbugs-exclude-filter.xml +++ b/src/main/resources/spotbugs/spotbugs-exclude-filter.xml @@ -158,6 +158,7 @@ <Or> <Class name="org.apache.commons.rng.sampling.distribution.SmallMeanPoissonSampler"/> <Class name="org.apache.commons.rng.sampling.distribution.PoissonSamplerCache"/> + <Class name="org.apache.commons.rng.sampling.DiscreteProbabilityCollectionSampler"/> </Or> <BugPattern name="CT_CONSTRUCTOR_THROW"/> </Match>