Class WeightedChance<E>
java.lang.Object
me.sciguymjm.uberenchant.api.utils.random.WeightedChance<E>
- Type Parameters:
E
- The type of value (Can be anything)
Simple utility class for weighted chances
Example Usage:
Example Usage:
WeightedChance<Boolean> wc = new WeightedChance<Boolean>(); // Create new WeightedChance for booleans
wc.add(9.0, false); // False 90% of the time (a 9 out of 10 chance)
wc.add(1.0, true); // True 10% of the time (a 1 out of 10 chance)
System.out.println(wc.next()); // Print the value
-
Constructor Summary
ConstructorDescriptionConstructor for a WeightedChanceWeightedChance
(Collection<? extends Weighted<E>> collection) Constructs a WeightedChance using the specified collectionWeightedChance
(Random random) Constructor for a WeightedChance -
Method Summary
Modifier and TypeMethodDescriptionboolean
Add a new possible selection with a weight.boolean
Add a new possible selection.void
addAll
(Collection<? extends Weighted<E>> collection) Adds all the items in the specified collection to this WeightedChanceboolean
Checks if this WeightedChance contains the specified value or not.boolean
Checks if this WeightedChance contains the specified entry or not.static <E> WeightedChance<E>
fromCollection
(Collection<? extends Weighted<E>> collection) Returns a new instance of a WeightedChance using specified collection
Same as usingWeightedChance(Collection)
double
getTotal()
Gets the total weight of all the valuesdouble
Gets the weight of the specified value.boolean
Remove possible selections if the value exists.boolean
Remove possible selections based on a predicate.select()
Selects the next possible value.static <E> E
select
(Collection<? extends Weighted<E>> collection) Static method to select a random weighted item from a collectionSelects the next possible value using specified random.static <E> E
select
(Random random, Collection<? extends Weighted<E>> collection) Static method to select a random weighted item from a collection using a random
-
Constructor Details
-
WeightedChance
public WeightedChance()Constructor for a WeightedChance -
WeightedChance
Constructor for a WeightedChance -
WeightedChance
Constructs a WeightedChance using the specified collection- Parameters:
collection
- A collection of items that implement theWeighted
interface
-
-
Method Details
-
fromCollection
Returns a new instance of a WeightedChance using specified collection
Same as usingWeightedChance(Collection)
- Type Parameters:
E
- The type of value (Can be anything)- Parameters:
collection
- A collection of items that implement theWeighted
interface- Returns:
- A new WeightedChance using data from the specified collection
-
select
Static method to select a random weighted item from a collection- Type Parameters:
E
- The type of value- Parameters:
collection
- The collection- Returns:
- A randomly selected entry based on weight
-
select
Static method to select a random weighted item from a collection using a random- Type Parameters:
E
- The type of value- Parameters:
random
- The random to use (Good for setting a seed)collection
- The collection- Returns:
- A randomly selected entry based on weight
-
add
Add a new possible selection with a weight.- Parameters:
value
- The valueweight
- The weight- Returns:
- True if it was successfully added
-
add
Add a new possible selection.- Parameters:
entry
- The selection (Implements theWeighted
interface)- Returns:
- True if it was successfully added
-
addAll
Adds all the items in the specified collection to this WeightedChance- Parameters:
collection
- The collection to add
-
remove
Remove possible selections if the value exists.- Parameters:
value
- The value to remove- Returns:
- True if any were removed
-
remove
Remove possible selections based on a predicate.- Parameters:
predicate
- The predicate to use- Returns:
- True if any were removed
-
select
Selects the next possible value.- Returns:
- The next value
-
select
Selects the next possible value using specified random.- Parameters:
random
- The Random to use- Returns:
- The next value
-
getWeight
Gets the weight of the specified value.- Parameters:
value
- The value to get the weight for- Returns:
- The weight or 0 if not found
-
getTotal
public double getTotal()Gets the total weight of all the values- Returns:
- Total weight
-
contains
Checks if this WeightedChance contains the specified value or not.- Parameters:
value
- the value to check for- Returns:
- True if this WeightedChance contains the value
-
contains
Checks if this WeightedChance contains the specified entry or not.- Parameters:
weighted
- the entry to check for- Returns:
- True if this WeightedChance contains the value
-