Class Chance

java.lang.Object
net.luis.utils.util.Chance

public class Chance extends Object
Represents a chance of something happening.
The chance is a double between 0.0 and 1.0, where 0.0 is 0% and 1.0 is 100%.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final double
    The chance value of something happens.
    private static final Pattern
    The pattern used to parse a chance.
    static final Chance
    A constant value for a chance of 1.0.
    private static final Random
    The random number generator used to generate the chance.
    static final Chance
    A constant value for a chance of 0.0.
    This is the default chance.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Chance(double chance)
    Constructs a new chance with the given chance.
    The chance will be clamped between 0.0 and 1.0.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    int
     
    boolean
    Checks if the chance is always false.
    boolean
    Checks if the chance is always true.
    static @NotNull Chance
    of(double chance)
    Creates a new chance with the given chance.
    static @NotNull Chance
    parse(@Nullable String chance)
    Parses a chance from a string.
    boolean
    Gets the result of the chance.
    static void
    setSeed(long seed)
    Sets the seed of the random number generator.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • CHANCE_PATTERN

      private static final Pattern CHANCE_PATTERN
      The pattern used to parse a chance.

      The chance can be either 1.0, or 0. followed by at least one digit.
      The chance must be followed by a percent sign.
      A dot or a comma can be used as a decimal separator.

    • RNG

      private static final Random RNG
      The random number generator used to generate the chance.
    • ZERO

      public static final Chance ZERO
      A constant value for a chance of 0.0.
      This is the default chance.
    • ONE

      public static final Chance ONE
      A constant value for a chance of 1.0.
    • chance

      private final double chance
      The chance value of something happens.
  • Constructor Details

    • Chance

      protected Chance(double chance)
      Constructs a new chance with the given chance.
      The chance will be clamped between 0.0 and 1.0.
      Parameters:
      chance - The chance value as a double
  • Method Details

    • of

      @NotNull public static @NotNull Chance of(double chance)
      Creates a new chance with the given chance.

      If the chance is 0.0 or below, ZERO will be returned.
      If the chance is 1.0 or above, ONE will be returned.

      Parameters:
      chance - The chance as a double
      Returns:
      The created chance
    • parse

      @NotNull public static @NotNull Chance parse(@Nullable @Nullable String chance)
      Parses a chance from a string.

      The chance can be either 1.0, or 0. followed by at least one digit.
      The chance must be followed by a percent sign.
      A dot or a comma can be used as a decimal separator.

      If the given string to parse is null or empty, ZERO will be returned.
      If the given string does not match the pattern (CHANCE_PATTERN), ZERO will be returned.

      Parameters:
      chance - The chance string to parse
      Returns:
      The parsed chance or ZERO
    • setSeed

      public static void setSeed(long seed)
      Sets the seed of the random number generator.
      Parameters:
      seed - The seed
    • isTrue

      public boolean isTrue()
      Checks if the chance is always true.
      Returns:
      True if the chance is 1.0 or above, otherwise false
    • isFalse

      public boolean isFalse()
      Checks if the chance is always false.
      Returns:
      True if the chance is 0.0 or below, otherwise false
    • result

      public boolean result()
      Gets the result of the chance.

      If isTrue() this will always return true.
      If isFalse() this will always return false.
      Otherwise, true if the random number generator returns a number below the chance.

      Returns:
      The result of the chance
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object