Class WeightedChance<E>

java.lang.Object
me.sciguymjm.uberenchant.api.utils.WeightedChance<E>
Type Parameters:
E - - The type of result (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(0.9, false); // False 90% of the time
 wc.add(0.1, true); // True 10% of the time

 System.out.println(wc.next()); // Print the result
 
  • Constructor Details

    • WeightedChance

      public WeightedChance()
  • Method Details

    • add

      public boolean add(double weight, E result)
      Add a new possible selection with a percent chance.
      Parameters:
      weight - The chance (Between 0.0 and 1.0, 1.0 being 100% chance)
      result - The result
      Returns:
      True if it was successfully added
    • remove

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

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

      public E next()
      Gets the next possible result.
      Returns:
      The next result
    • getWeight

      public double getWeight(E result)
      Gets the weight of the specified result.
      Parameters:
      result - The result to get the weight for
      Returns:
      The weight or 0 if not found
    • contains

      public boolean contains(E result)
      Checks if this WeightedChance contains the specified result or not.
      Parameters:
      result - the result to check for
      Returns:
      True if this WeightedChance contains the result