Package de.bcxp.challenge.common.utility
Class StringParsingUtility
java.lang.Object
de.bcxp.challenge.common.utility.StringParsingUtility
Utility class providing methods for parsing Strings.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
This is a utility class which provides only static methods, therefore it shouldn't be instantiated. -
Method Summary
Modifier and TypeMethodDescriptionstatic double
getDoubleFromString
(String numberCandidate, Locale locale) Parses double from the givenString
.static long
getLongFromString
(String numberCandidate, Locale locale) Parses int from the givenString
.private static Number
getParsedNumber
(String numberCandidate, Locale locale, NumberFormat numberFormat) private static String
validateCandidateString
(String numberCandidate) Validates that the provided string is non-null, not empty, and doesn't contain illegal characters that don't conform to number formatting.
-
Field Details
-
logger
private static final org.apache.logging.log4j.Logger logger -
localeLogMsg
- See Also:
-
localeExceptionMsg
- See Also:
-
-
Constructor Details
-
StringParsingUtility
private StringParsingUtility()This is a utility class which provides only static methods, therefore it shouldn't be instantiated.
-
-
Method Details
-
getLongFromString
public static long getLongFromString(String numberCandidate, Locale locale) throws NumberFormatException, ParseException Parses int from the givenString
.- Parameters:
numberCandidate
- the string to parse as an integerlocale
- Locale used when parsing number- Returns:
- the parsed integer value
- Throws:
ParseException
- if the string cannot be parsed into a valid numberNumberFormatException
- if the String doesn't contain a valid numberIllegalArgumentException
- if the input string is null, empty, or invalid
-
getDoubleFromString
public static double getDoubleFromString(String numberCandidate, Locale locale) throws NumberFormatException, ParseException Parses double from the givenString
.- Parameters:
numberCandidate
- the string to parse as a doublelocale
- Locale used when parsing number- Returns:
- the parsed double value
- Throws:
ParseException
- if the string cannot be parsed into a valid numberNumberFormatException
- if the String doesn't contain a valid numberIllegalArgumentException
- if the input string is null, empty, or invalid
-
getParsedNumber
private static Number getParsedNumber(String numberCandidate, Locale locale, NumberFormat numberFormat) throws ParseException - Parameters:
numberCandidate
- the string representation of the number to parse; must not benull
or emptylocale
- theLocale
whose formatting rules should be used for parsing; must not benull
numberFormat
- Format to use when parsing the numberCandidateString
- Returns:
- a
Number
representing the parsed value - Throws:
ParseException
- if the string cannot be parsed as a number according to the given localeNullPointerException
- ifnumberCandidate
orlocale
isnull
- See Also:
-
validateCandidateString
Validates that the provided string is non-null, not empty, and doesn't contain illegal characters that don't conform to number formatting.- Parameters:
numberCandidate
- the string to validate- Returns:
- the validated
String
stripped of leading and trailing whitespaces - Throws:
IllegalArgumentException
- if the string isnull
or emptyNumberFormatException
- if the string contains illegal characters
-