Skip to content

Commit

Permalink
deprecate method Number.randomNumber(..., boolean strict)
Browse files Browse the repository at this point in the history
there is no point to call `randomNumber(strict = false)`. It's the same as just `faker.random().nextLong()`.
  • Loading branch information
asolntsev committed Oct 31, 2024
1 parent 582e0c1 commit 47a9d0c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/datafaker/providers/base/Barcode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/net/datafaker/providers/base/Number.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>numberOfDigits</code>
* @param strict NOT USED
* @deprecated use {@link #randomNumber(int)} instead
*/
@Deprecated
public long randomNumber(int numberOfDigits, boolean strict) {
return randomNumber(numberOfDigits);

Check warning on line 88 in src/main/java/net/datafaker/providers/base/Number.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/net/datafaker/providers/base/Number.java#L88

Added line #L88 was not covered by tests
}

/**
* @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) {
Expand All @@ -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) {
Expand Down
13 changes: 6 additions & 7 deletions src/test/java/net/datafaker/providers/base/NumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseFaker> {

Expand Down Expand Up @@ -62,27 +63,25 @@ 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
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);
}
Expand Down

0 comments on commit 47a9d0c

Please sign in to comment.