Class RandomObjectTable<T>
- java.lang.Object
-
- org.spongepowered.api.util.weighted.RandomObjectTable<T>
-
- Type Parameters:
T
- The type of entry
- All Implemented Interfaces:
java.lang.Iterable<TableEntry<T>>
,java.util.Collection<TableEntry<T>>
- Direct Known Subclasses:
ChanceTable
,WeightedTable
public abstract class RandomObjectTable<T> extends java.lang.Object implements java.util.Collection<TableEntry<T>>
An abstract table holding weighted objects. Objects may be retrieved from the table according to weight or chance.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<TableEntry<T>>
entries
-
Constructor Summary
Constructors Modifier Constructor Description protected
RandomObjectTable(int rolls)
Creates a newRandomObjectTable
with the provided number of rolls.protected
RandomObjectTable(VariableAmount rolls)
Creates a newRandomObjectTable
with the provided number of rolls.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
add(TableEntry<T> entry)
boolean
add(T object, double weight)
Adds the given object to the table with the given weight.boolean
addAll(java.util.Collection<? extends TableEntry<T>> c)
void
clear()
boolean
contains(java.lang.Object o)
boolean
containsAll(java.util.Collection<?> c)
boolean
containsAllObjects(java.util.Collection<?> c)
Gets if this table contains all of the given objects, the objects may either beTableEntry
s or the objects contained within the entries.boolean
containsObject(java.lang.Object obj)
Gets if this table contains the given object, the object may either be aTableEntry
or the object contained within.java.util.List<TableEntry<T>>
entries()
Gets the entries in the table.abstract java.util.List<T>
get(java.util.Random rand)
Performs a number of rolls according to the number of rolls defined byrolls()
and returns items from the table for each roll.boolean
isEmpty()
java.util.Iterator<TableEntry<T>>
iterator()
boolean
remove(java.lang.Object entry)
boolean
removeAll(java.util.Collection<?> c)
boolean
removeObject(java.lang.Object object)
Removes the first instance of an entry in this table which is aWeightedObject
entry and contains the given object.boolean
retainAll(java.util.Collection<?> c)
VariableAmount
rolls()
Gets the number of times this table will roll while retrieving items.void
setRolls(int rolls)
Sets the number of times this table will roll while retrieving items.void
setRolls(VariableAmount rolls)
Sets the number of times this table will roll while retrieving items.int
size()
java.lang.Object[]
toArray()
<R> R[]
toArray(R[] a)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
Field Detail
-
entries
protected final java.util.List<TableEntry<T>> entries
-
-
Constructor Detail
-
RandomObjectTable
protected RandomObjectTable(int rolls)
Creates a newRandomObjectTable
with the provided number of rolls.- Parameters:
rolls
- the rolls- See Also:
rolls()
-
RandomObjectTable
protected RandomObjectTable(VariableAmount rolls)
Creates a newRandomObjectTable
with the provided number of rolls.- Parameters:
rolls
- the rolls- See Also:
rolls()
-
-
Method Detail
-
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 interfacejava.util.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 objectweight
- The weight of the object- Returns:
- If the object was successfully added
-
addAll
public boolean addAll(java.util.Collection<? extends TableEntry<T>> c)
- Specified by:
addAll
in interfacejava.util.Collection<T>
-
contains
public boolean contains(java.lang.Object o)
- Specified by:
contains
in interfacejava.util.Collection<T>
-
containsObject
public boolean containsObject(java.lang.Object obj)
Gets if this table contains the given object, the object may either be aTableEntry
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(java.util.Collection<?> c)
- Specified by:
containsAll
in interfacejava.util.Collection<T>
-
containsAllObjects
public boolean containsAllObjects(java.util.Collection<?> c)
Gets if this table contains all of the given objects, the objects may either beTableEntry
s 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 interfacejava.util.Collection<T>
-
remove
public boolean remove(java.lang.Object entry)
- Specified by:
remove
in interfacejava.util.Collection<T>
-
removeObject
public boolean removeObject(java.lang.Object object)
Removes the first instance of an entry in this table which is aWeightedObject
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(java.util.Collection<?> c)
- Specified by:
removeAll
in interfacejava.util.Collection<T>
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
- Specified by:
retainAll
in interfacejava.util.Collection<T>
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Collection<T>
-
size
public int size()
- Specified by:
size
in interfacejava.util.Collection<T>
-
get
public abstract java.util.List<T> get(java.util.Random rand)
Performs a number of rolls according to the number of rolls defined byrolls()
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 java.util.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 java.util.Iterator<TableEntry<T>> iterator()
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArray
in interfacejava.util.Collection<T>
-
toArray
public <R> R[] toArray(R[] a)
- Specified by:
toArray
in interfacejava.util.Collection<T>
-
-