Package dev.aherscu.qa.testing.utils
Class NumberUtils
- java.lang.Object
-
- dev.aherscu.qa.testing.utils.NumberUtils
-
public final class NumberUtils extends Object
Number utilities.- Author:
- aherscu
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Number
numericValueOf(String text, Class<? extends Number>... expectedNumericType)
Tries to callvalueOf(String)
on specified numeric type with specified text.static int
parseIntOrZero(String value)
Safely parses specified integer.static long
parseLongOrZero(String value)
Safely parses specified long.static double
safeDoubleOf(Number number)
Returns a double even if no number was supplied.
-
-
-
Method Detail
-
numericValueOf
@SafeVarargs public static Number numericValueOf(String text, Class<? extends Number>... expectedNumericType)
Tries to callvalueOf(String)
on specified numeric type with specified text.NOTE: There is no generic method for parsing text into any given
Number
subtype; this method will work for part of these subtypes, for others will just throw anUnsupportedOperationException
:- Parameters:
text
- the text containing numeric valueexpectedNumericType
- the expected numeric type; optional, assumingDouble
if not provided- Returns:
- the numeric value
- Throws:
UnsupportedOperationException
- if failed to call the valueOf method on expected number type
-
parseIntOrZero
public static int parseIntOrZero(String value)
Safely parses specified integer.- Parameters:
value
- integer value as string- Returns:
- the integer value if parsed successfully, otherwise 0
-
parseLongOrZero
public static long parseLongOrZero(String value)
Safely parses specified long.- Parameters:
value
- long value as string- Returns:
- the long value if parsed successfully, otherwise 0
-
safeDoubleOf
public static double safeDoubleOf(Number number)
Returns a double even if no number was supplied.- Parameters:
number
- the number- Returns:
- the double representation of the supplied number or
Double.NaN
if no number was supplied.
-
-