Class NumberHelper


  • public final class NumberHelper
    extends Object
    A collection of useful static methods to deal with numbers. The abstract class Number is the superclass of platform classes representing numeric values that are convertible to the primitive types byte, double, float, int, long, and short.
    Since:
    2.0.0
    Author:
    Mohammed Shaik Hussain Ali
    See Also:
    Number, Byte, Double, Float, Integer, Long, Short
    • Method Summary

      Modifier and Type Method Description
      static Byte getNullSafe​(Byte value)
      Returns a non-null byte, even if the object being passed is a null byte.
      static Double getNullSafe​(Double value)
      Returns a non-null double, even if the object being passed is a null double.
      static Float getNullSafe​(Float value)
      Returns a non-null float, even if the object being passed is a null float.
      static Integer getNullSafe​(Integer value)
      Returns a non-null integer, even if the object being passed is a null integer.
      static Long getNullSafe​(Long value)
      Returns a non-null long, even if the object being passed is a null long.
      static Short getNullSafe​(Short value)
      Returns a non-null short, even if the object being passed is a null short.
      static int getNumOfDigits​(int number)
      Returns the number of digits in the passed in number.
      static int[] getUniqueRandomNumbers​(int bound, int arraySize)
      Returns an array of pseudorandom int values between zero (inclusive) and the specified upper bound (exclusive).
      static int[] getUniqueRandomNumbers​(int lowerBound, int upperBound, int arraySize)
      Returns an array of pseudorandom int values between the specified lower bound (inclusive) and the specified upper bound (exclusive).
      static boolean isPerfectSquare​(Byte value)
      Returns true if number is a Perfect Square.
      static boolean isPerfectSquare​(Integer value)
      Returns true if number is a Perfect Square.
      static boolean isPerfectSquare​(Long value)
      Returns true if number is a Perfect Square.
      static boolean isPerfectSquare​(Short value)
      Returns true if number is a Perfect Square.
    • Method Detail

      • getNullSafe

        public static Byte getNullSafe​(Byte value)
        Returns a non-null byte, even if the object being passed is a null byte.

        If the passed-in object is a non-null byte, then it is returned as-is.

        Example usage:
              Byte num1 = 10;
              Byte num2 = null;
        
              Byte num3 = NumberHelper.getNullSafe(num1);
        
              Byte num4 = NumberHelper.getNullSafe(num2);
        
              // num3 now contains the number: 10
              // num4 now contains the number: -128.
         
        Parameters:
        value - The number to safeguard against null.
        Returns:
        If value is null then Byte.MIN_VALUE.
        Since:
        3.1.0
      • getNullSafe

        public static Short getNullSafe​(Short value)
        Returns a non-null short, even if the object being passed is a null short.

        If the passed-in object is a non-null short, then it is returned as-is.

        Example usage:
              Short num1 = 10;
              Short num2 = null;
        
              Short num3 = NumberHelper.getNullSafe(num1);
        
              Short num4 = NumberHelper.getNullSafe(num2);
        
              // num3 now contains the number: 10
              // num4 now contains the number: -2^15.
         
        Parameters:
        value - The number to safeguard against null.
        Returns:
        If value is null then Short.MIN_VALUE.
        Since:
        2.0.0
      • getNullSafe

        public static Long getNullSafe​(Long value)
        Returns a non-null long, even if the object being passed is a null long.

        If the passed-in object is a non-null long, then it is returned as-is.

        Example usage:
              Long num1 = 10;
              Long num2 = null;
        
              Long num3 = NumberHelper.getNullSafe(num1);
        
              Long num4 = NumberHelper.getNullSafe(num2);
        
              // num3 now contains the number: 10
              // num4 now contains the number: -2^63
         
        Parameters:
        value - The number to safeguard against null.
        Returns:
        If value is null then Long.MIN_VALUE.
        Since:
        2.0.0
      • getNullSafe

        public static Float getNullSafe​(Float value)
        Returns a non-null float, even if the object being passed is a null float.

        If the passed-in object is a non-null float, then it is returned as-is.

        Example usage:
              Float num1 = 10.0f;
              Float num2 = null;
        
              Float num3 = NumberHelper.getNullSafe(num1);
        
              Float num4 = NumberHelper.getNullSafe(num2);
        
              // num3 now contains the number: 10
              // num4 now contains the number: 2^-149
         
        Parameters:
        value - The number to safeguard against null.
        Returns:
        If value is null then Float.MIN_VALUE.
        Since:
        2.0.0
      • getNullSafe

        public static Double getNullSafe​(Double value)
        Returns a non-null double, even if the object being passed is a null double.

        If the passed-in object is a non-null double, then it is returned as-is.

        Example usage:
              Double num1 = 10.0d;
              Double num2 = null;
        
              Double num3 = NumberHelper.getNullSafe(num1);
        
              Double num4 = NumberHelper.getNullSafe(num2);
        
              // num3 now contains the number: 10
              // num4 now contains the number: 2^-149
         
        Parameters:
        value - The number to safeguard against null.
        Returns:
        If value is null then Double.MIN_VALUE.
        Since:
        3.1.0
      • getNullSafe

        public static Integer getNullSafe​(Integer value)
        Returns a non-null integer, even if the object being passed is a null integer.

        If the passed-in object is a non-null integer, then it is returned as-is.

        Example usage:
              Integer num1 = 10;
              Integer num2 = null;
        
              Integer num3 = NumberHelper.getNullSafe(num1);
        
              Integer num4 = NumberHelper.getNullSafe(num2);
        
              // num3 now contains the number: 10
              // num4 now contains the number: -2^31
         
        Parameters:
        value - The number to safeguard against null.
        Returns:
        If value is null then Integer.MIN_VALUE.
        Since:
        1.0
      • isPerfectSquare

        public static boolean isPerfectSquare​(Byte value)
        Returns true if number is a Perfect Square.

        Example usage:
              NumberHelper.isPerfectSquare(0);    // true
              NumberHelper.isPerfectSquare(1);    // true
              NumberHelper.isPerfectSquare(81);   // true
              NumberHelper.isPerfectSquare(100);  // true
              NumberHelper.isPerfectSquare(5);    // false
              NumberHelper.isPerfectSquare(101);  // false
              NumberHelper.isPerfectSquare(250);  // false
         
        Parameters:
        value - The number to test
        Returns:
        Whether or not the number is a Perfect Square
        Since:
        3.1.0
        See Also:
        Perfect Square (Wikipedia)
      • isPerfectSquare

        public static boolean isPerfectSquare​(Short value)
        Returns true if number is a Perfect Square.

        Example usage:
              NumberHelper.isPerfectSquare(0);    // true
              NumberHelper.isPerfectSquare(1);    // true
              NumberHelper.isPerfectSquare(81);   // true
              NumberHelper.isPerfectSquare(100);  // true
              NumberHelper.isPerfectSquare(5);    // false
              NumberHelper.isPerfectSquare(101);  // false
              NumberHelper.isPerfectSquare(250);  // false
         
        Parameters:
        value - The number to test
        Returns:
        Whether or not the number is a Perfect Square
        Since:
        3.1.0
        See Also:
        Perfect Square (Wikipedia)
      • isPerfectSquare

        public static boolean isPerfectSquare​(Integer value)
        Returns true if number is a Perfect Square.

        Example usage:
              NumberHelper.isPerfectSquare(0);    // true
              NumberHelper.isPerfectSquare(1);    // true
              NumberHelper.isPerfectSquare(81);   // true
              NumberHelper.isPerfectSquare(100);  // true
              NumberHelper.isPerfectSquare(5);    // false
              NumberHelper.isPerfectSquare(101);  // false
              NumberHelper.isPerfectSquare(250);  // false
         
        Parameters:
        value - The number to test
        Returns:
        Whether or not the number is a Perfect Square
        Since:
        3.1.0
        See Also:
        Perfect Square (Wikipedia)
      • isPerfectSquare

        public static boolean isPerfectSquare​(Long value)
        Returns true if number is a Perfect Square.

        Example usage:
              NumberHelper.isPerfectSquare(0);    // true
              NumberHelper.isPerfectSquare(1);    // true
              NumberHelper.isPerfectSquare(81);   // true
              NumberHelper.isPerfectSquare(100);  // true
              NumberHelper.isPerfectSquare(5);    // false
              NumberHelper.isPerfectSquare(101);  // false
              NumberHelper.isPerfectSquare(250);  // false
         
        Parameters:
        value - The number to test
        Returns:
        Whether or not the number is a Perfect Square
        Since:
        3.1.0
        See Also:
        Perfect Square (Wikipedia)
      • getUniqueRandomNumbers

        public static int[] getUniqueRandomNumbers​(int bound,
                                                   int arraySize)
        Returns an array of pseudorandom int values between zero (inclusive) and the specified upper bound (exclusive).

        Convenience method equivalent to NumberHelper.getUniqueRandomNumbers(0, upperBound, arraySize)
        Parameters:
        bound - the upper bound (exclusive)
        arraySize - the number of unique random numbers expected
        Returns:
        an Integer array of pseudorandom int values between zero (inclusive) and the upper bound (exclusive).
        Throws:
        IllegalArgumentException - if any of the arguments are negative integers
        IllegalArgumentException - if lower bound is greater than or equal to upper bound
        Since:
        2.1.0
        See Also:
        getUniqueRandomNumbers(int, int, int)
      • getUniqueRandomNumbers

        public static int[] getUniqueRandomNumbers​(int lowerBound,
                                                   int upperBound,
                                                   int arraySize)
        Returns an array of pseudorandom int values between the specified lower bound (inclusive) and the specified upper bound (exclusive).
        Parameters:
        lowerBound - the least value returned
        upperBound - the upper bound (exclusive)
        arraySize - the number of unique random numbers expected
        Returns:
        an Integer array of pseudorandom int values between the lower bound (inclusive) and the upper bound (exclusive).
        Throws:
        IllegalArgumentException - if any of the arguments are negative integers
        IllegalArgumentException - if lower bound is greater than or equal to upper bound
        Since:
        2.1.0
        See Also:
        ThreadLocalRandom.nextInt(int, int), Collection.stream(), Stream.mapToInt(java.util.function.ToIntFunction)
      • getNumOfDigits

        public static int getNumOfDigits​(int number)
        Returns the number of digits in the passed in number.
        Parameters:
        number - for which to count the number of digits in
        Returns:
        number of digits
        Since:
        2.1.0