Class NumberMethods

java.lang.Object
org.klojang.convert.NumberMethods

public final class NumberMethods extends Object
Methods for parsing, inspecting and converting Number instances.

NB For mathematical operations, see MathMethods.

Author:
Ayco Holleman
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final BigDecimal
    Double.MAX_VALUE converted to a BigDecimal.
    static final BigDecimal
    Long.MAX_VALUE converted to a BigDecimal.
    static final BigInteger
    Long.MAX_VALUE converted to a BigInteger.
    static final BigDecimal
    Double.MIN_VALUE converted to a BigDecimal.
    static final BigDecimal
    Long.MIN_VALUE converted to a BigDecimal.
    static final BigInteger
    Long.MIN_VALUE converted to a BigInteger.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends Number, R extends Number>
    R
    convert(T number, Class<R> targetType)
    Safely converts a number of an unspecified type to a number of a definite type.
    static <T extends Number>
    boolean
    fitsInto(Number number, Class<T> targetType)
    Returns true if the specified number can be converted to the specified target type without loss of information.
    static <T extends Number>
    boolean
    fitsInto(String s, Class<T> targetType)
    Tests whether the specified string can be parsed into a Number of the specified type.
    static boolean
    Returns true if the specified string can be parsed into a BigDecimal.
    static boolean
    Returns true if the specified string can be parsed into a BigInteger without loss of information.
    static boolean
    Returns true if the specified string can be parsed into a byte without loss of information.
    static boolean
    Returns true if the specified string can be parsed into a double without loss of information.
    static boolean
    Returns true if the specified string can be parsed into a float without loss of information.
    static boolean
    Returns true if the specified string can be parsed into an int without loss of information.
    static boolean
    isIntegral(Class<?> type)
    Returns true if the specified class is one of Byte, Short, Integer, Long, BigInteger.
    static boolean
    Returns true if the specified number is an integral number.
    static boolean
    Returns true if the specified string can be parsed into a long without loss of information.
    static boolean
    isRound(double d)
    Determines whether the specified double's fractional part is zero or absent.
    static boolean
    isRound(float f)
    Determines whether the specified float's fractional part is zero or absent.
    static boolean
    Determines whether the specified BigDecimal's fractional part is zero or absent.
    static boolean
    Returns true if the specified string can be parsed into a short without loss of information.
    static boolean
    isWrapper(Class<?> numberType)
    Returns true if the specified class is one of the standard primitive number wrappers: Byte, Short, Integer, Long, Float, Double.
    static boolean
    isWrapper(Number number)
    Returns true if the specified number belongs to one of the primitive number wrappers.
    static <T extends Number>
    T
    parse(String s, Class<T> targetType)
    Parses the specified string into a number of the specified type.
    static BigDecimal
    Parses the specified string into a BigInteger.
    static BigInteger
    Parses the specified string into a BigInteger.
    static byte
    Parses the specified string into a byte.
    static double
    Parses the specified string into a double.
    static float
    Parses the specified string into a float.
    static int
    Parses the specified string into a int.
    static long
    Parses the specified string into a long.
    static short
    Parses the specified string into a short.
    static BigDecimal
    Converts a Number of an unspecified type to a BigDecimal.
    Returns an empty Optional if the specified string cannot be parsed into BigDecimal, else an Optional containing the BigDecimal value parsed out of the string.
    Returns an empty Optional if the specified string cannot be parsed into BigInteger, else an Optional containing the BigInteger value parsed out of the string.
    Returns an empty OptionalInt if the specified string cannot be parsed into an 8-bit integer, else an OptionalInt containing the byte value parsed out of the string.
    Returns an empty OptionalDouble if the specified string cannot be parsed into a double value, else an OptionalDouble containing the double value parsed out of the string.
    Returns an empty OptionalDouble if the specified string cannot be parsed into a regular, finite float value, else an OptionalDouble containing the float value parsed out of the string.
    Returns an empty OptionalInt if the specified string cannot be parsed into a 32-bit integer, else an OptionalInt containing the int value parsed out of the string.
    Returns an empty OptionalLong if the specified string cannot be parsed into a 64-bit integer, else an OptionalLong containing the long value parsed out of the string.
    Returns an empty OptionalInt if the specified string cannot be parsed into a 16-bit integer, else an OptionalInt containing the short value parsed out of the string.

    Methods inherited from class java.lang.Object

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

    • MIN_DOUBLE_BD

      public static final BigDecimal MIN_DOUBLE_BD
      Double.MIN_VALUE converted to a BigDecimal.
    • MAX_DOUBLE_BD

      public static final BigDecimal MAX_DOUBLE_BD
      Double.MAX_VALUE converted to a BigDecimal.
    • MIN_LONG_BD

      public static final BigDecimal MIN_LONG_BD
      Long.MIN_VALUE converted to a BigDecimal.
    • MAX_LONG_BD

      public static final BigDecimal MAX_LONG_BD
      Long.MAX_VALUE converted to a BigDecimal.
    • MIN_LONG_BI

      public static final BigInteger MIN_LONG_BI
      Long.MIN_VALUE converted to a BigInteger.
    • MAX_LONG_BI

      public static final BigInteger MAX_LONG_BI
      Long.MAX_VALUE converted to a BigInteger.
  • Method Details

    • isWrapper

      public static boolean isWrapper(Class<?> numberType)
      Returns true if the specified class is one of the standard primitive number wrappers: Byte, Short, Integer, Long, Float, Double.
      Parameters:
      numberType - the class to test
      Returns:
      whether the class is a primitive number wrapper
      See Also:
    • isWrapper

      public static boolean isWrapper(Number number)
      Returns true if the specified number belongs to one of the primitive number wrappers.
      Parameters:
      number - the number to test
      Returns:
      whether the specified number belongs to one of the primitive number wrappers
      See Also:
    • isIntegral

      public static boolean isIntegral(Class<?> type)
      Returns true if the specified class is one of Byte, Short, Integer, Long, BigInteger.
      Parameters:
      type - the class to test
      Returns:
      whether the class is an integral number type
    • isIntegral

      public static boolean isIntegral(Number number)
      Returns true if the specified number is an integral number.
      Parameters:
      number - the number to test
      Returns:
      whether the specified number is an integral number
      See Also:
    • parseInt

      public static int parseInt(String s) throws TypeConversionException
      Parses the specified string into a int. If the string does not represent a number, or if it cannot be parsed into an int without loss of information, a TypeConversionException is thrown.
      Parameters:
      s - the string to be parsed
      Returns:
      the int value represented by the string
      Throws:
      TypeConversionException - if the string does not represent or number, or if conversion would lead to loss of information
    • isInt

      public static boolean isInt(String s)
      Returns true if the specified string can be parsed into an int without loss of information. The argument is allowed to be null, in which case the return value will be false.
      Parameters:
      s - the string to be parsed
      Returns:
      whether the specified string can be parsed into an int without causing integer overflow
    • toInt

      public static OptionalInt toInt(String s)
      Returns an empty OptionalInt if the specified string cannot be parsed into a 32-bit integer, else an OptionalInt containing the int value parsed out of the string.
      Parameters:
      s - the string to be parsed
      Returns:
      an OptionalInt containing the int value parsed out of the string
    • parseLong

      public static long parseLong(String s) throws TypeConversionException
      Parses the specified string into a long. If the string does not represent a number, or if it cannot be parsed into a long without loss of information, a TypeConversionException is thrown.
      Parameters:
      s - the string to be parsed
      Returns:
      the long value represented by the string
      Throws:
      TypeConversionException - if the string does not represent or number, or if conversion would lead to loss of information
    • isLong

      public static boolean isLong(String s)
      Returns true if the specified string can be parsed into a long without loss of information. The argument is allowed to be null, in which case the return value will be false.
      Parameters:
      s - the string to be parsed
      Returns:
      whether the specified string can be parsed into a long without causing integer overflow
    • toLong

      public static OptionalLong toLong(String s)
      Returns an empty OptionalLong if the specified string cannot be parsed into a 64-bit integer, else an OptionalLong containing the long value parsed out of the string.
      Parameters:
      s - the string to be parsed
      Returns:
      an OptionalLong containing the long value parsed out of the string
    • parseDouble

      public static double parseDouble(String s) throws TypeConversionException
      Parses the specified string into a double. If the string does not represent a number, or if it cannot be parsed into a double without loss of information, a TypeConversionException is thrown.
      Parameters:
      s - the string to be parsed
      Returns:
      the double value represented by the string
      Throws:
      TypeConversionException - if the string does not represent or number, or if conversion would lead to loss of information
    • isDouble

      public static boolean isDouble(String s)
      Returns true if the specified string can be parsed into a double without loss of information. The argument is allowed to be null, in which case the return value will be false.
      Parameters:
      s - the string to be parsed
      Returns:
      whether he specified string can be parsed into a regular, finite double
    • toDouble

      public static OptionalDouble toDouble(String s)
      Returns an empty OptionalDouble if the specified string cannot be parsed into a double value, else an OptionalDouble containing the double value parsed out of the string.
      Parameters:
      s - the string to be parsed
      Returns:
      an OptionalDouble containing the double value parsed out of the string
    • parseFloat

      public static float parseFloat(String s) throws TypeConversionException
      Parses the specified string into a float. If the string does not represent a number, or if it cannot be parsed into a float without loss of information, a TypeConversionException is thrown.
      Parameters:
      s - the string to be parsed
      Returns:
      the float value represented by the string
      Throws:
      TypeConversionException - if the string does not represent or number, or if conversion would lead to loss of information
    • isFloat

      public static boolean isFloat(String s)
      Returns true if the specified string can be parsed into a float without loss of information. The argument is allowed to be null, in which case the return value will be false.
      Parameters:
      s - the string to be parsed
      Returns:
      whether he specified string can be parsed into a regular, finite float
    • toFloat

      public static OptionalDouble toFloat(String s)
      Returns an empty OptionalDouble if the specified string cannot be parsed into a regular, finite float value, else an OptionalDouble containing the float value parsed out of the string.
      Parameters:
      s - the string to be parsed
      Returns:
      an OptionalDouble containing the float value parsed out of the string
    • parseShort

      public static short parseShort(String s) throws TypeConversionException
      Parses the specified string into a short. If the string does not represent a number, or if it cannot be parsed into a short without loss of information, a TypeConversionException is thrown.
      Parameters:
      s - the string to be parsed
      Returns:
      the short value represented by the string
      Throws:
      TypeConversionException - if the string does not represent or number, or if conversion would lead to loss of information
    • isShort

      public static boolean isShort(String s)
      Returns true if the specified string can be parsed into a short without loss of information. The argument is allowed to be null, in which case the return value will be false.
      Parameters:
      s - the string to be parsed
      Returns:
      whether he specified string can be parsed into a short without causing integer overflow
    • toShort

      public static OptionalInt toShort(String s)
      Returns an empty OptionalInt if the specified string cannot be parsed into a 16-bit integer, else an OptionalInt containing the short value parsed out of the string.
      Parameters:
      s - the string to be parsed
      Returns:
      an OptionalInt containing the short value parsed out of the string
    • parseByte

      public static byte parseByte(String s) throws TypeConversionException
      Parses the specified string into a byte. If the string does not represent a number, or if it cannot be parsed into a byte without loss of information, a TypeConversionException is thrown.
      Parameters:
      s - the string to be parsed
      Returns:
      the byte value represented by the string
      Throws:
      TypeConversionException - if the string does not represent or number, or if conversion would lead to loss of information
    • isByte

      public static boolean isByte(String s)
      Returns true if the specified string can be parsed into a byte without loss of information. The argument is allowed to be null, in which case the return value will be false.
      Parameters:
      s - the string to be parsed
      Returns:
      whether he specified string can be parsed into a byte without causing an integer overflow
    • toByte

      public static OptionalInt toByte(String s)
      Returns an empty OptionalInt if the specified string cannot be parsed into an 8-bit integer, else an OptionalInt containing the byte value parsed out of the string.
      Parameters:
      s - the string to be parsed
      Returns:
      an OptionalInt containing the byte value parsed out of the string
    • parseBigInteger

      public static BigInteger parseBigInteger(String s) throws TypeConversionException
      Parses the specified string into a BigInteger. If the string does not represent a number, or if it cannot be parsed into a BigInteger without loss of information, a TypeConversionException is thrown.
      Parameters:
      s - the string to be parsed
      Returns:
      the BigInteger value represented by the string
      Throws:
      TypeConversionException - if the string does not represent or number, or if conversion would lead to loss of information
    • isBigInteger

      public static boolean isBigInteger(String s)
      Returns true if the specified string can be parsed into a BigInteger without loss of information. The argument is allowed to be null, in which case the return value will be false.
      Parameters:
      s - the string to be parsed
      Returns:
      whether he specified string can be parsed into a BigInteger
    • toBigInteger

      public static Optional<BigInteger> toBigInteger(String s)
      Returns an empty Optional if the specified string cannot be parsed into BigInteger, else an Optional containing the BigInteger value parsed out of the string.
      Parameters:
      s - the string to be parsed
      Returns:
      an Optional containing the BigInteger value parsed out of the string
    • parseBigDecimal

      public static BigDecimal parseBigDecimal(String s) throws TypeConversionException
      Parses the specified string into a BigInteger. If the string does not represent a number, a TypeConversionException is thrown.
      Parameters:
      s - the string to be parsed
      Returns:
      the BigDecimal value represented by the string
      Throws:
      TypeConversionException - if the string does not represent or number
    • isBigDecimal

      public static boolean isBigDecimal(String s)
      Returns true if the specified string can be parsed into a BigDecimal. The argument is allowed to be null, in which case the return value will be false.
      Parameters:
      s - the string to be parsed
      Returns:
      whether he specified string can be parsed into a BigDecimal
    • toBigDecimal

      public static Optional<BigDecimal> toBigDecimal(String s)
      Returns an empty Optional if the specified string cannot be parsed into BigDecimal, else an Optional containing the BigDecimal value parsed out of the string.
      Parameters:
      s - the string to be parsed
      Returns:
      an Optional containing the BigDecimal value parsed out of the string
    • parse

      public static <T extends Number> T parse(String s, Class<T> targetType) throws TypeConversionException
      Parses the specified string into a number of the specified type. Throws an TypeConversionException if the string is not a number, or if the number is too big to fit into the target type.
      Type Parameters:
      T - the type of Number to convert the string to
      Parameters:
      s - the string to be parsed
      targetType - the class of the Number type
      Returns:
      a Number of the specified type
      Throws:
      TypeConversionException - if the string does not represent or number, or if conversion would lead to loss of information
    • fitsInto

      public static <T extends Number> boolean fitsInto(String s, Class<T> targetType)
      Tests whether the specified string can be parsed into a Number of the specified type.
      Type Parameters:
      T - the type of Number to convert the string to
      Parameters:
      s - the string to be parsed
      targetType - the class of the Number type
      Returns:
      whether the specified string can be parsed into a Number of the specified type
    • toBigDecimal

      public static BigDecimal toBigDecimal(Number n)
      Converts a Number of an unspecified type to a BigDecimal.
      Parameters:
      n - the number
      Returns:
      the BigDecimal representing the number
    • convert

      public static <T extends Number, R extends Number> R convert(T number, Class<R> targetType) throws TypeConversionException
      Safely converts a number of an unspecified type to a number of a definite type. Throws a TypeConversionException if the number cannot be converted to the target type without loss of information. The number is allowed to be null, in which case null will be returned.
      Type Parameters:
      T - the input type
      R - the output type
      Parameters:
      number - the number to be converted
      targetType - the class of the target type
      Returns:
      an instance of the target type
      Throws:
      TypeConversionException
    • fitsInto

      public static <T extends Number> boolean fitsInto(Number number, Class<T> targetType)
      Returns true if the specified number can be converted to the specified target type without loss of information. The number is allowed to be null, in which case true will be returned.
      Type Parameters:
      T - the type of Number to convert to
      Parameters:
      number - the Number to convert
      targetType - the type of Number to convert to
      Returns:
      whether conversion will be lossless
    • isRound

      public static boolean isRound(float f)
      Determines whether the specified float's fractional part is zero or absent.
      Parameters:
      f - the float to inspect
      Returns:
      whether the specified float's fractional part is zero or absent
    • isRound

      public static boolean isRound(double d)
      Determines whether the specified double's fractional part is zero or absent.
      Parameters:
      d - the double to inspect
      Returns:
      whether the specified double's fractional part is zero or absent
    • isRound

      public static boolean isRound(BigDecimal bd)
      Determines whether the specified BigDecimal's fractional part is zero or absent.
      Parameters:
      bd - the BigDecimal to inspect
      Returns:
      whether the specified BigDecimal's fractional part is zero or absent