Class ParseUtil


  • public final class ParseUtil
    extends java.lang.Object
    Helper functionality for parsing.

    This class is fairly optimized. Because exceptions frequently occur in parsing (e.g., when trying to parse a text as float), this implementation avoids allocating exceptions and stack traces in these cases, and uses static instances instead. By setting the logging level for the ParseUtil class to FINE, the backtraces can still be obtained for development. But usually you will want to catch this exception and instead throw your own with additional context information of the value that failed to parse.

    Since:
    0.7.5
    Author:
    Erich Schubert
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.NumberFormatException EMPTY_STRING
      Preallocated exceptions.
      static java.lang.NumberFormatException EXPONENT_OVERFLOW
      Preallocated exceptions.
      private static int INFINITY_LENGTH
      Length of pattern
      private static char[] INFINITY_PATTERN
      Infinity pattern, with any capitalization
      static java.lang.NumberFormatException INVALID_EXPONENT
      Preallocated exceptions.
      private static Logging LOG
      Logging helper.
      static java.lang.NumberFormatException NOT_A_NUMBER
      Preallocated exceptions.
      static java.lang.NumberFormatException PRECISION_OVERFLOW
      Preallocated exceptions.
      static java.lang.NumberFormatException TRAILING_CHARACTERS
      Preallocated exceptions.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ParseUtil()
      Private constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static boolean matchInf​(byte[] str, byte firstchar, int start, int end)
      Match "inf", "infinity" in a number of different capitalizations.
      private static boolean matchInf​(java.lang.CharSequence str, char firstchar, int start, int end)
      Match "inf", "infinity" in a number of different capitalizations.
      private static boolean matchNaN​(byte[] str, byte firstchar, int start, int end)
      Match "NaN" in a number of different capitalizations.
      private static boolean matchNaN​(java.lang.CharSequence str, char firstchar, int start, int end)
      Match "NaN" in a number of different capitalizations.
      static double parseDouble​(byte[] str, int start, int end)
      Parse a double from a character sequence.
      static double parseDouble​(java.lang.CharSequence str)
      Parse a double from a character sequence.
      static double parseDouble​(java.lang.CharSequence str, int start, int end)
      Parse a double from a character sequence.
      static int parseIntBase10​(java.lang.CharSequence str)
      Parse an integer from a character sequence.
      static int parseIntBase10​(java.lang.CharSequence str, int start, int end)
      Parse an integer from a character sequence.
      static long parseLongBase10​(java.lang.CharSequence str)
      Parse a long integer from a character sequence.
      static long parseLongBase10​(java.lang.CharSequence str, int start, int end)
      Parse a long integer from a character sequence.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOG

        private static final Logging LOG
        Logging helper.
      • EMPTY_STRING

        public static final java.lang.NumberFormatException EMPTY_STRING
        Preallocated exceptions.
      • EXPONENT_OVERFLOW

        public static final java.lang.NumberFormatException EXPONENT_OVERFLOW
        Preallocated exceptions.
      • INVALID_EXPONENT

        public static final java.lang.NumberFormatException INVALID_EXPONENT
        Preallocated exceptions.
      • TRAILING_CHARACTERS

        public static final java.lang.NumberFormatException TRAILING_CHARACTERS
        Preallocated exceptions.
      • PRECISION_OVERFLOW

        public static final java.lang.NumberFormatException PRECISION_OVERFLOW
        Preallocated exceptions.
      • NOT_A_NUMBER

        public static final java.lang.NumberFormatException NOT_A_NUMBER
        Preallocated exceptions.
      • INFINITY_PATTERN

        private static final char[] INFINITY_PATTERN
        Infinity pattern, with any capitalization
      • INFINITY_LENGTH

        private static final int INFINITY_LENGTH
        Length of pattern
    • Constructor Detail

      • ParseUtil

        private ParseUtil()
        Private constructor. Static methods only.
    • Method Detail

      • parseDouble

        public static double parseDouble​(byte[] str,
                                         int start,
                                         int end)
        Parse a double from a character sequence.

        In contrast to Javas Double.parseDouble(java.lang.String), this will not create an object and thus is expected to put less load on the garbage collector. It will accept some more spellings of NaN and infinity, thus removing the need for checking for these independently.

        Parameters:
        str - String
        start - Begin
        end - End
        Returns:
        Double value
      • parseDouble

        public static double parseDouble​(java.lang.CharSequence str)
        Parse a double from a character sequence.

        In contrast to Javas Double.parseDouble(java.lang.String), this will not create an object and thus is expected to put less load on the garbage collector. It will accept some more spellings of NaN and infinity, thus removing the need for checking for these independently.

        Parameters:
        str - String
        Returns:
        Double value
      • parseDouble

        public static double parseDouble​(java.lang.CharSequence str,
                                         int start,
                                         int end)
        Parse a double from a character sequence.

        In contrast to Javas Double.parseDouble(java.lang.String), this will not create an object and thus is expected to put less load on the garbage collector. It will accept some more spellings of NaN and infinity, thus removing the need for checking for these independently.

        Parameters:
        str - String
        start - Begin
        end - End
        Returns:
        Double value
      • parseLongBase10

        public static long parseLongBase10​(java.lang.CharSequence str)
        Parse a long integer from a character sequence.
        Parameters:
        str - String
        Returns:
        Long value
      • parseLongBase10

        public static long parseLongBase10​(java.lang.CharSequence str,
                                           int start,
                                           int end)
        Parse a long integer from a character sequence.
        Parameters:
        str - String
        start - Begin
        end - End
        Returns:
        Long value
      • parseIntBase10

        public static int parseIntBase10​(java.lang.CharSequence str)
        Parse an integer from a character sequence.
        Parameters:
        str - String
        Returns:
        Int value
      • parseIntBase10

        public static int parseIntBase10​(java.lang.CharSequence str,
                                         int start,
                                         int end)
        Parse an integer from a character sequence.
        Parameters:
        str - String
        start - Begin
        end - End
        Returns:
        int value
      • matchInf

        private static boolean matchInf​(byte[] str,
                                        byte firstchar,
                                        int start,
                                        int end)
        Match "inf", "infinity" in a number of different capitalizations.
        Parameters:
        str - String to match
        firstchar - First character
        start - Interval begin
        end - Interval end
        Returns:
        true when infinity was recognized.
      • matchInf

        private static boolean matchInf​(java.lang.CharSequence str,
                                        char firstchar,
                                        int start,
                                        int end)
        Match "inf", "infinity" in a number of different capitalizations.
        Parameters:
        str - String to match
        firstchar - First character
        start - Interval begin
        end - Interval end
        Returns:
        true when infinity was recognized.
      • matchNaN

        private static boolean matchNaN​(byte[] str,
                                        byte firstchar,
                                        int start,
                                        int end)
        Match "NaN" in a number of different capitalizations.
        Parameters:
        str - String to match
        firstchar - First character
        start - Interval begin
        end - Interval end
        Returns:
        true when NaN was recognized.
      • matchNaN

        private static boolean matchNaN​(java.lang.CharSequence str,
                                        char firstchar,
                                        int start,
                                        int end)
        Match "NaN" in a number of different capitalizations.
        Parameters:
        str - String to match
        firstchar - First character
        start - Interval begin
        end - Interval end
        Returns:
        true when NaN was recognized.