Class WeightedChance<E>

java.lang.Object
me.sciguymjm.uberenchant.api.utils.random.WeightedChance<E>
Type Parameters:
E - The type of value (Can be anything)

public class WeightedChance<E> extends Object
Simple utility class for weighted chances
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 Details

    • WeightedChance

      public WeightedChance()
      Constructor for a WeightedChance
    • WeightedChance

      public WeightedChance(Random random)
      Constructor for a WeightedChance
    • WeightedChance

      public WeightedChance(Collection<? extends Weighted<E>> collection)
      Constructs a WeightedChance using the specified collection
      Parameters:
      collection - A collection of items that implement the Weighted interface
  • Method Details

    • fromCollection

      public static <E> WeightedChance<E> fromCollection(Collection<? extends Weighted<E>> collection)
      Returns a new instance of a WeightedChance using specified collection
      Same as using WeightedChance(Collection)
      Type Parameters:
      E - The type of value (Can be anything)
      Parameters:
      collection - A collection of items that implement the Weighted interface
      Returns:
      A new WeightedChance using data from the specified collection
    • select

      public static <E> E select(Collection<? extends Weighted<E>> collection)
      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

      public 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
      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

      public boolean add(E value, double weight)
      Add a new possible selection with a weight.
      Parameters:
      value - The value
      weight - The weight
      Returns:
      True if it was successfully added
    • add

      public boolean add(Weighted<E> entry)
      Add a new possible selection.
      Parameters:
      entry - The selection (Implements the Weighted interface)
      Returns:
      True if it was successfully added
    • addAll

      public void addAll(Collection<? extends Weighted<E>> collection)
      Adds all the items in the specified collection to this WeightedChance
      Parameters:
      collection - The collection to add
    • remove

      public boolean remove(E value)
      Remove possible selections if the value exists.
      Parameters:
      value - The value to remove
      Returns:
      True if any were removed
    • remove

      public boolean remove(Predicate<Weighted<E>> predicate)
      Remove possible selections based on a predicate.
      Parameters:
      predicate - The predicate to use
      Returns:
      True if any were removed
    • select

      public E select()
      Selects the next possible value.
      Returns:
      The next value
    • select

      public E select(Random random)
      Selects the next possible value using specified random.
      Parameters:
      random - The Random to use
      Returns:
      The next value
    • getWeight

      public double getWeight(E value)
      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

      public boolean contains(E value)
      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

      public boolean contains(Weighted<E> weighted)
      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