From 47a9d0c63fa6f654677e393701bd14ff468eaca6 Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Thu, 31 Oct 2024 13:37:22 +0200 Subject: [PATCH] deprecate method `Number.randomNumber(..., boolean strict)` there is no point to call `randomNumber(strict = false)`. It's the same as just `faker.random().nextLong()`. --- .../net/datafaker/providers/base/Barcode.java | 2 +- .../net/datafaker/providers/base/Number.java | 24 +++++++++++-------- .../datafaker/providers/base/NumberTest.java | 13 +++++----- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/datafaker/providers/base/Barcode.java b/src/main/java/net/datafaker/providers/base/Barcode.java index 3fe488920..e75b040f7 100644 --- a/src/main/java/net/datafaker/providers/base/Barcode.java +++ b/src/main/java/net/datafaker/providers/base/Barcode.java @@ -45,7 +45,7 @@ private static int roundToHighestMultiplyOfTen(int number) { private long ean(int length) { long firstPart = switch (length) { - case 8, 12, 13, 14 -> this.faker.number().randomNumber(length - 1, true); + case 8, 12, 13, 14 -> this.faker.number().randomNumber(length - 1); default -> 0; }; int odd = 0; diff --git a/src/main/java/net/datafaker/providers/base/Number.java b/src/main/java/net/datafaker/providers/base/Number.java index df53aad7e..8dfeac9e6 100644 --- a/src/main/java/net/datafaker/providers/base/Number.java +++ b/src/main/java/net/datafaker/providers/base/Number.java @@ -80,19 +80,24 @@ public long numberBetween(long min, long max) { /** * @param numberOfDigits the number of digits the generated value should have - * @param strict whether or not the generated value should have exactly numberOfDigits + * @param strict NOT USED + * @deprecated use {@link #randomNumber(int)} instead */ + @Deprecated public long randomNumber(int numberOfDigits, boolean strict) { + return randomNumber(numberOfDigits); + } + + /** + * @param numberOfDigits the number of digits the generated value should have + */ + public long randomNumber(int numberOfDigits) { if (numberOfDigits <= 0) { - return faker.random().nextInt(1); + throw new IllegalArgumentException("Number of digits must be positive"); } long min = pow(10, numberOfDigits - 1); - if (strict) { - long max = min * 10; - return faker.random().nextLong(max - min) + min; - } - - return faker.random().nextLong(min * 10); + long max = min * 10; + return faker.random().nextLong(min, max); } private long pow(long value, int d) { @@ -110,8 +115,7 @@ private long pow(long value, int d) { * Returns a random number */ public long randomNumber() { - int numberOfDigits = faker.random().nextInt(1, 10); - return randomNumber(numberOfDigits, false); + return faker.random().nextLong(); } public double randomDouble(int maxNumberOfDecimals, int min, int max) { diff --git a/src/test/java/net/datafaker/providers/base/NumberTest.java b/src/test/java/net/datafaker/providers/base/NumberTest.java index 163046272..080a0c0da 100644 --- a/src/test/java/net/datafaker/providers/base/NumberTest.java +++ b/src/test/java/net/datafaker/providers/base/NumberTest.java @@ -15,6 +15,7 @@ import java.util.function.Supplier; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; class NumberTest extends BaseFakerTest { @@ -62,19 +63,17 @@ void testRandomNumber() { void testRandomNumberWithSingleDigitStrict() { final Number number = faker.number(); for (int i = 0; i < 100; ++i) { - long value = number.randomNumber(1, true); + long value = number.randomNumber(1); assertThat(value).isLessThan(10L) .isGreaterThanOrEqualTo(0L); } } @Test - void testRandomNumberWithZeroDigitsStrict() { + void randomNumberWithZeroDigits() { final Number number = faker.number(); - for (int i = 0; i < 100; ++i) { - long value = number.randomNumber(0, true); - assertThat(value).isZero(); - } + assertThatThrownBy(() -> number.randomNumber(0)) + .isInstanceOf(IllegalArgumentException.class); } @Test @@ -82,7 +81,7 @@ void testRandomNumberWithGivenDigitsStrict() { final Number number = faker.number(); for (int i = 1; i < 9; ++i) { for (int x = 0; x < 100; ++x) { - long value = number.randomNumber(i, true); + long value = number.randomNumber(i); String stringValue = String.valueOf(value); assertThat(stringValue).hasSize(i); }