Class Integers

java.lang.Object
com.globalmentor.java.Integers

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

    Modifier and Type
    Method
    Description
    static boolean
    equals(Object integerObject, int integer)
    Compares an integer object with the value of a literal integer.
    static int
    parseIntDefault(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
    Parses an integer value in the given string.
    static int
    Parses the given text string and returns a number without throwing an exception if the string is invalid.
    static int
    Parses the given string and returns an ordinal value.
    static int
    Parses the given string and returns an ordinal value without throwing an exception if the string is invalid.
    static String
    toHexString(int value, int length)
    Converts an integer into a hex string with the specified number of digits.
    static String
    toHexString(int value, int length, Case hexCase)
    Converts an integer into a hex string with the specified number of digits.
    static int[]
    toIntArray(Integer... integers)
    Creates an array of primitive values from the given array of objects.
    static Integer[]
    toIntegerArray(int... ints)
    Creates an array of objects from the given array of primitive values.
    static 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 Details

    • equals

      public static boolean equals(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(String string) throws 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:
      NumberFormatException - Thrown if the string does not contain a parsable integer.
      See Also:
    • parseIntDefault

      public static int parseIntDefault(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(String ordinal) throws 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:
      NumberFormatException - Thrown if the given string does not contain a valid ordinal value.
    • parseOrdinalValue

      public static int parseOrdinalValue(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(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 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:
    • toHexString

      public static 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:
    • toHexString

      public static 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:
    • toIntArray

      public static int[] toIntArray(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 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.