Class base10Util


  • public final class base10Util
    extends Object

    The base10Util class is a final class that provides static methods for converting base 10 numbering system values in string representation to a Java's Primitive Data Type.

    Currently this class supports converting base 10 numbers values in string representation to integer int values and integer long values.

    This class can parse unsigned base 10 numbers to a supported integer signed type as if the integer type is unsigned. However, some of the values must be interprete properly to get the correct result.

    Example for interpreting signed value as unsigned value.

    It is possible to store the value of 18446744073709551615L into a long(signed) value. However, if that value is stored into a signed long integer type and if we were to interprete the value normally, we would get a -1L value. However, if the -1L value is pass to LongUtil.toStringAsUnsigned, we would get 18446744073709551615 in string format.

    The following example is to get to -1L. First, we assign a value of 9223372036854775807L to an interger long variable, multiply that variable to 2L, and add 1L to it.

         long a = 9223372036854775807L * 2L + 1L;
         System.out.println(a);
         System.out.println(LongUtil.toStringAsUnsigned(a)); 
     

    Example methods for interprete signed type as unsigned type in a decimal strings value are IntUtil.toStringAsUnsigned and LongUtil.toStringAsUnsigned.

    This class also provides tools for comparing strings pertain to the mathematical aspect. This class provide the method compareAsInt that can compare strings by their actual digits values when the strings contain valid base 10 numbers values that can be parsed to integer int values.

    Strings can also be compared using the intOrSmaller method, of which compares the strings bases on their content reference to integer int values.

    Other method compareAs**** or ****OrSmaller(**** equals name) if available, are similar to the compareAsInt or intOrSmaller method, except they are for the "name(****)" type.

    Since:
    1.000.00000.f
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int compareAsInt​(String firstNumber, String secondNumber)
      Compare two base 10 number strings as integer int values.
      static int compareAsLong​(String firstNumber, String secondNumber)
      Compare two base 10 number strings as integer long values.
      static int intOrSmaller​(String firstNumber, String secondNumber)
      Compare two strings bases on the content of the strings reference to an integer int value.
      static int longOrSmaller​(String firstNumber, String secondNumber)
      Compare two strings bases on the content of the strings reference to an integer long value.
      static int toInt​(String input)
      Parse the input string as signed base 10 digits representation into an integer int value.
      static int toIntAsUnsigned​(String input)
      Parse the input string as unsigned base 10 number representation into an integer int value as if the integer int is an unsigned type.
      static int toIntTrueError​(String input)
      Parse the input string as signed base 10 digits representation into an integer int value.
      static long toLong​(String input)
      Parse the input string as signed base 10 number representation into an integer long value.
      static long toLongAsUnsigned​(String input)
      Parse the input string as unsigned base 10 number representation into an integer long value as if the integer long is an unsigned type.
      static long toLongTrueError​(String input)
      Parse the input string as signed base 10 number representation into an integer long value.
    • Method Detail

      • toInt

        public static final int toInt​(String input)
        Parse the input string as signed base 10 digits representation into an integer int value.
        Parameters:
        input - A string to be parsed as signed base 10 number to an integer int value.
        Returns:
        An integer int value of the signed base 10 number input string.
        Throws:
        NumberFormatException - If the input string contains invalid signed base 10 digits, if the input string contains a value that is smaller than the value of Integer.MIN_VALUE( -2147483648), or if the input string contains a value that is larger than the value of Integer.MAX_VALUE( 2147483647).
        EmptyStringException - If the input string is empty.
        Since:
        1.000.00000.f
      • toIntTrueError

        public static final int toIntTrueError​(String input)
        Parse the input string as signed base 10 digits representation into an integer int value. This method throws true errors on unsuccessful parse cases. This method may take longer on unsucessful parse cases.
        Parameters:
        input - A string to be parsed as signed base 10 number to an integer int value.
        Returns:
        An integer int value of the signed base 10 number input string.
        Throws:
        NumberFormatException - If the input string contains invalid signed base 10 digits.
        NumberUnderFlowException - If the input string contains a value that is smaller than the value of Integer.MIN_VALUE( -2147483648).
        NumberOverFlowException - If the input string contains a value that is larger than the value of Integer.MAX_VALUE( 2147483647).
        EmptyStringException - If the input string is empty.
        Since:
        1.000.00000.f
      • toIntAsUnsigned

        public static final int toIntAsUnsigned​(String input)
        Parse the input string as unsigned base 10 number representation into an integer int value as if the integer int is an unsigned type. For values that need to be interpreted correctly, see the toStringAsUnsigned method of the IntUtil class.
        Parameters:
        input - A string to be parsed as unsigned base 10 number to an integer int value as if the integer int is an unsigned type.
        Returns:
        An int value that represents an unsigned integer int value of the unsigned base 10 number input string.
        Throws:
        NumberFormatException - If the input string contains invalid unsigned base 10 digits, if the input string contains a value that is beyond the capacity of the integer int data type.
        EmptyStringException - If the input string is empty.
        Since:
        1.000.00000.f
      • toLong

        public static final long toLong​(String input)
        Parse the input string as signed base 10 number representation into an integer long value.
        Parameters:
        input - A string to be parsed as signed base 10 number to an integer long value.
        Returns:
        An integer long value of the signed base 10 number input string.
        Throws:
        NumberFormatException - If the input string contains invalid signed base 10 digits, if the input string contains a value that is smaller than the value of Long.MIN_VALUE( -9223372036854775808L), or if the input string contains a value that is larger than the value of Long.MAX_VALUE( 9223372036854775807L).
        EmptyStringException - If the input string is empty.
        Since:
        1.000.00000.f
      • toLongTrueError

        public static final long toLongTrueError​(String input)
        Parse the input string as signed base 10 number representation into an integer long value. This method throws true errors on unsuccessful parse cases. This method may take longer on unsucessful parse cases.
        Parameters:
        input - A string to be parsed as signed base 10 number to an integer long value.
        Returns:
        An integer long value of the signed base 10 number input string.
        Throws:
        NumberFormatException - If the input string contains invalid base 10 digits.
        NumberUnderFlowException - If the input string contains a value that is smaller than the value of Long.MIN_VALUE( -9223372036854775808L).
        NumberOverFlowException - If the input string contains a value that is larger than the value of Long.MAX_VALUE( 9223372036854775807L).
        EmptyStringException - If the input string is empty.
        Since:
        1.000.00000.f
      • toLongAsUnsigned

        public static final long toLongAsUnsigned​(String input)
        Parse the input string as unsigned base 10 number representation into an integer long value as if the integer long is an unsigned type. For values that need to be interpreted correctly, see the toStringAsUnsigned method of the LongUtil class.
        Parameters:
        input - A string to be parsed as unsigned base 10 number to an integer long value as if the integer long is an unsigned type.
        Returns:
        An integer long value represent the unsigned integer long value of the unsigned base 10 number input string.
        Throws:
        NumberFormatException - If the input string contains invalid unsigned base 10 digits, or if the {code input} string contains a value that is beyond the capacity of the long data type.
        EmptyStringException - If the input string is empty.
        Since:
        1.000.00000.f
      • compareAsInt

        public static int compareAsInt​(String firstNumber,
                                       String secondNumber)
        Compare two base 10 number strings as integer int values. This method treat integer int values as signed integer type. This method does not throw errors. However, the toInt method uses by this method throws errors.
        Parameters:
        firstNumber - A string to be compared to the string secondNumber.
        secondNumber - A string to be compared to the string firstNumber.
        Returns:
        An integer int value of 1 if the firstNumber string is larger than the secondNumber string, 0 if they are both equal, or -1 if the firstNumber string is smaller than the secondNumber string.
        Since:
        1.000.00000.f
        See Also:
        toInt(String)
      • intOrSmaller

        public static int intOrSmaller​(String firstNumber,
                                       String secondNumber)

        Compare two strings bases on the content of the strings reference to an integer int value. If the strings are valid base 10 number strings that can be parse as integer int values then they will be compared bases on their integer int values. Otherwise, the strings will be compared bases on the priority ranking order below. This method treat integer int values as signed integer type.

        Priority order ranking: (lo - hi)
        0 - Invalid number strings
        1 - Underflow
        2 - Overflow
        3 - Empty strings
        4 - Valid int values

        Parameters:
        firstNumber - A string to be compared to the string secondNumber.
        secondNumber - A string to be compared to the string firstNumber.
        Returns:
        An integer int value of 1 if the firstNumber string is larger than the secondNumber string, 0 if they are both equal, or -1 if the firstNumber string is smaller than the secondNumber string.
        Since:
        1.000.00000.f
      • compareAsLong

        public static int compareAsLong​(String firstNumber,
                                        String secondNumber)
        Compare two base 10 number strings as integer long values. This method treat integer long values as signed integer type. This method does not throw errors. However, the toLong method uses by this method throws errors.
        Parameters:
        firstNumber - A string to be compared to the string secondNumber.
        secondNumber - A string to be compared to the string firstNumber.
        Returns:
        An integer int value of 1 if the firstNumber string is larger than the secondNumber string, 0 if they are both equal, or -1 if the firstNumber string is smaller than the secondNumber string.
        Since:
        1.000.00000.f
        See Also:
        toLong(String)
      • longOrSmaller

        public static int longOrSmaller​(String firstNumber,
                                        String secondNumber)

        Compare two strings bases on the content of the strings reference to an integer long value. If the strings are valid base 10 number strings that can be parse as integer long values then they will be compared bases on their integer long values. Otherwise, the strings will be compared bases on the priority ranking order below. This method treat integer long values as signed integer type.

        Priority order ranking: (lo - hi)
        0 - Invalid number strings
        1 - Underflow
        2 - Overflow
        3 - Empty strings
        4 - Valid long values

        Parameters:
        firstNumber - A string to be compared to the string secondNumber.
        secondNumber - A string to be compared to the string firstNumber.
        Returns:
        An integer int value of 1 if the firstNumber string is larger than the secondNumber string, 0 if they are both equal, or -1 if the firstNumber string is smaller than the secondNumber string.
        Since:
        1.000.00000.f