Class MathUtils

java.lang.Object
org.kingdoms.utils.MathUtils

public final class MathUtils extends Object
A util class providing a little more extra features in addition
to Java's Math class.
Uses the latest Java 8 concurrent techniques.
  • Constructor Details

    • MathUtils

      public MathUtils()
  • Method Details

    • getPageNumbers

      public static int getPageNumbers(int items, int size)
    • evaluateEquation

      public static double evaluateEquation(String eqn, Object... edits)
      Evalutes the given equation using Java's Math class methods. Supports all the methods from Math class as string. All the mathematical operators and unique charcaters are also supported. An invocation of this method creates an instance of MathCompiler.
      Parameters:
      eqn - the equation to evalute.
      Returns:
      the calculated equation.
    • eval

      public static double eval(String eqn, org.bukkit.OfflinePlayer placeholder, Object... edits)
    • getFractionalPart

      public static double getFractionalPart(double number)
    • eval

      public static double eval(String eqn, PlaceholderContextBuilder settings)
    • eval

      public static double eval(org.kingdoms.utils.compilers.MathCompiler.Expression eqn, PlaceholderContextProvider context)
    • eval

      public static double eval(String eqn, MessageBuilder builder)
    • expectDouble

      public static Double expectDouble(String id, Object data)
    • parseDouble

      public static Double parseDouble(String str)
    • parseFloat

      public static Float parseFloat(String str)
    • eval

      public static double eval(String eqn, Kingdom placeholder, Object... edits)
    • eval

      public static double eval(String eqn, Nation placeholder, Object... edits)
    • parseInt

      public static Integer parseInt(CharSequence str)
    • parseInt

      public static Integer parseInt(CharSequence str, boolean signed)
    • parseIntUnchecked

      public static Integer parseIntUnchecked(CharSequence str, boolean signed)
    • parseLong

      public static Long parseLong(CharSequence str)
    • parseLong

      public static Long parseLong(CharSequence str, boolean signed)
    • averageOf

      public static long averageOf(long... longs)
    • hasChance

      public static boolean hasChance(double percent)
      Gets a random chance between 0 and 100

      An invocation of this method behaves in exactly the same way as the invocation

      randInt(0, 100) <= percent
      Parameters:
      percent - the required chance percent.
      Returns:
      true if the chance is equals or greater than the required chance.
    • randInt

      public static int randInt(int min, int max)
      A concurrent thread-safe random number generator. Uses ThreadLocalRandom
      Parameters:
      min - the possible minimum amount.
      max - the possible maximum amount.
      Returns:
      a random number between the given values.
    • getPercent

      public static int getPercent(double amount, double max)
      Gets the percentage of the given number with a max value.

      An invocation of this method of the form getPercent(current, max) yields exactly the same result as the expression

      getAmountFromAmount(double, double, double)(current, max, 100)
      Parameters:
      max - the max possible value for the given value.
      amount - the value.
      Returns:
      the percentage of the value.
    • percentOfAmount

      public static double percentOfAmount(double percent, double amount)
    • percentOfPercent

      public static int percentOfPercent(int... percents)
    • getAmountFromAmount

      public static int getAmountFromAmount(double current, double max, double amount)
      Gets the percentage of the given number with a max value.

      Examples

      tet
      See Also:
    • roundToDigits

      public static double roundToDigits(double value, int precision)
      Rounds a number to the specified decimal digit.

      Example

      roundToDigits(1.1674524234, 2);
      // Output: 1.17
      Parameters:
      value - the actual number.
      precision - the decimal numbers to keep and round others.
      Returns:
      a rounded decimal.
    • getShortNumber

      public static String getShortNumber(Number number)
      Converts big numbers to one decimal numbers with number prefix. Only supports thousand, million and billion.

      Examples

           getShortNumber(1255);
           // Output: 1.2K
      
           getShortNumber(1560000);
           // Output: 1.5mil
      
           getShortNumber(4900000000);
           // Output: 4.9bil
       
      Parameters:
      number - the number to convert.
      Returns:
      a short formatted number.
    • increasingRandInt

      public static int increasingRandInt(int min, int max)
      Gets a random number within the range of the given numbers with
      a higher chance for higher numbers that x + 1 has +1 more
      chance than x.
      Parameters:
      min - the minimum number in the range.
      max - the maximum number in the range.
      Returns:
      a chosen number between the given range.
    • decreasingRandInt

      public static int decreasingRandInt(int min, int max)
      Gets a random number within the range of the given numbers with
      a lower chance for lower numbers that x + 1 has -1 less
      chance than x.
      Parameters:
      min - the minimum number in the range.
      max - the maximum number in the range.
      Returns:
      a chosen number between the given range.
    • naturalSum

      public static int naturalSum(int n)
    • factorial

      public static double factorial(int n)
    • isPrime

      public static boolean isPrime(int number)
      Prime numbers are Natural numbers that must ONLY have two factors, which is always 1 and themselves An example would be 2, 3, 5 and 7 1 is not a prime not, because it only has one factor, and that is 1
      Parameters:
      number -
      Returns:
    • generatePrimes

      public static int[] generatePrimes(int until)
    • sqrtn

      public static double sqrtn(double num, int n)
    • isEven

      public static boolean isEven(int number)
      Checks if a number is even using the AND bitwise operator.
      Parameters:
      number - the number to check
      Returns:
      true if the number is even, otherwise false as it's an odd number.
    • isOdd

      public static boolean isOdd(int number)
      Integer = 32bits 10101010 00000000 = 0 AND = & 00000000 = 0 Even
      Parameters:
      number - the number to check.
      Returns:
      true if the number is odd, otherwise false as it's an even number.