Class Integers


  • public class Integers
    extends java.lang.Object
    Utilities for manipulating integer objects.
    Author:
    Garret Wilson
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean equals​(java.lang.Object integerObject, int integer)
      Compares an integer object with the value of a literal integer.
      static int parseIntDefault​(java.lang.String string, int defaultValue)
      Parses a string and returns its integer value, or a default value if the string does not contain an integer.
      static int parseIntValue​(java.lang.String string)
      Parses an integer value in the given string.
      static int parseNumberTextValue​(java.lang.String numberText)
      Parses the given text string and returns a number without throwing an exception if the string is invalid.
      static int parseOrdinal​(java.lang.String ordinal)
      Parses the given string and returns an ordinal value.
      static int parseOrdinalValue​(java.lang.String ordinal)
      Parses the given string and returns an ordinal value without throwing an exception if the string is invalid.
      static java.lang.String toHexString​(int value, int length)
      Converts an integer into a hex string with the specified number of digits.
      static java.lang.String toHexString​(int value, int length, Case hexCase)
      Converts an integer into a hex string with the specified number of digits.
      static int[] toIntArray​(java.lang.Integer... integers)
      Creates an array of primitive values from the given array of objects.
      static java.lang.Integer[] toIntegerArray​(int... ints)
      Creates an array of objects from the given array of primitive values.
      static java.lang.String toString​(int value, int radix, int length)
      Returns a string representation of a given integer in the given radix, modified to match the the given length.
      • Methods inherited from class java.lang.Object

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

      • equals

        public static boolean equals​(java.lang.Object integerObject,
                                     int integer)
        Compares an integer object with the value of a literal integer.
        Parameters:
        integerObject - The object that supposedly holds an integer.
        integer - The literal integer value to compare to the integer object.
        Returns:
        true if the object is an Integer, the value of which equals that of integer.
      • parseIntValue

        public static int parseIntValue​(java.lang.String string)
                                 throws java.lang.NumberFormatException
        Parses an integer value in the given string. If the string contains a decimal point followed by zeros (a double representation of an integer value), this method correctly returns the integer value without throwing an exception; this is the one difference between this method and Integer.parseInt().
        Parameters:
        string - The string containing the integer value, which may or may not have a decimal point, but if it does the decimal should be followed by zeros.
        Returns:
        The integer value represented by the string.
        Throws:
        java.lang.NumberFormatException - Thrown if the string does not contain a parsable integer.
        See Also:
        Integer.parseInt(String)
      • parseIntDefault

        public static int parseIntDefault​(java.lang.String string,
                                          int defaultValue)
        Parses a string and returns its integer value, or a default value if the string does not contain an integer.
        Parameters:
        string - The string which might contain an integer value.
        defaultValue - The default value if the string does not contain a valid integer.
        Returns:
        The integer value of the string, or the default value if the string does not contain a valid integer or if the string is null.
      • parseOrdinal

        public static int parseOrdinal​(java.lang.String ordinal)
                                throws java.lang.NumberFormatException
        Parses the given string and returns an ordinal value.
        Parameters:
        ordinal - The string containing an ordinal value, such as "first" or "second".
        Returns:
        The 1-based order of the string.
        Throws:
        java.lang.NumberFormatException - Thrown if the given string does not contain a valid ordinal value.
      • parseOrdinalValue

        public static int parseOrdinalValue​(java.lang.String ordinal)
        Parses the given string and returns an ordinal value without throwing an exception if the string is invalid.
        Parameters:
        ordinal - The string containing an ordinal value, such as "first" or "second".
        Returns:
        The 1-based order of the string, or -1 if the string does not contain a valid ordinal value.
      • parseNumberTextValue

        public static int parseNumberTextValue​(java.lang.String numberText)
        Parses the given text string and returns a number without throwing an exception if the string is invalid.
        Parameters:
        numberText - The string containing a number value, such as "one" or "two".
        Returns:
        The number contained of the string, or -1 if the string does not contain a valid text number.
      • toString

        public static java.lang.String toString​(int value,
                                                int radix,
                                                int length)
        Returns a string representation of a given integer in the given radix, modified to match the the given length.
        Parameters:
        value - An integer to be converted to a string.
        radix - The radix to use in the string representation.
        length - The number of digits the returned string should have.
        Returns:
        A string representation of the argument in the specified radix.
        See Also:
        Integer.toString(int, int)
      • toHexString

        public static java.lang.String toHexString​(int value,
                                                   int length)
        Converts an integer into a hex string with the specified number of digits.
        Parameters:
        value - The value to convert.
        length - The number of digits the returned string should have.
        Returns:
        Lowercase hex version of the given value with the correct number of digits, using zeros to pad the left of the string to the correct length.
        See Also:
        Integer.toHexString(int)
      • toHexString

        public static java.lang.String toHexString​(int value,
                                                   int length,
                                                   Case hexCase)
        Converts an integer into a hex string with the specified number of digits.
        Parameters:
        value - The value to convert.
        length - The number of digits the returned string should have.
        hexCase - Whether the hex characters should be lowercase or uppercase.
        Returns:
        The hex version of the given value with the correct number of digits, using zeros to pad the left of the string to the correct length.
        See Also:
        Integer.toHexString(int)
      • toIntArray

        public static int[] toIntArray​(java.lang.Integer... integers)
        Creates an array of primitive values from the given array of objects.
        Parameters:
        integers - The objects to place in an primitive value array.
        Returns:
        An array of primitive values representing the given objects.
      • toIntegerArray

        public static java.lang.Integer[] toIntegerArray​(int... ints)
        Creates an array of objects from the given array of primitive values.
        Parameters:
        ints - The primitive values to place in an object array.
        Returns:
        An array of object values representing the given primitive values.