Package jmri.util

Class TypeConversionUtil


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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean convertToBoolean​(java.lang.Object value, boolean do_i18n)
      Convert a value to a boolean.
      static boolean convertToBoolean_JythonRules​(java.lang.Object value, boolean do_i18n)
      Convert a value to a boolean by Jython rules.
      static double convertToDouble​(java.lang.Object value, boolean do_i18n)
      Convert a value to a double.
      static double convertToDouble​(java.lang.Object value, boolean do_i18n, boolean checkAll, boolean throwOnError)
      Convert a value to a double.
      static double convertToDouble​(java.lang.Object value, boolean do_i18n, boolean checkAll, boolean throwOnError, boolean warnOnError)
      Convert a value to a double.
      static long convertToLong​(java.lang.Object value)
      Convert a value to a long.
      static long convertToLong​(java.lang.Object value, boolean checkAll, boolean throwOnError)
      Convert a value to a long.
      static long convertToLong​(java.lang.Object value, boolean checkAll, boolean throwOnError, boolean warnOnError)
      Convert a value to a long.
      static java.lang.String convertToString​(java.lang.Object value, boolean do_i18n)
      Convert a value to a String.
      static boolean isFloatingNumber​(java.lang.Object object)
      Is this object an integer or a floating point number?
      static boolean isIntegerNumber​(java.lang.Object object)
      Is this object an integer number?
      static boolean isString​(java.lang.Object object)
      Is this object a String?
      • Methods inherited from class java.lang.Object

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

      • isIntegerNumber

        public static boolean isIntegerNumber​(java.lang.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​(java.lang.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​(java.lang.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
                                               java.lang.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
                                                           java.lang.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
                                         java.lang.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
                                         java.lang.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:
        java.lang.NumberFormatException - on error if throwOnError is true
      • convertToLong

        public static long convertToLong​(@CheckForNull
                                         java.lang.Object value,
                                         boolean checkAll,
                                         boolean throwOnError,
                                         boolean warnOnError)
                                  throws java.lang.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:
        java.lang.NumberFormatException - on error if throwOnError is true
      • convertToDouble

        public static double convertToDouble​(@CheckForNull
                                             java.lang.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
                                             java.lang.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:
        java.lang.NumberFormatException - on error if throwOnError is true
      • convertToDouble

        public static double convertToDouble​(@CheckForNull
                                             java.lang.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:
        java.lang.NumberFormatException - on error if throwOnError is true
      • convertToString

        @Nonnull
        public static java.lang.String convertToString​(@CheckForNull
                                                       java.lang.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