Class WeightedTable<T>
- java.lang.Object
-
- org.spongepowered.api.util.weighted.RandomObjectTable<T>
-
- org.spongepowered.api.util.weighted.WeightedTable<T>
-
- Type Parameters:
T
- The entry type
- All Implemented Interfaces:
java.lang.Iterable<TableEntry<T>>
,java.util.Collection<TableEntry<T>>
- Direct Known Subclasses:
UnmodifiableWeightedTable
public class WeightedTable<T> extends RandomObjectTable<T>
A table of weighted entry, each entry is given a weight, the higher the weight the more likely the chance that the entry is chosen. Each roll will only return a single entries value.
-
-
Field Summary
-
Fields inherited from class org.spongepowered.api.util.weighted.RandomObjectTable
entries
-
-
Constructor Summary
Constructors Constructor Description WeightedTable()
Creates a newWeightedTable
with a default roll count of1
.WeightedTable(int rolls)
Creates a newWeightedTable
with the provided number ofrolls
.WeightedTable(VariableAmount rolls)
Creates a newWeightedTable
with the providedVariableAmount
for the amount of rolls and variance.
-
Method Summary
All Methods Instance 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
equals(java.lang.Object o)
java.util.List<T>
get(java.util.Random rand)
Performs a number of rolls according to the number of rolls defined byRandomObjectTable.rolls()
and returns items from the table for each roll.int
hashCode()
java.util.Iterator<TableEntry<T>>
iterator()
protected void
recalculateWeight()
Recalculates the total weight of all entries in this table.boolean
remove(java.lang.Object entry)
boolean
removeAll(java.util.Collection<?> c)
boolean
removeObject(java.lang.Object entry)
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)
java.lang.String
toString()
-
Methods inherited from class org.spongepowered.api.util.weighted.RandomObjectTable
contains, containsAll, containsAllObjects, containsObject, entries, isEmpty, rolls, setRolls, setRolls, size, toArray, toArray
-
-
-
-
Constructor Detail
-
WeightedTable
public WeightedTable()
Creates a newWeightedTable
with a default roll count of1
.
-
WeightedTable
public WeightedTable(int rolls)
Creates a newWeightedTable
with the provided number ofrolls
.- Parameters:
rolls
- The rolls for variance
-
WeightedTable
public WeightedTable(VariableAmount rolls)
Creates a newWeightedTable
with the providedVariableAmount
for the amount of rolls and variance.- Parameters:
rolls
- The rolls for variance
-
-
Method Detail
-
add
public boolean add(TableEntry<T> entry)
- Specified by:
add
in interfacejava.util.Collection<T>
- Overrides:
add
in classRandomObjectTable<T>
-
add
public boolean add(T object, double weight)
Description copied from class:RandomObjectTable
Adds the given object to the table with the given weight.- Overrides:
add
in classRandomObjectTable<T>
- 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>
- Overrides:
addAll
in classRandomObjectTable<T>
-
remove
public boolean remove(java.lang.Object entry)
- Specified by:
remove
in interfacejava.util.Collection<T>
- Overrides:
remove
in classRandomObjectTable<T>
-
removeObject
public boolean removeObject(java.lang.Object entry)
Description copied from class:RandomObjectTable
Removes the first instance of an entry in this table which is aWeightedObject
entry and contains the given object.- Overrides:
removeObject
in classRandomObjectTable<T>
- Parameters:
entry
- 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>
- Overrides:
removeAll
in classRandomObjectTable<T>
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
- Specified by:
retainAll
in interfacejava.util.Collection<T>
- Overrides:
retainAll
in classRandomObjectTable<T>
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Collection<T>
- Overrides:
clear
in classRandomObjectTable<T>
-
recalculateWeight
protected void recalculateWeight()
Recalculates the total weight of all entries in this table.
-
get
public java.util.List<T> get(java.util.Random rand)
Description copied from class:RandomObjectTable
Performs a number of rolls according to the number of rolls defined byRandomObjectTable.rolls()
and returns items from the table for each roll.- Specified by:
get
in classRandomObjectTable<T>
- Parameters:
rand
- The random object to use- Returns:
- The returned items, may be empty but not null
-
iterator
public java.util.Iterator<TableEntry<T>> iterator()
- Specified by:
iterator
in interfacejava.util.Collection<T>
- Specified by:
iterator
in interfacejava.lang.Iterable<T>
- Overrides:
iterator
in classRandomObjectTable<T>
-
equals
public boolean equals(java.lang.Object o)
- Specified by:
equals
in interfacejava.util.Collection<T>
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Specified by:
hashCode
in interfacejava.util.Collection<T>
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-