Class StringParsingUtility

java.lang.Object
de.bcxp.challenge.common.utility.StringParsingUtility

public final class StringParsingUtility extends Object
Utility class providing methods for parsing Strings.
  • Field Details

  • 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 given String.
      Parameters:
      numberCandidate - the string to parse as an integer
      locale - Locale used when parsing number
      Returns:
      the parsed integer value
      Throws:
      ParseException - if the string cannot be parsed into a valid number
      NumberFormatException - if the String doesn't contain a valid number
      IllegalArgumentException - 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 given String.
      Parameters:
      numberCandidate - the string to parse as a double
      locale - Locale used when parsing number
      Returns:
      the parsed double value
      Throws:
      ParseException - if the string cannot be parsed into a valid number
      NumberFormatException - if the String doesn't contain a valid number
      IllegalArgumentException - if the input string is null, empty, or invalid
    • getParsedNumber

      private static Number getParsedNumber(String numberCandidate, Locale locale, NumberFormat numberFormat) throws ParseException
      Parses a numeric string into a Number instance using the specified Locale.
      Parameters:
      numberCandidate - the string representation of the number to parse; must not be null or empty
      locale - the Locale whose formatting rules should be used for parsing; must not be null
      numberFormat - Format to use when parsing the numberCandidate String
      Returns:
      a Number representing the parsed value
      Throws:
      ParseException - if the string cannot be parsed as a number according to the given locale
      NullPointerException - if numberCandidate or locale is null
      See Also:
    • validateCandidateString

      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.
      Parameters:
      numberCandidate - the string to validate
      Returns:
      the validated String stripped of leading and trailing whitespaces
      Throws:
      IllegalArgumentException - if the string is null or empty
      NumberFormatException - if the string contains illegal characters