Class NumberStringUtil


  • public final class NumberStringUtil
    extends Object

    The NumberStringUtil class is a final class that provides methods for working with numbers in String format.

    This class provide methods for validating strings to see whether they are valid number, validating chars to see whether if they are valid digits, and comparing strings of number.

    This class has the assume**** methods, of which operate on strings with the assumption that the strings are valid number values. For comparing string of numbers, the assume**** methods are very fast methods that can be used in circumstances where it is sure that the strings are valid number values.

    Under the right circumstance, the assume**** methods are very powerful and fast methods. However, under that use case, the strings must be valid number strings for the methods to produce the correct result. Thus, the assume**** methods of this class are good for circumstances where the input data have been validated before usage and there isn't a need for revalidating the data.

    For an example of one of the assume**** methods of this class, The assumeCompare methods of this class besides being able to compare numbers it can also compare dates with fix format. Format like YYYY-MM-DD, YYYY/MM/DD, or similar can be compared. However, for correct results, the month, day, or year value must consistenly be in the same amount of digits(i.e. year 500 and 2000 have to be written as 0500 and 1000, or month 1 and 12 have to be written as 01 and 12). Also, year always have to be first, then month, then finally day.

    Example usage of the assumeCompare method.

         String a = "1208925819614629174706176";
         String b = "1208925819614629174706177";
         System.out.println(NumberStringUtil.assumeCompare(a,b));
    
         String d1 = "2018-01-23";
         String d2 = "2018-12-24";
         System.out.println(NumberStringUtil.assumeCompare(d1,d2));
    
         String d3 = "2018-12-24";
         String d4 = "2018-12-03";
         System.out.println(NumberStringUtil.assumeCompare(d3,d4));
     

    For comparing strings of numbers, besides the assumeCompare or the assumeCompareAllBase method, this class also provide the compare and the compareAsBase method for comparing strings of number where there is a need to validate the strings when comparing.

    For speed wise, the assumeCompare**** and compare**** methods of this class excel in circumstance where the strings do not have to be compared up until the last digit to know which string is larger or smaller.

    Another advantage of the compare**** and assumeCompare**** methods is that, there isn't a upper bound on the length of the input strings. That is as long as memory allow.

    Since:
    1.0.0.f
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int assumeCompare​(String firstString, String secondString)
      Compare two strings with the assumption that both strings are valid decimal integers.
      static int assumeCompareAllBase​(String firstString, String secondString)
      Compare two strings with the assumption that both strings are valid integers of any radix between 2 and 36.
      static boolean assumeIsEven​(String input)
      Check if a string an even number with the assumption that the string is a valid decimal integer in string representation.
      static boolean assumeIsOdd​(String input)
      Check if a string a odd number with the assumption that the string is a valid decimal integer in string representation.
      static int compare​(String firstString, String secondString)
      Compare two strings as decimal integers.
      static int compareAsBase​(String firstString, String secondString, int base)
      Compare two strings as integers of a define radix.
      static boolean isBase​(String input, int base)
      Check if a string is a valid signed integer of the defined radix.
      static boolean isBaseDigit​(char input, int base)
      Check if a char is a valid digit of a defined radix.
      static boolean isBinary​(String input)
      Check if a string is a valid signed binary integer.
      static boolean isBinaryDigit​(char input)
      Check if a char is a valid binary digit.
      static boolean isDigit​(char input)
      Check if a char is a valid decimal digit.
      static boolean isHex​(String input)
      Check if a string is a valid signed hexadecimal integer.
      static boolean isHexDigit​(char input)
      Check if a char is a valid hexadecimal digit.
      static boolean isInteger​(String input)
      Check if a string is a valid signed decimal integer.
      static boolean isOctal​(String input)
      Check if a string is a valid signed octal integer.
      static boolean isOctalDigit​(char input)
      Check if a char is a valid octal digit.
      static boolean isUnsignedBase​(String input, int base)
      Check if a string is a valid unsigned integer of the defined radix.
      static boolean isUnsignedBinary​(String input)
      Check if a string is a valid unsigned binary integer.
      static boolean isUnsignedHex​(String input)
      Check if a string is a valid unsigned hexadecimal integer.
      static boolean isUnsignedInteger​(String input)
      Check if a string is a valid unsigned decimal integer.
      static boolean isUnsignedOctal​(String input)
      Check if a string is a valid unsigned octal integer.
    • Method Detail

      • assumeCompare

        public static int assumeCompare​(String firstString,
                                        String secondString)

        Compare two strings with the assumption that both strings are valid decimal integers.

        This method evaluates both, negative and postive values. This method may produce unwanted result if the previous is not met or if the number formatting is incorrect.

        This method considers empty strings as smallest. This method disregards leading zeroes. This method considers a string with only a '-' or a '+' sign as 0.

        Parameters:
        firstString - A string to be compared to the secondString string.
        secondString - A string to be compared to the firstString string.
        Returns:
        An integer int of 1 if the firstString is larger than the secondString, -1 if the firstString is smaller than the secondString, or 0 if both strings are equal.
        Since:
        1.0.0.f
      • assumeCompareAllBase

        public static int assumeCompareAllBase​(String firstString,
                                               String secondString)

        Compare two strings with the assumption that both strings are valid integers of any radix between 2 and 36.

        This method evaluates both, negative and positive values. This method will work, if both strings are integers of the same radix and the radix is from 2 to 36. This method may produce unwanted result if the previous are not met or if the formatting is incorrect.

        Digits of radix larger than 10 are 'a' to 'z' with 'a' being 10, and 'z' being 35. A is equal to a, Z is equal to z, and so forth.

        This method considers empty strings as smallest. This method disregards leading zeroes. This method considers a string with only a '-' or a '+' sign as 0.

        Parameters:
        firstString - A string to be compared to the secondString string.
        secondString - A string to be compared to the firstString string.
        Returns:
        An integer int of 1 if the firstString is larger than the secondString, -1 if the firstString is smaller than the secondString, or 0 if both strings are equal.
        Since:
        1.0.0.f
      • assumeIsEven

        public static boolean assumeIsEven​(String input)
        Check if a string an even number with the assumption that the string is a valid decimal integer in string representation.
        Parameters:
        input - A string.
        Returns:
        A boolean value of true if the last character in the string is an even value, or false for everything else.
        Since:
        1.0.0.f
      • assumeIsOdd

        public static boolean assumeIsOdd​(String input)
        Check if a string a odd number with the assumption that the string is a valid decimal integer in string representation.
        Parameters:
        input - A string.
        Returns:
        A boolean value of true if the last character in the string is an odd value, or false for everything else.
        Since:
        1.0.0.f
      • compare

        public static int compare​(String firstString,
                                  String secondString)

        Compare two strings as decimal integers. This method evaluates both, negative and postive values. This method throw errors if either one of the strings is an invalid decimal integer. Nonetheless, the strings can be very large in length. This method disregard leading zeroes.

        Parameters:
        firstString - A string to be compared to the secondString string.
        secondString - A string to be compared to the firstString string.
        Returns:
        An integer int of 1 if the firstString is larger than the secondString, -1 if the firstString is smaller than the secondString, or 0 if both strings are equal.
        Throws:
        NumberFormatException - If the either one of the strings is not a valid decimal integer. A single '+' or '-' sign without any digit is an invalid decimal integer value.
        EmptyStringException - If either one of the strings is empty.
        Since:
        1.0.0.f
      • compareAsBase

        public static int compareAsBase​(String firstString,
                                        String secondString,
                                        int base)

        Compare two strings as integers of a define radix. This method evaluates both, negative and postive values. This method throw errors if either one of the strings is an invalid integer of the defined radix. The radix of the strings can only be in between of 2 to 36. There isn't a maximum length for the strings. Nonetheless, the strings can't be empty.

        Parameters:
        firstString - A string to be compared to the secondString string.
        secondString - A string to be compared to the firstString string.
        base - An integer int value that defines the number radix for both strings.
        Returns:
        An integer int of 1 if the firstString is larger than the secondString, -1 if the firstString is smaller than the secondString, or 0 if both strings are equal.
        Throws:
        NumberFormatException - If the either one of the strings is not a valid integer number. A single '+' or '-' sign without any digit is an invalid integer value.
        EmptyStringException - If either one of the strings is empty.
        UnsupportedOperationException - If the input base int value is a value that is smaller than 2 or larger than 36.
        Since:
        1.0.0.f
      • isBase

        public static boolean isBase​(String input,
                                     int base)
        Check if a string is a valid signed integer of the defined radix.
        Parameters:
        input - A string.
        base - An integer int value that defines the number radix.
        Returns:
        A boolean value of true if the input string is a valid signed integer of the radix that defined by the base int, or otherwise, false.
        Throws:
        UnsupportedOperationException - If the input base int value is a value that is smaller than 2 or larger than 36.
        Since:
        1.0.0.f
      • isBaseDigit

        public static boolean isBaseDigit​(char input,
                                          int base)
        Check if a char is a valid digit of a defined radix.
        Parameters:
        input - A char.
        base - An integer int value that defines the number radix.
        Returns:
        A boolean value of true if the input char is a valid digit of a radix that defined by the base int, or otherwise, false.
        Throws:
        UnsupportedOperationException - If the input base int value is a value that is smaller than 2 or larger than 36.
        Since:
        1.0.0.f
      • isBinary

        public static boolean isBinary​(String input)
        Check if a string is a valid signed binary integer.
        Parameters:
        input - A string.
        Returns:
        A boolean value of true if the input string is a valid signed binary integer, or otherwise, false.
        Since:
        1.0.0.f
      • isBinaryDigit

        public static boolean isBinaryDigit​(char input)
        Check if a char is a valid binary digit.
        Parameters:
        input - A char.
        Returns:
        A boolean value of true if the input char is a valid binary digit, or otherwise, false.
        Since:
        1.0.0.f
      • isDigit

        public static boolean isDigit​(char input)
        Check if a char is a valid decimal digit.
        Parameters:
        input - A char.
        Returns:
        A boolean value of true if the input char is a valid decimal digit, or otherwise, false.
        Since:
        1.0.0.f
      • isHex

        public static boolean isHex​(String input)
        Check if a string is a valid signed hexadecimal integer.
        Parameters:
        input - A string.
        Returns:
        A boolean value of true if the input string is a valid signed hexadecimal integer, or otherwise, false.
        Since:
        1.0.0.f
      • isHexDigit

        public static boolean isHexDigit​(char input)
        Check if a char is a valid hexadecimal digit.
        Parameters:
        input - A char.
        Returns:
        A boolean value of true if the input char is a valid hexadecimal digit, or otherwise, false.
        Since:
        1.0.0.f
      • isInteger

        public static boolean isInteger​(String input)
        Check if a string is a valid signed decimal integer.
        Parameters:
        input - A string.
        Returns:
        A boolean value of true if the input string is a valid signed decimal integer, or otherwise, false.
        Since:
        1.0.0.f
      • isOctal

        public static boolean isOctal​(String input)
        Check if a string is a valid signed octal integer.
        Parameters:
        input - A string.
        Returns:
        A boolean value of true if the input string is a valid signed octal integer, or otherwise, false.
        Since:
        1.0.0.f
      • isOctalDigit

        public static boolean isOctalDigit​(char input)
        Check if a char is a valid octal digit.
        Parameters:
        input - A char.
        Returns:
        A boolean value of true if the input char is a valid octal digit, or otherwise, false.
        Since:
        1.0.0.f
      • isUnsignedBase

        public static boolean isUnsignedBase​(String input,
                                             int base)
        Check if a string is a valid unsigned integer of the defined radix.
        Parameters:
        input - A string.
        base - An integer int value that defines the number radix.
        Returns:
        A boolean value of true if the input string is a valid unsigned integer of the radix that defined by the base int, or otherwise, false.
        Throws:
        UnsupportedOperationException - If the input base int value is a value that is smaller than 2 or larger than 36.
        Since:
        1.0.0.f
      • isUnsignedBinary

        public static boolean isUnsignedBinary​(String input)
        Check if a string is a valid unsigned binary integer.
        Parameters:
        input - A string.
        Returns:
        A boolean value of true if the input string is a valid unsigned binary integer, or otherwise, false.
        Since:
        1.0.0.f
      • isUnsignedHex

        public static boolean isUnsignedHex​(String input)
        Check if a string is a valid unsigned hexadecimal integer.
        Parameters:
        input - A string.
        Returns:
        A boolean value of true if the input string is a valid unsigned hexadecimal integer, or otherwise, false.
        Since:
        1.0.0.f
      • isUnsignedInteger

        public static boolean isUnsignedInteger​(String input)
        Check if a string is a valid unsigned decimal integer.
        Parameters:
        input - A string.
        Returns:
        A boolean value of true if the input string is a valid unsigned decimal integer, or otherwise, false.
        Since:
        1.0.0.f
      • isUnsignedOctal

        public static boolean isUnsignedOctal​(String input)
        Check if a string is a valid unsigned octal integer.
        Parameters:
        input - A string.
        Returns:
        A boolean value of true if the input string is a valid unsigned octal integer, or otherwise, false.
        Since:
        1.0.0.f