Module is.codion.tools.loadtest
Interface ItemRandomizer<T>
- Type Parameters:
T- the type of item this random item model returns
public interface ItemRandomizer<T>
ItemRandomizer provides a way to randomly choose an item based on a weight value.
For instances use the following factory functions:
String one = "one";
String two = "two";
String three = "three";
ItemRandomizer<String> randomizer = ItemRandomizer.randomizer(List.of(
randomItem(one, 10),
randomItem(two, 60),
randomItem(three, 30)));
//10% chance of getting 'one', 60% chance of getting 'two' and 30% chance of getting 'three'.
String random = randomizer.get().orElse(null);
// The weights can be changed
randomizer.weight(one).set(20);
randomizer(Collection)-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceWraps an item for usage in the ItemRandomizer. -
Method Summary
Modifier and TypeMethodDescriptionget()Fetches a random item from this model based on the item weights.items()static <T> ItemRandomizer<T> randomizer(Collection<ItemRandomizer.RandomItem<T>> items) Instantiates a newItemRandomizer.
-
Method Details
-
items
Collection<ItemRandomizer.RandomItem<T>> items()- Returns:
- the items in this model.
-
weight
- Returns:
- the
Valuecontrolling the weight
-
enabled
- Parameters:
item- the item- Returns:
- the item enabled state
-
get
Fetches a random item from this model based on the item weights.- Returns:
- a randomly chosen item or an empty
Optionalin case no item is enabled or the total weights are zero
-
randomizer
Instantiates a newItemRandomizer.- Type Parameters:
T- the item type- Parameters:
items- the items to randomize- Returns:
- a new
ItemRandomizer
-