java.lang.Object
net.luis.utils.collection.WeightCollection<T>
- Type Parameters:
T
- The type of the elements
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.
The higher the weight, the higher the chance that the element is returned.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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 TypeMethodDescriptionvoid
Adds the given value with the given weight to the collection.void
clear()
Removes all values from the collection.boolean
Checks if the collection contains the given value.boolean
int
getSize()
Returns the number of elements in the collection.int
getTotal()
Returns the total weight of the collection.int
hashCode()
boolean
isEmpty()
Checks if the collection is empty.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.boolean
Removes the given value from the collection.toString()
-
Field Details
-
map
The internal map. -
rng
The random number generator. -
total
private int totalThe total weight of the collection.
-
-
Constructor Details
-
WeightCollection
public WeightCollection()Constructs a new empty weight collection. -
WeightCollection
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
Adds the given value with the given weight to the collection.- Parameters:
weight
- The weight of the valuevalue
- The value to add- Throws:
NullPointerException
- If the value is nullIllegalArgumentException
- If the weight is less than or equal to
-
remove
Removes the given value from the collection.- Parameters:
value
- The value to remove- Returns:
- True, if the value was removed, otherwise false
-
contains
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
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
-
equals
-
hashCode
public int hashCode()
-