T - The type of the arbitrary object.@FunctionalInterface public interface Arbitrary<T>
| Modifier and Type | Method and Description |
|---|---|
Gen<T> |
apply(int size)
Returns a generator for objects of type T.
|
default Arbitrary<T> |
distinct()
Returns an Arbitrary based on this Arbitrary which produces unique values.
|
default Arbitrary<T> |
distinctBy(Comparator<? super T> comparator)
Returns an Arbitrary based on this Arbitrary which produces unique values based on the given comparator.
|
default <U> Arbitrary<T> |
distinctBy(Function<? super T,? extends U> keyExtractor)
Returns an Arbitrary based on this Arbitrary which produces unique values based on the given function.
|
default Arbitrary<T> |
filter(Predicate<? super T> predicate)
Returns an Arbitrary based on this Arbitrary which produces values that fulfill the given predicate.
|
default <U> Arbitrary<U> |
flatMap(Function<? super T,? extends Arbitrary<? extends U>> mapper)
Maps arbitrary objects T to arbitrary object U.
|
static Arbitrary<Integer> |
integer()
Generates arbitrary integer values.
|
default Arbitrary<T> |
intersperse(Arbitrary<T> other)
Intersperses values from this arbitrary instance with those of another.
|
static <T> Arbitrary<List<T>> |
list(Arbitrary<T> arbitraryT)
Generates arbitrary lists based on a given element generator arbitraryT.
|
static Arbitrary<LocalDateTime> |
localDateTime()
Generates arbitrary
LocalDateTimes with LocalDateTime.now() as median and
ChronoUnit.DAYS as chronological unit. |
static Arbitrary<LocalDateTime> |
localDateTime(ChronoUnit unit)
|
static Arbitrary<LocalDateTime> |
localDateTime(LocalDateTime median,
ChronoUnit unit)
Generates arbitrary
LocalDateTimes. |
default <U> Arbitrary<U> |
map(Function<? super T,? extends U> mapper)
Maps arbitrary objects T to arbitrary object U.
|
static <U> Arbitrary<U> |
of(U... values)
Generates an arbitrary value from a fixed set of values
|
static <U> Arbitrary<U> |
ofAll(Gen<U> generator)
Generates an arbitrary value from a given generator
|
default Arbitrary<T> |
peek(Consumer<? super T> action) |
static <T> Arbitrary<Stream<T>> |
stream(Arbitrary<T> arbitraryT)
Generates arbitrary streams based on a given element generator arbitraryT.
|
static Arbitrary<String> |
string(Gen<Character> gen)
Generates arbitrary strings based on a given alphabet represented by gen.
|
default <U> U |
transform(Function<? super Arbitrary<T>,? extends U> f)
Transforms this
Arbitrary. |
Gen<T> apply(int size)
Gen.map(Function) and Gen.flatMap(Function) to
combine object generators.
Example:
// represents arbitrary binary trees of a certain depth n
final class ArbitraryTree implements Arbitrary<BinaryTree<Integer>> {
@Override
public Gen<BinaryTree<Integer>> apply(int n) {
return Gen.choose(-1000, 1000).flatMap(value -> {
if (n == 0) {
return Gen.of(BinaryTree.leaf(value));
} else {
return Gen.frequency(
Tuple.of(1, Gen.of(BinaryTree.leaf(value))),
Tuple.of(4, Gen.of(BinaryTree.branch(apply(n / 2).get(), value, apply(n / 2).get())))
);
}
});
}
}
// tree generator with a size hint of 10
final Gen<BinaryTree<Integer>> treeGen = new ArbitraryTree().apply(10);
// stream sum of tree node values to console for 100 arbitrary trees
Stream.of(() -> treeGen.apply(RNG.get())).map(Tree::sum).take(100).stdout();
size - A (not necessarily positive) size parameter which may be interpreted individually and is constant for all arbitrary objects regarding one property check.default Arbitrary<T> distinct()
default Arbitrary<T> distinctBy(Comparator<? super T> comparator)
comparator - A comparatordefault <U> Arbitrary<T> distinctBy(Function<? super T,? extends U> keyExtractor)
U - key typekeyExtractor - A functiondefault Arbitrary<T> filter(Predicate<? super T> predicate)
predicate - A predicatedefault <U> Arbitrary<U> flatMap(Function<? super T,? extends Arbitrary<? extends U>> mapper)
U - New type of arbitrary objectsmapper - A function that maps arbitrary Ts to arbitrary Us given a mapper.default Arbitrary<T> intersperse(Arbitrary<T> other)
other - another T arbitrary to accept values from.default <U> Arbitrary<U> map(Function<? super T,? extends U> mapper)
U - Type of the mapped objectmapper - A function that maps an arbitrary T to an object of type U.default <U> U transform(Function<? super Arbitrary<T>,? extends U> f)
Arbitrary.U - Type of transformation resultf - A transformationUNullPointerException - if f is null@SafeVarargs static <U> Arbitrary<U> of(U... values)
U - Type of generator valuevalues - A fixed set of valuesstatic <U> Arbitrary<U> ofAll(Gen<U> generator)
U - Type of generator valuegenerator - A generator to produce arbitrary valuesstatic Arbitrary<Integer> integer()
static Arbitrary<LocalDateTime> localDateTime()
LocalDateTimes with LocalDateTime.now() as median and
ChronoUnit.DAYS as chronological unit.localDateTime(LocalDateTime, ChronoUnit)static Arbitrary<LocalDateTime> localDateTime(ChronoUnit unit)
unit - Chronological unit of sizelocalDateTime(LocalDateTime, ChronoUnit)static Arbitrary<LocalDateTime> localDateTime(LocalDateTime median, ChronoUnit unit)
LocalDateTimes. All generated values are drawn from a range with median
as center and median +/- size as included boundaries. unit defines the chronological unit
of size.
Example:
Arbitrary.localDateTime(LocalDateTime.now(), ChronoUnit.YEARS);
median - Center of the LocalDateTime rangeunit - Chronological unit of sizestatic Arbitrary<String> string(Gen<Character> gen)
Example:
Arbitrary.string(
Gen.frequency(
Tuple.of(1, Gen.choose('A', 'Z')),
Tuple.of(1, Gen.choose('a', 'z')),
Tuple.of(1, Gen.choose('0', '9'))));
gen - A character generatorstatic <T> Arbitrary<List<T>> list(Arbitrary<T> arbitraryT)
Example:
Arbitrary.list(Arbitrary.integer());
T - Component type of the ListarbitraryT - Arbitrary elements of type Tstatic <T> Arbitrary<Stream<T>> stream(Arbitrary<T> arbitraryT)
Example:
Arbitrary.stream(Arbitrary.integer());
T - Component type of the StreamarbitraryT - Arbitrary elements of type TCopyright © 2021. All Rights Reserved.