T - the supplied typepublic class WeightedSupplier<T> extends Object implements Supplier<T>, Function<Random,T>
Supplier that randomly delegates to one of several (sub-)suppliers.
Each sub-supplier is assigned a weight, which determines the probability of it being chosen
upon calls to get().
The add(Object, int) and add(Supplier, int) methods return a reference to this,
so calls can be chained.
Usage example:
Supplier<String> mySupplier = ...;
String str = new WeightedSupplier<String>()
.add("foo", 5)
.add(mySupplier, 10)
.get();
With a one-third chance, the value "foo" will be assigned to str. Otherwise (i.e.,
with a two-thirds chance), the result of mySupplier.get() will be assigned to str. Note
that in the former case, mySupplier.get() will not even be invoked.| Constructor and Description |
|---|
WeightedSupplier() |
| Modifier and Type | Method and Description |
|---|---|
WeightedSupplier<T> |
add(Supplier<? extends T> supplier,
int weight)
Adds a sub-supplier with a given weight.
|
WeightedSupplier<T> |
add(T obj,
int weight)
Adds an object to be supplied with a given weight.
|
T |
apply(Random r) |
Supplier<T> |
forRandom(Random r) |
T |
get() |
public WeightedSupplier<T> add(T obj, int weight)
obj - the object to be suppliedweight - the weightthispublic WeightedSupplier<T> add(Supplier<? extends T> supplier, int weight)
supplier - the sub-supplierweight - the weightthisCopyright © 2015. All Rights Reserved.