pslib
Class WeightedList<T>

java.lang.Object
  extended by pslib.WeightedList<T>
All Implemented Interfaces:
java.lang.Iterable<T>

public class WeightedList<T>
extends java.lang.Object
implements java.lang.Iterable<T>

WeightedList Class

The WeightedArray class allows a list of objects to be stored alongside an integer weight. Calls to the get() function will return a random object from the list, the probability of which is determined by its weight value.

This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library. If not, see http://www.gnu.org/licenses/.


Constructor Summary
WeightedList(T[] e)
          Create a new object instance with the specified list elements and equal weighting
WeightedList(T[] a, int[] w)
          Create a new object instance with the specified list elements and weighting
 
Method Summary
 T get()
          Return a random list element
 java.util.Iterator iterator()
          Return an iterator for the list
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WeightedList

public WeightedList(T[] e)
Create a new object instance with the specified list elements and equal weighting

Parameters:
e - list elements

WeightedList

public WeightedList(T[] a,
                    int[] w)
Create a new object instance with the specified list elements and weighting

Negative, zero or absent weight values will set to the default value of 1.

Parameters:
a -
w -
Method Detail

get

public final T get()
Return a random list element

The probability of each list element being returned is defined by its weight value.

Returns:
a random list element

iterator

public final java.util.Iterator iterator()
Return an iterator for the list

Specified by:
iterator in interface java.lang.Iterable<T>
Returns:
an iterator for the list