Class WeightedSupplier<T>
- java.lang.Object
-
- net.automatalib.common.util.random.WeightedSupplier<T>
-
- Type Parameters:
T- the supplied type
public class WeightedSupplier<T> extends Object implements Supplier<T>, Function<Random,T>
This class implements aSupplierthat 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 toget().The
add(Object, int)andadd(Supplier, int)methods return a reference tothis, so calls can be chained.Usage example:
With a one-third chance, the valueSupplier<String> mySupplier = ...; String str = new WeightedSupplier<String>() .add("foo", 5) .add(mySupplier, 10) .get();"foo"will be assigned tostr. Otherwise (i.e., with a two-thirds chance), the result ofmySupplier.get()will be assigned tostr. Note that in the former case,mySupplier.get()will not even be invoked.
-
-
Constructor Summary
Constructors Constructor Description WeightedSupplier()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.Tapply(Random r)Supplier<T>forRandom(Random r)Tget()
-
-
-
Method Detail
-
add
public WeightedSupplier<T> add(T obj, int weight)
Adds an object to be supplied with a given weight.- Parameters:
obj- the object to be suppliedweight- the weight- Returns:
this
-
add
public WeightedSupplier<T> add(Supplier<? extends T> supplier, int weight)
Adds a sub-supplier with a given weight.- Parameters:
supplier- the sub-supplierweight- the weight- Returns:
this
-
-