Package at.noahb.utils.generic
Class WeightedElements<T>
java.lang.Object
at.noahb.utils.generic.WeightedElements<T>
- Author:
- Sam (GitHub: Sam's GitHub)
This class will hold a list of weighted elements, and can be used to randomly select an element from the list based on their respective weights.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(WeightedElement<T> e) Add an element to the list.Creates and adds a new WeightedElement to the internal list.clear()Clear the internal list.doublegetChance(int index) doublegetElement(int index) doublegetObject(int index) static <T> TgetRandom(List<WeightedElement<T>> weightedElements) A static method to get a random element from a list of weighted elements.doublestatic WeightedElementCreate a new WeightedElements object.Creates a new WeightedElement and remove any element from the internal list that matches the newly created object.set(WeightedElement<T>... e)
-
Constructor Details
-
WeightedElements
public WeightedElements()
-
-
Method Details
-
getRandom
A static method to get a random element from a list of weighted elements.- Parameters:
weightedElements- A list of weighted elements.- Returns:
- A random element from the list while factoring in the elements' weights.
-
of
Create a new WeightedElements object.- Parameters:
o- The object to hold.weight- The weight of the object.- Returns:
- A new WeightedElements object.
-
getElements
- Returns:
- The list of weighted elements.
-
getElement
- Parameters:
index- Where to get the element from in the list- Returns:
- The element at the given index.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getObject
public double getObject(int index) - Parameters:
index- The position of the object in the list.- Returns:
- The object at the given index.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getChance
public double getChance(int index) - Parameters:
index- The position of the object in the list.- Returns:
- The weight of the object at the given index.
- Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getChance
- Parameters:
o- The object to look for in the list.- Returns:
- The weight of the object if it is contained in the list, otherwise it will return 0.
-
getTotalWeight
public double getTotalWeight()- Returns:
- The total weight of all the elements in the list.
-
getObjectList
- Returns:
- A new list of objects, with the same elements as the original list, but without having a weight associated with them.
-
add
Add an element to the list.- Returns:
- The current WeightedElements object.
-
add
Creates and adds a new WeightedElement to the internal list.- Returns:
- The current WeightedElements object.
-
remove
- Parameters:
e- The object to remove from the internal list.- Returns:
- The current WeightedElements object.
-
remove
Creates a new WeightedElement and remove any element from the internal list that matches the newly created object.- Returns:
- The current WeightedElements object.
-
set
- Parameters:
e- The new list of WeightedElements.- Returns:
- The current WeightedElements object.
-
clear
Clear the internal list.- Returns:
- The current WeightedElements object.
-
getRandom
- Returns:
- A random element from the internal list of all WeightedElements while factoring in each element's weight.
-