Class WeightedElements<T>

java.lang.Object
at.noahb.utils.generic.WeightedElements<T>

public class WeightedElements<T> extends Object
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 Details

    • WeightedElements

      public WeightedElements()
  • Method Details

    • getRandom

      public static <T> T getRandom(List<WeightedElement<T>> weightedElements)
      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

      public static WeightedElement of(Object o, double weight)
      Create a new WeightedElements object.
      Parameters:
      o - The object to hold.
      weight - The weight of the object.
      Returns:
      A new WeightedElements object.
    • getElements

      public List<WeightedElement<T>> getElements()
      Returns:
      The list of weighted elements.
    • getElement

      public WeightedElement<T> getElement(int index)
      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

      public double getChance(Object o)
      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

      public List<T> getObjectList()
      Returns:
      A new list of objects, with the same elements as the original list, but without having a weight associated with them.
    • add

      public WeightedElements<T> add(WeightedElement<T> e)
      Add an element to the list.
      Returns:
      The current WeightedElements object.
    • add

      public WeightedElements<T> add(T o, double weight)
      Creates and adds a new WeightedElement to the internal list.
      Returns:
      The current WeightedElements object.
    • remove

      public WeightedElements<T> remove(WeightedElement e)
      Parameters:
      e - The object to remove from the internal list.
      Returns:
      The current WeightedElements object.
    • remove

      public WeightedElements<T> remove(T o, double weight)
      Creates a new WeightedElement and remove any element from the internal list that matches the newly created object.
      Returns:
      The current WeightedElements object.
    • set

      public WeightedElements<T> set(WeightedElement<T>... e)
      Parameters:
      e - The new list of WeightedElements.
      Returns:
      The current WeightedElements object.
    • clear

      public WeightedElements<T> clear()
      Clear the internal list.
      Returns:
      The current WeightedElements object.
    • getRandom

      public T getRandom()
      Returns:
      A random element from the internal list of all WeightedElements while factoring in each element's weight.