-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #1395 add methods `nextInt(Range)` and `nextLong(Range)` to define explicitly if the min/max should be inclusive or exclusive * deprecate method `Number.randomNumber(..., boolean strict)` there is no point to call `randomNumber(strict = false)`. It's the same as just `faker.random().nextLong()`.
- Loading branch information
Showing
11 changed files
with
212 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package net.datafaker.service; | ||
|
||
import java.util.function.Function; | ||
|
||
public record Range<T extends Comparable<T>>(Bound<T> from, Bound<T> to) { | ||
public enum End {INCLUSIVE, EXCLUSIVE} | ||
public record Bound<T>(T value, End end) {} | ||
|
||
/** | ||
* A range that contains all values | ||
* 1. greater than or equal to {@code from}, and | ||
* 2. less than or equal to {@code to}. | ||
*/ | ||
public static <T extends Comparable<T>> Range<T> inclusive(T from, T to) { | ||
if (from.compareTo(to) > 0) { | ||
throw new IllegalArgumentException("Lower bound (%s) > upper bound (%s)".formatted(from, to)); | ||
} | ||
return new Range<>(new Bound<>(from, End.INCLUSIVE), new Bound<>(to, End.INCLUSIVE)); | ||
} | ||
|
||
/** | ||
* A range that contains all values | ||
* 1. greater than or equal to {@code from}, and | ||
* 2. less than {@code to}. | ||
*/ | ||
public static <T extends Comparable<T>> Range<T> inclusiveExclusive(T from, T to) { | ||
if (from.compareTo(to) >= 0) { | ||
throw new IllegalArgumentException("Lower bound (%s) >= upper bound (%s)".formatted(from, to)); | ||
} | ||
return new Range<>(new Bound<>(from, End.INCLUSIVE), new Bound<>(to, End.EXCLUSIVE)); | ||
} | ||
|
||
/** | ||
* A range that contains all values | ||
* 1. strictly greater than {@code from}, and | ||
* 2. strictly less than {@code to}. | ||
*/ | ||
public static <T extends Number & Comparable<T>> Range<T> exclusive(T from, T to) { | ||
if (to.longValue() == Long.MIN_VALUE) { | ||
throw new IllegalArgumentException("Lower bound (%s) >= upper bound (%s)".formatted(from, to)); | ||
} | ||
long upperLimit = to.longValue() - 1; | ||
if (from.longValue() >= upperLimit) { | ||
throw new IllegalArgumentException("Lower bound (%s) >= upper bound-1 (%s)".formatted(from, upperLimit)); | ||
} | ||
return new Range<>(new Bound<>(from, End.EXCLUSIVE), new Bound<>(to, End.EXCLUSIVE)); | ||
} | ||
|
||
/** | ||
* A range that contains all values | ||
* 1. strictly greater than {@code from}, and | ||
* 2. less than or equal to {@code to}. | ||
*/ | ||
public static <T extends Comparable<T>> Range<T> exclusiveInclusive(T from, T to) { | ||
if (from.compareTo(to) >= 0) { | ||
throw new IllegalArgumentException("Lower bound (%s) >= upper bound (%s)".formatted(from, to)); | ||
} | ||
return new Range<>(new Bound<>(from, End.EXCLUSIVE), new Bound<>(to, End.INCLUSIVE)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <V extends Comparable<V>> Range<V> cast(Function<T, V> caster) { | ||
return new Range<>( | ||
new Bound<>(caster.apply(from.value), from.end), | ||
new Bound<>(caster.apply(to.value), to.end) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.