From 31c6f56280136b719b9c31c88ecc753f48a64969 Mon Sep 17 00:00:00 2001 From: Otmar Ertl Date: Wed, 27 Nov 2024 15:55:52 +0100 Subject: [PATCH] PseudoRandomGenerator.reset returns self-reference, PseudoRandomGeneratorProvider.create with given seed --- .palantir/revapi.yml | 6 ++++ .../hash4j/random/PseudoRandomGenerator.java | 5 +-- .../random/PseudoRandomGeneratorProvider.java | 12 ++++++- .../dynatrace/hash4j/random/SplitMix64V1.java | 5 +-- ...ractPseudoRandomGeneratorProviderTest.java | 31 +++++++++++++++++++ ...eudoRandomGeneratorProviderForTesting.java | 6 ++-- .../random/SplitMix64_v1ProviderTest.java | 23 ++++++++++++++ .../DistinctElementHashProviderTest.java | 5 +-- 8 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 src/test/java/com/dynatrace/hash4j/random/AbstractPseudoRandomGeneratorProviderTest.java create mode 100644 src/test/java/com/dynatrace/hash4j/random/SplitMix64_v1ProviderTest.java diff --git a/.palantir/revapi.yml b/.palantir/revapi.yml index 84ecff6b..22ce077e 100644 --- a/.palantir/revapi.yml +++ b/.palantir/revapi.yml @@ -100,6 +100,12 @@ acceptedBreaks: new: "method long com.dynatrace.hash4j.hashing.HashStream64::resetAndHashToLong(T,\ \ com.dynatrace.hash4j.hashing.HashFunnel)" justification: "{added new methods}" + "0.19.0": + com.dynatrace.hash4j:hash4j: + - code: "java.method.returnTypeChanged" + old: "method void com.dynatrace.hash4j.random.PseudoRandomGenerator::reset(long)" + new: "method com.dynatrace.hash4j.random.PseudoRandomGenerator com.dynatrace.hash4j.random.PseudoRandomGenerator::reset(long)" + justification: "{return self-reference instead of void}" "0.7.1": com.dynatrace.hash4j:hash4j: - code: "java.class.noLongerImplementsInterface" diff --git a/src/main/java/com/dynatrace/hash4j/random/PseudoRandomGenerator.java b/src/main/java/com/dynatrace/hash4j/random/PseudoRandomGenerator.java index aa91040b..893047bf 100644 --- a/src/main/java/com/dynatrace/hash4j/random/PseudoRandomGenerator.java +++ b/src/main/java/com/dynatrace/hash4j/random/PseudoRandomGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Dynatrace LLC + * Copyright 2022-2024 Dynatrace LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,8 +47,9 @@ public interface PseudoRandomGenerator { * Resets the pseudo-random generator using the given 64-bit seed value. * * @param seed the seed value + * @return this */ - void reset(long seed); + PseudoRandomGenerator reset(long seed); /** * Returns a random uniformly distributed {@code double} value in the range [0, 1). diff --git a/src/main/java/com/dynatrace/hash4j/random/PseudoRandomGeneratorProvider.java b/src/main/java/com/dynatrace/hash4j/random/PseudoRandomGeneratorProvider.java index 20a21c36..907df2f4 100644 --- a/src/main/java/com/dynatrace/hash4j/random/PseudoRandomGeneratorProvider.java +++ b/src/main/java/com/dynatrace/hash4j/random/PseudoRandomGeneratorProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Dynatrace LLC + * Copyright 2022-2024 Dynatrace LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,16 @@ public interface PseudoRandomGeneratorProvider { */ PseudoRandomGenerator create(); + /** + * Creates a new {@link PseudoRandomGenerator} instance and sets a seed. + * + * @param seed the seed value + * @return the new pseudo-random generator instance + */ + default PseudoRandomGenerator create(long seed) { + return create().reset(seed); + } + /** * Returns a {@link PseudoRandomGeneratorProvider} based on the SplitMix64 algorithm. * diff --git a/src/main/java/com/dynatrace/hash4j/random/SplitMix64V1.java b/src/main/java/com/dynatrace/hash4j/random/SplitMix64V1.java index e0efa3c5..bbc0e997 100644 --- a/src/main/java/com/dynatrace/hash4j/random/SplitMix64V1.java +++ b/src/main/java/com/dynatrace/hash4j/random/SplitMix64V1.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Dynatrace LLC + * Copyright 2022-2024 Dynatrace LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,7 +44,8 @@ public long nextLong() { } @Override - public void reset(long seed) { + public SplitMix64V1 reset(long seed) { this.state = seed; + return this; } } diff --git a/src/test/java/com/dynatrace/hash4j/random/AbstractPseudoRandomGeneratorProviderTest.java b/src/test/java/com/dynatrace/hash4j/random/AbstractPseudoRandomGeneratorProviderTest.java new file mode 100644 index 00000000..5509f8bf --- /dev/null +++ b/src/test/java/com/dynatrace/hash4j/random/AbstractPseudoRandomGeneratorProviderTest.java @@ -0,0 +1,31 @@ +/* + * Copyright 2024 Dynatrace LLC + * + * Licensed 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 com.dynatrace.hash4j.random; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +public abstract class AbstractPseudoRandomGeneratorProviderTest { + + protected abstract PseudoRandomGeneratorProvider getPseudoRandomGeneratorProvider(); + + @Test + void testCreateWithSeed() { + long seed = 0x668914708c9e7635L; + Assertions.assertThat(getPseudoRandomGeneratorProvider().create(seed).nextLong()) + .isEqualTo(getPseudoRandomGeneratorProvider().create().reset(seed).nextLong()); + } +} diff --git a/src/test/java/com/dynatrace/hash4j/random/PseudoRandomGeneratorProviderForTesting.java b/src/test/java/com/dynatrace/hash4j/random/PseudoRandomGeneratorProviderForTesting.java index 6aaaac6f..72e3c71d 100644 --- a/src/test/java/com/dynatrace/hash4j/random/PseudoRandomGeneratorProviderForTesting.java +++ b/src/test/java/com/dynatrace/hash4j/random/PseudoRandomGeneratorProviderForTesting.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 Dynatrace LLC + * Copyright 2023-2024 Dynatrace LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -85,7 +85,9 @@ public int uniformInt(int exclusiveBound) { } @Override - public void reset(long seed) {} + public PseudoRandomGenerator reset(long seed) { + return this; + } @Override public double nextDouble() { diff --git a/src/test/java/com/dynatrace/hash4j/random/SplitMix64_v1ProviderTest.java b/src/test/java/com/dynatrace/hash4j/random/SplitMix64_v1ProviderTest.java new file mode 100644 index 00000000..1d78fa88 --- /dev/null +++ b/src/test/java/com/dynatrace/hash4j/random/SplitMix64_v1ProviderTest.java @@ -0,0 +1,23 @@ +/* + * Copyright 2024 Dynatrace LLC + * + * Licensed 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 com.dynatrace.hash4j.random; + +public class SplitMix64_v1ProviderTest extends AbstractPseudoRandomGeneratorProviderTest { + @Override + protected PseudoRandomGeneratorProvider getPseudoRandomGeneratorProvider() { + return PseudoRandomGeneratorProvider.splitMix64_V1(); + } +} diff --git a/src/test/java/com/dynatrace/hash4j/similarity/DistinctElementHashProviderTest.java b/src/test/java/com/dynatrace/hash4j/similarity/DistinctElementHashProviderTest.java index 655615b8..e608e114 100644 --- a/src/test/java/com/dynatrace/hash4j/similarity/DistinctElementHashProviderTest.java +++ b/src/test/java/com/dynatrace/hash4j/similarity/DistinctElementHashProviderTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Dynatrace LLC + * Copyright 2022-2024 Dynatrace LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -125,9 +125,10 @@ public int uniformInt(int exclusiveBound) { } @Override - public void reset(long seed) { + public PseudoRandomGenerator reset(long seed) { prg.reset(seed); count = 0; + return this; } @Override