Package jmri.util

Class TypeConversionUtil

java.lang.Object
jmri.util.TypeConversionUtil

public final class TypeConversionUtil extends Object
Converts between java types, for example String to Double and double to boolean.
  • Constructor Details

  • Method Details

    • isIntegerNumber

      public static boolean isIntegerNumber(Object object)
      Is this object an integer number?

      The method returns true if the object is any of these classes:

      • AtomicInteger
      • AtomicLong
      • BigInteger
      • Byte
      • Short
      • Integer
      • Long
      Parameters:
      object - the object to check
      Returns:
      true if the object is an object that is an integer, false otherwise
    • isFloatingNumber

      public static boolean isFloatingNumber(Object object)
      Is this object an integer or a floating point number?

      The method returns true if the object is any of these classes:

      • AtomicInteger
      • AtomicLong
      • BigInteger
      • Byte
      • Short
      • Integer
      • Long
      • BigDecimal
      • Float
      • Double
      Parameters:
      object - the object to check
      Returns:
      true if the object is an object that is either an integer or a float, false otherwise
    • isString

      public static boolean isString(Object object)
      Is this object a String?
      Parameters:
      object - the object to check
      Returns:
      true if the object is a String, false otherwise
    • convertToBoolean

      public static boolean convertToBoolean(@CheckForNull Object value, boolean do_i18n)
      Convert a value to a boolean.

      Rules: "0" string is converted to false "0.000" string is converted to false, if the number of decimals is > 0 An integer number is converted to false if the number is 0 A floating number is converted to false if the number is -0.5 < x < 0.5 The string "true" (case insensitive) returns true. The string "false" (case insensitive) returns false. A Reportable is first converted to a string using toReportString() and then treated as a string. A JSON TextNode is first converted to a string using asText() and then treated as a string. Everything else throws an exception.

      For objects that implement the Reportable interface, the value is fetched from the method toReportString().

      Parameters:
      value - the value to convert
      do_i18n - true if internationalization should be done, false otherwise
      Returns:
      the boolean value
    • convertToBoolean_JythonRules

      public static boolean convertToBoolean_JythonRules(@CheckForNull Object value, boolean do_i18n)
      Convert a value to a boolean by Jython rules.

      Rules: null is converted to false empty string is converted to false "0" string is converted to false "0.000" string is converted to false, if the number of decimals is > 0 empty map is converted to false empty collection is converted to false An integer number is converted to false if the number is 0 A floating number is converted to false if the number is -0.5 < x < 0.5 Everything else is converted to true

      For objects that implement the Reportable interface, the value is fetched from the method toReportString().

      Parameters:
      value - the value to convert
      do_i18n - true if internationalization should be done, false otherwise
      Returns:
      the boolean value
    • convertToLong

      public static long convertToLong(@CheckForNull Object value)
      Convert a value to a long.

      Rules: null is converted to 0 empty string is converted to 0 empty collection is converted to 0 an instance of the interface Number is converted to the number a string that can be parsed as a number is converted to that number. a string that doesn't start with a digit is converted to 0

      For objects that implement the Reportable interface, the value is fetched from the method toReportString() before doing the conversion.

      Parameters:
      value - the value to convert
      Returns:
      the long value
    • convertToLong

      public static long convertToLong(@CheckForNull Object value, boolean checkAll, boolean throwOnError)
      Convert a value to a long.

      Rules: null is converted to 0 empty string is converted to 0 empty collection is converted to 0 an instance of the interface Number is converted to the number a string that can be parsed as a number is converted to that number. a string that doesn't start with a digit is converted to 0

      For objects that implement the Reportable interface, the value is fetched from the method toReportString() before doing the conversion.

      Parameters:
      value - the value to convert
      checkAll - true if the whole string should be checked, false otherwise
      throwOnError - true if a NumberFormatException should be thrown on error, false otherwise
      Returns:
      the long value
      Throws:
      NumberFormatException - on error if throwOnError is true
    • convertToLong

      public static long convertToLong(@CheckForNull Object value, boolean checkAll, boolean throwOnError, boolean warnOnError) throws NumberFormatException
      Convert a value to a long.

      Rules: null is converted to 0 empty string is converted to 0 empty collection is converted to 0 an instance of the interface Number is converted to the number a string that can be parsed as a number is converted to that number. a string that doesn't start with a digit is converted to 0

      For objects that implement the Reportable interface, the value is fetched from the method toReportString() before doing the conversion.

      Parameters:
      value - the value to convert
      checkAll - true if the whole string should be checked, false otherwise
      throwOnError - true if a NumberFormatException should be thrown on error, false otherwise
      warnOnError - true if a warning message should be logged on error
      Returns:
      the long value
      Throws:
      NumberFormatException - on error if throwOnError is true
    • convertToDouble

      public static double convertToDouble(@CheckForNull Object value, boolean do_i18n)
      Convert a value to a double.

      Rules: null is converted to 0 empty string is converted to 0 empty collection is converted to 0 an instance of the interface Number is converted to the number a string that can be parsed as a number is converted to that number. if a string starts with a number AND do_i18n is false, it's converted to that number a string that doesn't start with a digit is converted to 0

      For objects that implement the Reportable interface, the value is fetched from the method toReportString() before doing the conversion.

      Parameters:
      value - the value to convert
      do_i18n - true if internationalization should be done, false otherwise
      Returns:
      the double value
    • convertToDouble

      public static double convertToDouble(@CheckForNull Object value, boolean do_i18n, boolean checkAll, boolean throwOnError)
      Convert a value to a double.

      Rules: null is converted to 0 empty string is converted to 0 empty collection is converted to 0 an instance of the interface Number is converted to the number a string that can be parsed as a number is converted to that number. if a string starts with a number AND do_i18n is false, it's converted to that number a string that doesn't start with a digit is converted to 0

      For objects that implement the Reportable interface, the value is fetched from the method toReportString() before doing the conversion.

      Parameters:
      value - the value to convert
      do_i18n - true if internationalization should be done, false otherwise
      checkAll - true if the whole string should be checked, false otherwise
      throwOnError - true if a NumberFormatException should be thrown on error, false otherwise
      Returns:
      the double value
      Throws:
      NumberFormatException - on error if throwOnError is true
    • convertToDouble

      public static double convertToDouble(@CheckForNull Object value, boolean do_i18n, boolean checkAll, boolean throwOnError, boolean warnOnError)
      Convert a value to a double.

      Rules: null is converted to 0 empty string is converted to 0 empty collection is converted to 0 an instance of the interface Number is converted to the number a string that can be parsed as a number is converted to that number. if a string starts with a number AND do_i18n is false, it's converted to that number a string that doesn't start with a digit is converted to 0

      For objects that implement the Reportable interface, the value is fetched from the method toReportString() before doing the conversion.

      Parameters:
      value - the value to convert
      do_i18n - true if internationalization should be done, false otherwise
      checkAll - true if the whole string should be checked, false otherwise
      throwOnError - true if a NumberFormatException should be thrown on error, false otherwise
      warnOnError - true if a warning message should be logged on error
      Returns:
      the double value
      Throws:
      NumberFormatException - on error if throwOnError is true
    • convertToString

      @Nonnull public static String convertToString(@CheckForNull Object value, boolean do_i18n)
      Convert a value to a String.
      Parameters:
      value - the value to convert
      do_i18n - true if internationalization should be done, false otherwise
      Returns:
      the String value