Package com.shaiksphere.mindsmine.jems
Class NumberHelper
java.lang.Object
com.shaiksphere.mindsmine.jems.NumberHelper
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
.-
Method Summary
Modifier and TypeMethodDescriptionstatic 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) Returnstrue
if number is a Perfect Square.static boolean
isPerfectSquare
(Integer value) Returnstrue
if number is a Perfect Square.static boolean
isPerfectSquare
(Long value) Returnstrue
if number is a Perfect Square.static boolean
isPerfectSquare
(Short value) Returnstrue
if number is a Perfect Square.
-
Method Details
-
getNullSafe
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 againstnull
.- Returns:
- If value is
null
thenByte.MIN_VALUE
. - Since:
- 3.1.0
-
getNullSafe
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 againstnull
.- Returns:
- If value is
null
thenShort.MIN_VALUE
. - Since:
- 2.0.0
-
getNullSafe
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 againstnull
.- Returns:
- If value is
null
thenLong.MIN_VALUE
. - Since:
- 2.0.0
-
getNullSafe
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 againstnull
.- Returns:
- If value is
null
thenFloat.MIN_VALUE
. - Since:
- 2.0.0
-
getNullSafe
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 againstnull
.- Returns:
- If value is
null
thenDouble.MIN_VALUE
. - Since:
- 3.1.0
-
getNullSafe
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 againstnull
.- Returns:
- If value is
null
thenInteger.MIN_VALUE
. - Since:
- 1.0
-
isPerfectSquare
Returnstrue
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:
-
isPerfectSquare
Returnstrue
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:
-
isPerfectSquare
Returnstrue
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:
-
isPerfectSquare
Returnstrue
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:
-
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 toNumberHelper.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 integersIllegalArgumentException
- if lower bound is greater than or equal to upper bound- Since:
- 2.1.0
- See Also:
-
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 returnedupperBound
- 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 integersIllegalArgumentException
- if lower bound is greater than or equal to upper bound- Since:
- 2.1.0
- See Also:
-
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
-