org.jminor.common.model
Class ItemRandomizerModel<T>

java.lang.Object
  extended by org.jminor.common.model.ItemRandomizerModel<T>
All Implemented Interfaces:
ItemRandomizer<T>
Direct Known Subclasses:
BoundedItemRandomizerModel

public class ItemRandomizerModel<T>
extends Object
implements ItemRandomizer<T>

A default ItemRandomizer implementation.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jminor.common.model.ItemRandomizer
ItemRandomizer.RandomItem<T>
 
Constructor Summary
ItemRandomizerModel()
          Instantiates a new empty RandomItemModel.
ItemRandomizerModel(int defaultWeight, T... items)
          Instantiates a new RandomItemModel with the given items.
 
Method Summary
 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 getEnabledObserver()
          
 int getItemCount()
          
 List<ItemRandomizer.RandomItem<T>> getItems()
          
protected  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 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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ItemRandomizerModel

public ItemRandomizerModel()
Instantiates a new empty RandomItemModel.


ItemRandomizerModel

public ItemRandomizerModel(int defaultWeight,
                           T... items)
Instantiates a new RandomItemModel with the given items.

Parameters:
defaultWeight - the default weight to assign to each intial item
items - the items to add to this model
Method Detail

addItem

public void addItem(T item,
                    int weight)
Adds the given item to this model with the given weight value.

Specified by:
addItem in interface ItemRandomizer<T>
Parameters:
item - the item to add
weight - the initial weight to assign to the item

incrementWeight

public void incrementWeight(T item)
Increments the weight of the given item by one

Specified by:
incrementWeight in interface ItemRandomizer<T>
Parameters:
item - the item

decrementWeight

public void decrementWeight(T item)
Decrements the weight of the given item by one

Specified by:
decrementWeight in interface ItemRandomizer<T>
Parameters:
item - the item

setWeight

public void setWeight(T item,
                      int weight)
Sets the weight of the given item

Specified by:
setWeight in interface ItemRandomizer<T>
Parameters:
item - the item
weight - the value

isItemEnabled

public boolean isItemEnabled(T item)

Specified by:
isItemEnabled in interface ItemRandomizer<T>
Parameters:
item - the item
Returns:
true if the item is enabled

setItemEnabled

public void setItemEnabled(T item,
                           boolean value)

Specified by:
setItemEnabled in interface ItemRandomizer<T>
Parameters:
item - the item
value - true if the item should be enabled

addItem

public final void addItem(T item)
Adds the given item to this model with default weight of 0.

Specified by:
addItem in interface ItemRandomizer<T>
Parameters:
item - the item to add

getItems

public final List<ItemRandomizer.RandomItem<T>> getItems()

Specified by:
getItems in interface ItemRandomizer<T>
Returns:
the items in this model.

getItemCount

public final int getItemCount()

Specified by:
getItemCount in interface ItemRandomizer<T>
Returns:
the number of items in this model.

getWeightsObserver

public final EventObserver getWeightsObserver()

Specified by:
getWeightsObserver in interface ItemRandomizer<T>
Returns:
an Event which fires each time a weight has been changed.

getEnabledObserver

public final EventObserver getEnabledObserver()

Specified by:
getEnabledObserver in interface ItemRandomizer<T>
Returns:
an Event which fires each time the enabled status of an item has been changed.

getRandomItem

public final T getRandomItem()
Fetches a random item from this model based on the item weights.

Specified by:
getRandomItem in interface ItemRandomizer<T>
Returns:
a randomly chosen item.

getWeightRatio

public final double getWeightRatio(T item)
Returns this items share in the total weights as a floating point number between 0 and 1

Specified by:
getWeightRatio in interface ItemRandomizer<T>
Parameters:
item - the item
Returns:
the ratio of the total weights held by the given item

getWeight

public final int getWeight(T item)
Returns the weight of the given item.

Specified by:
getWeight in interface ItemRandomizer<T>
Parameters:
item - the item
Returns:
the item weight

getRandomItem

protected final ItemRandomizer.RandomItem<T> getRandomItem(T item)
Returns the RandomItem associated with item.

Parameters:
item - the item
Returns:
the RandomItem
Throws:
RuntimeException - in case the item is not found

fireWeightsChangedEvent

protected final void fireWeightsChangedEvent()
Notifies this model that the item weights have changed.


getRandom

protected final Random getRandom()
Returns:
a Random instance.