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.
Object one = new Object(); Object two = new Object(); Object three = new Object(); ItemRandomizer model = ItemRandomizer.itemRandomizer(Arrays.asList(one, two, three)); model.setWeight(one, 10); model.setWeight(two, 60); model.setWeight(three, 30); //10% chance of getting 'one', 60% chance of getting 'two' and 30% chance of getting 'three'. Object random = model.randomItem();For instances use the following factory functions:
itemRandomizer(Collection)
,
boundedItemRandomizer(Collection)
, boundedItemRandomizer(Collection, int)
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
Wraps an item for usage in the ItemRandomizer. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> ItemRandomizer<T>
boundedItemRandomizer
(Collection<T> items) Instantiates a newItemRandomizer
with the added constraint that the total item weights can not exceed a defined maximum.static <T> ItemRandomizer<T>
boundedItemRandomizer
(Collection<T> items, int maximumTotalWeights) Instantiates a newItemRandomizer
with the added constraint that the total item weights can not exceed a defined maximum.void
decrementWeight
(T item) Decrements the weight of the given item by onevoid
incrementWeight
(T item) Increments the weight of the given item by oneboolean
isItemEnabled
(T item) int
static <T> ItemRandomizer<T>
itemRandomizer
(Collection<ItemRandomizer.RandomItem<T>> items) Instantiates a newItemRandomizer
.items()
Fetches a random item from this model based on the item weights.void
setItemEnabled
(T item, boolean enabled) void
Sets the weight of the given itemint
Returns the weight of the given item.double
weightRatio
(T item) Returns this items share in the total weights as a floating point number between 0 and 1
-
Method Details
-
itemCount
int itemCount()- Returns:
- the number of items in this model.
-
items
Collection<ItemRandomizer.RandomItem<T>> items()- Returns:
- the items in this model.
-
weight
Returns the weight of the given item.- Parameters:
item
- the item- Returns:
- the item weight
-
setWeight
Sets the weight of the given item- Parameters:
item
- the itemweight
- the value
-
randomItem
T randomItem()Fetches a random item from this model based on the item weights.- Returns:
- a randomly chosen item.
-
weightRatio
Returns this items share in the total weights as a floating point number between 0 and 1- Parameters:
item
- the item- Returns:
- the ratio of the total weights held by the given item
-
incrementWeight
Increments the weight of the given item by one- Parameters:
item
- the item
-
decrementWeight
Decrements the weight of the given item by one- Parameters:
item
- the item- Throws:
IllegalStateException
- in case the weight is 0
-
isItemEnabled
- Parameters:
item
- the item- Returns:
- true if the item is enabled
-
setItemEnabled
- Parameters:
item
- the itemenabled
- true if the item should be enabled
-
itemRandomizer
Instantiates a newItemRandomizer
.- Type Parameters:
T
- the item type- Parameters:
items
- the items to randomize- Returns:
- a new
ItemRandomizer
-
boundedItemRandomizer
Instantiates a newItemRandomizer
with the added constraint that the total item weights can not exceed a defined maximum. When the weight of one item is incremented the weight of another is decremented in a round-robin kind of fashion and when an item weight is decremented the weight of another is incremented.
Instantiates a newItemRandomizer
with the maximum total weights as 100.- Type Parameters:
T
- the item type- Parameters:
items
- the items- Returns:
- a new
ItemRandomizer
-
boundedItemRandomizer
Instantiates a newItemRandomizer
with the added constraint that the total item weights can not exceed a defined maximum. When the weight of one item is incremented the weight of another is decremented in a round-robin kind of fashion and when an item weight is decremented the weight of another is incremented.- Type Parameters:
T
- the item type- Parameters:
items
- the itemsmaximumTotalWeights
- the maximum total weights- Returns:
- a new
ItemRandomizer
-