org.jminor.common.model
Interface ItemRandomizer<T>

Type Parameters:
T - the type of item this random item model returns
All Known Implementing Classes:
BoundedItemRandomizerModel, ItemRandomizerModel

public interface ItemRandomizer<T>

ItemRandomizer provides a way for randomly choosing an item based on a weight value.

 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();
 


Nested Class Summary
static interface ItemRandomizer.RandomItem<T>
           
 
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
 EventObserver getEnabledObserver()
           
 int getItemCount()
           
 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 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
 

Method Detail

getItemCount

int getItemCount()
Returns:
the number of items in this model.

getItems

Collection<ItemRandomizer.RandomItem<T>> getItems()
Returns:
the items in this model.

getWeight

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

Parameters:
item - the item
Returns:
the item weight

getWeightsObserver

EventObserver getWeightsObserver()
Returns:
an Event which fires each time a weight has been changed.

getEnabledObserver

EventObserver getEnabledObserver()
Returns:
an Event which fires each time the enabled status of an item has been changed.

setWeight

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

Parameters:
item - the item
weight - the value

addItem

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

Parameters:
item - the item to add

addItem

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

Parameters:
item - the item to add
weight - the initial weight to assign to the item

getRandomItem

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

Returns:
a randomly chosen item.

getWeightRatio

double getWeightRatio(T item)
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

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

Parameters:
item - the item

decrementWeight

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

Parameters:
item - the item
Throws:
IllegalStateException - in case the weight is 0

isItemEnabled

boolean isItemEnabled(T item)
Parameters:
item - the item
Returns:
true if the item is enabled

setItemEnabled

void setItemEnabled(T item,
                    boolean value)
Parameters:
item - the item
value - true if the item should be enabled