T
- the type of item this random item model returnspublic interface ItemRandomizer<T>
Object one = new Object(); Object two = new Object(); Object three = new Object(); ItemRandomizer model = ... 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.getRandomItem();
Modifier and Type | Interface and Description |
---|---|
static interface |
ItemRandomizer.RandomItem<T>
Wraps an item for usage in the ItemRandomizer
|
Modifier and Type | Method and Description |
---|---|
void |
addItem(T item)
Adds the given item to this model with default weight of 0.
|
void |
addItem(T item,
int weight)
Adds the given item to this model with the given weight value.
|
void |
decrementWeight(T item)
Decrements the weight of the given item by one
|
EventObserver<java.lang.Boolean> |
getEnabledObserver() |
int |
getItemCount() |
java.util.Collection<ItemRandomizer.RandomItem<T>> |
getItems() |
T |
getRandomItem()
Fetches a random item from this model based on the item weights.
|
int |
getWeight(T item)
Returns the weight of the given item.
|
double |
getWeightRatio(T item)
Returns this items share in the total weights as a floating point number between 0 and 1
|
EventObserver<java.lang.Integer> |
getWeightsObserver() |
void |
incrementWeight(T item)
Increments the weight of the given item by one
|
boolean |
isItemEnabled(T item) |
void |
setItemEnabled(T item,
boolean value) |
void |
setWeight(T item,
int weight)
Sets the weight of the given item
|
int getItemCount()
java.util.Collection<ItemRandomizer.RandomItem<T>> getItems()
int getWeight(T item)
item
- the itemEventObserver<java.lang.Integer> getWeightsObserver()
EventObserver<java.lang.Boolean> getEnabledObserver()
void setWeight(T item, int weight)
item
- the itemweight
- the valuevoid addItem(T item)
item
- the item to addvoid addItem(T item, int weight)
item
- the item to addweight
- the initial weight to assign to the itemT getRandomItem()
double getWeightRatio(T item)
item
- the itemvoid incrementWeight(T item)
item
- the itemvoid decrementWeight(T item)
item
- the itemjava.lang.IllegalStateException
- in case the weight is 0boolean isItemEnabled(T item)
item
- the itemvoid setItemEnabled(T item, boolean value)
item
- the itemvalue
- true if the item should be enabled