Class WeightCollection<T>

java.lang.Object
net.luis.utils.collection.WeightCollection<T>
Type Parameters:
T - The type of the elements

public class WeightCollection<T> extends Object
A collection which returns a random element based on the weight of the elements.
The higher the weight, the higher the chance that the element is returned.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final NavigableMap<Integer,T>
    The internal map.
    private final Random
    The random number generator.
    private int
    The total weight of the collection.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new empty weight collection.
    WeightCollection(@NotNull Random rng)
    Constructs a new empty weight collection with the given random number generator.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int weight, T value)
    Adds the given value with the given weight to the collection.
    void
    Removes all values from the collection.
    boolean
    contains(T value)
    Checks if the collection contains the given value.
    boolean
     
    int
    Returns the number of elements in the collection.
    int
    Returns the total weight of the collection.
    int
     
    boolean
    Checks if the collection is empty.
    Gets an element from the collection based on the weight of the elements.
    Elements with a higher weight have a higher chance to be returned.
    boolean
    remove(T value)
    Removes the given value from the collection.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • map

      private final NavigableMap<Integer,T> map
      The internal map.
    • rng

      private final Random rng
      The random number generator.
    • total

      private int total
      The total weight of the collection.
  • Constructor Details

    • WeightCollection

      public WeightCollection()
      Constructs a new empty weight collection.
    • WeightCollection

      public WeightCollection(@NotNull @NotNull Random rng)
      Constructs a new empty weight collection with the given random number generator.
      Parameters:
      rng - The random number generator
      Throws:
      NullPointerException - If the random number generator is null
  • Method Details

    • add

      public void add(int weight, @NotNull T value)
      Adds the given value with the given weight to the collection.
      Parameters:
      weight - The weight of the value
      value - The value to add
      Throws:
      NullPointerException - If the value is null
      IllegalArgumentException - If the weight is less than or equal to
    • remove

      public boolean remove(@Nullable T value)
      Removes the given value from the collection.
      Parameters:
      value - The value to remove
      Returns:
      True, if the value was removed, otherwise false
    • contains

      public boolean contains(@Nullable T value)
      Checks if the collection contains the given value.
      Parameters:
      value - The value to check
      Returns:
      True, if the collection contains the value, otherwise false
    • clear

      public void clear()
      Removes all values from the collection.
    • next

      public T next()
      Gets an element from the collection based on the weight of the elements.
      Elements with a higher weight have a higher chance to be returned.
      Returns:
      An weighted random element
    • isEmpty

      public boolean isEmpty()
      Checks if the collection is empty.
      Returns:
      True, if the collection is empty, otherwise false
    • getSize

      public int getSize()
      Returns the number of elements in the collection.
      Returns:
      The size of the collection
    • getTotal

      public int getTotal()
      Returns the total weight of the collection.
      Returns:
      The total weight
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object