T
- the type returned by this randomizerpublic class ItemRandomizerModel<T> extends java.lang.Object implements ItemRandomizer<T>
ItemRandomizer.RandomItem<T>
Constructor and Description |
---|
ItemRandomizerModel()
Instantiates a new empty RandomItemModel.
|
ItemRandomizerModel(int defaultWeight,
T... items)
Instantiates a new RandomItemModel with the given items.
|
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
|
protected void |
fireWeightsChangedEvent()
Notifies this model that the item weights have changed.
|
EventObserver<java.lang.Boolean> |
getEnabledObserver() |
int |
getItemCount() |
java.util.List<ItemRandomizer.RandomItem<T>> |
getItems() |
protected java.util.Random |
getRandom() |
T |
getRandomItem()
Fetches a random item from this model based on the item weights.
|
protected ItemRandomizer.RandomItem<T> |
getRandomItem(T item)
Returns the RandomItem associated with
item . |
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
|
public ItemRandomizerModel()
public ItemRandomizerModel(int defaultWeight, T... items)
defaultWeight
- the default weight to assign to each initial itemitems
- the items to add to this modelpublic void addItem(T item, int weight)
addItem
in interface ItemRandomizer<T>
item
- the item to addweight
- the initial weight to assign to the itempublic void incrementWeight(T item)
incrementWeight
in interface ItemRandomizer<T>
item
- the itempublic void decrementWeight(T item)
decrementWeight
in interface ItemRandomizer<T>
item
- the itempublic void setWeight(T item, int weight)
setWeight
in interface ItemRandomizer<T>
item
- the itemweight
- the valuepublic final boolean isItemEnabled(T item)
isItemEnabled
in interface ItemRandomizer<T>
item
- the itempublic final void setItemEnabled(T item, boolean value)
setItemEnabled
in interface ItemRandomizer<T>
item
- the itemvalue
- true if the item should be enabledpublic final void addItem(T item)
addItem
in interface ItemRandomizer<T>
item
- the item to addpublic final java.util.List<ItemRandomizer.RandomItem<T>> getItems()
getItems
in interface ItemRandomizer<T>
public final int getItemCount()
getItemCount
in interface ItemRandomizer<T>
public final EventObserver<java.lang.Integer> getWeightsObserver()
getWeightsObserver
in interface ItemRandomizer<T>
public final EventObserver<java.lang.Boolean> getEnabledObserver()
getEnabledObserver
in interface ItemRandomizer<T>
public final T getRandomItem()
getRandomItem
in interface ItemRandomizer<T>
public final double getWeightRatio(T item)
getWeightRatio
in interface ItemRandomizer<T>
item
- the itempublic final int getWeight(T item)
getWeight
in interface ItemRandomizer<T>
item
- the itemprotected final ItemRandomizer.RandomItem<T> getRandomItem(T item)
item
.item
- the itemjava.lang.RuntimeException
- in case the item is not foundprotected final void fireWeightsChangedEvent()
protected final java.util.Random getRandom()