Class RandomObjectTable<T>

java.lang.Object
org.spongepowered.api.util.weighted.RandomObjectTable<T>
Type Parameters:
T - The type of entry
All Implemented Interfaces:
Iterable<TableEntry<T>>, Collection<TableEntry<T>>
Direct Known Subclasses:
ChanceTable, WeightedTable

public abstract class RandomObjectTable<T> extends Object implements Collection<TableEntry<T>>
An abstract table holding weighted objects. Objects may be retrieved from the table according to weight or chance.
  • Field Details

  • Constructor Details

    • RandomObjectTable

      protected RandomObjectTable(int rolls)
      Creates a new RandomObjectTable with the provided number of rolls.
      Parameters:
      rolls - the rolls
      See Also:
    • RandomObjectTable

      protected RandomObjectTable(VariableAmount rolls)
      Creates a new RandomObjectTable with the provided number of rolls.
      Parameters:
      rolls - the rolls
      See Also:
  • Method Details

    • rolls

      public VariableAmount rolls()
      Gets the number of times this table will roll while retrieving items. For each roll a complete pass through of the table will occur.
      Returns:
      The number of rolls
    • setRolls

      public void setRolls(VariableAmount rolls)
      Sets the number of times this table will roll while retrieving items.
      Parameters:
      rolls - The new roll count
    • setRolls

      public void setRolls(int rolls)
      Sets the number of times this table will roll while retrieving items.
      Parameters:
      rolls - The new roll count
    • add

      public boolean add(TableEntry<T> entry)
      Specified by:
      add in interface Collection<T>
    • add

      public boolean add(T object, double weight)
      Adds the given object to the table with the given weight.
      Parameters:
      object - The new object
      weight - The weight of the object
      Returns:
      If the object was successfully added
    • addAll

      public boolean addAll(Collection<? extends TableEntry<T>> c)
      Specified by:
      addAll in interface Collection<T>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<T>
    • containsObject

      public boolean containsObject(Object obj)
      Gets if this table contains the given object, the object may either be a TableEntry or the object contained within.
      Parameters:
      obj - The object to check for
      Returns:
      If the object is contained within the table
    • containsAll

      public boolean containsAll(Collection<?> c)
      Specified by:
      containsAll in interface Collection<T>
    • containsAllObjects

      public boolean containsAllObjects(Collection<?> c)
      Gets if this table contains all of the given objects, the objects may either be TableEntrys or the objects contained within the entries.
      Parameters:
      c - The objects to check for
      Returns:
      If all of the objects are contained within the table
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<T>
    • remove

      public boolean remove(Object entry)
      Specified by:
      remove in interface Collection<T>
    • removeObject

      public boolean removeObject(Object object)
      Removes the first instance of an entry in this table which is a WeightedObject entry and contains the given object.
      Parameters:
      object - The object to remove
      Returns:
      If the table was changed as a result
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<T>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
    • get

      public abstract List<T> get(Random rand)
      Performs a number of rolls according to the number of rolls defined by rolls() and returns items from the table for each roll.
      Parameters:
      rand - The random object to use
      Returns:
      The returned items, may be empty but not null
    • entries

      public List<TableEntry<T>> entries()
      Gets the entries in the table. Note that the specific sub class of this abstract table will determine the context that the entry weights should be interpreted in (either weights or chances).
      Returns:
      The raw entries
    • iterator

      public Iterator<TableEntry<T>> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<T>
    • toArray

      public <R> R[] toArray(R[] a)
      Specified by:
      toArray in interface Collection<T>