Class PropertyConverter


  • public final class PropertyConverter
    extends java.lang.Object
    A utility class to convert the configuration properties into any type.
    Since:
    2.8.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String BIN_PREFIX
      Constant for the prefix of binary numbers.
      private static int BIN_RADIX
      Constant for the radix of binary numbers.
      private static java.lang.Class<?>[] CONSTR_ARGS
      Constant for the argument classes of the Number constructor that takes a String.
      private static java.lang.String HEX_PREFIX
      Constant for the prefix of hex numbers.
      private static int HEX_RADIX
      Constant for the radix of hex numbers.
      private static java.lang.String INTERNET_ADDRESS_CLASSNAME_JAKARTA
      The fully qualified name of jakarta.mail.internet.InternetAddress, as used in the javamail-2.0+ API.
      private static java.lang.String INTERNET_ADDRESS_CLASSNAME_JAVAX
      The fully qualified name of javax.mail.internet.InternetAddress, as used in the javamail-1.* API.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private PropertyConverter()
      Private constructor prevents instances from being created.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.Object convertToEnum​(java.lang.Class<?> enumClass, java.lang.Object value)
      Converts a value to a constant of an enumeration class.
      static java.lang.Object to​(java.lang.Class<?> cls, java.lang.Object value, DefaultConversionHandler convHandler)
      Converts the specified value object to the given target data class.
      static java.math.BigDecimal toBigDecimal​(java.lang.Object value)
      Converts the specified object into a BigDecimal.
      static java.math.BigInteger toBigInteger​(java.lang.Object value)
      Converts the specified object into a BigInteger.
      static java.lang.Boolean toBoolean​(java.lang.Object value)
      Converts the specified object into a Boolean.
      static java.lang.Byte toByte​(java.lang.Object value)
      Converts the specified object into a Byte.
      static java.util.Calendar toCalendar​(java.lang.Object value, java.lang.String format)
      Converts the specified object into a Calendar.
      static java.lang.Character toCharacter​(java.lang.Object value)
      Converts the specified value object to a Character.
      static java.awt.Color toColor​(java.lang.Object value)
      Converts the specified object into a Color.
      static java.util.Date toDate​(java.lang.Object value, java.lang.String format)
      Converts the specified object into a Date.
      static java.lang.Double toDouble​(java.lang.Object value)
      Converts the specified object into a Double.
      static java.time.Duration toDuration​(java.lang.Object value)
      Converts the specified object into a Duration.
      (package private) static <E extends java.lang.Enum<E>>
      E
      toEnum​(java.lang.Object value, java.lang.Class<E> cls)
      Converts the specified value into an Enum.
      static java.io.File toFile​(java.lang.Object value)
      Converts the specified object into a File.
      static java.lang.Float toFloat​(java.lang.Object value)
      Converts the specified object into a Float.
      (package private) static java.net.InetAddress toInetAddress​(java.lang.Object value)
      Converts the specified value into an internet address.
      static java.lang.Integer toInteger​(java.lang.Object value)
      Converts the specified object into an Integer.
      (package private) static java.lang.Object toInternetAddress​(java.lang.Object value, java.lang.String targetClassName)
      Converts the specified value into an email address with the given class name.
      static java.util.Locale toLocale​(java.lang.Object value)
      Converts the specified object into a Locale.
      static java.lang.Long toLong​(java.lang.Object value)
      Converts the specified object into a Long.
      (package private) static java.lang.Number toNumber​(java.lang.Object value, java.lang.Class<?> targetClass)
      Tries to convert the specified object into a number object.
      static java.nio.file.Path toPath​(java.lang.Object value)
      Converts the specified object into a Path.
      static java.util.regex.Pattern toPattern​(java.lang.Object value)
      Converts the specified object into a Pattern.
      static java.lang.Short toShort​(java.lang.Object value)
      Converts the specified object into a Short.
      static java.net.URI toURI​(java.lang.Object value)
      Converts the specified object into an URI.
      static java.net.URL toURL​(java.lang.Object value)
      Converts the specified object into an URL.
      • Methods inherited from class java.lang.Object

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

      • HEX_PREFIX

        private static final java.lang.String HEX_PREFIX
        Constant for the prefix of hex numbers.
        See Also:
        Constant Field Values
      • HEX_RADIX

        private static final int HEX_RADIX
        Constant for the radix of hex numbers.
        See Also:
        Constant Field Values
      • BIN_PREFIX

        private static final java.lang.String BIN_PREFIX
        Constant for the prefix of binary numbers.
        See Also:
        Constant Field Values
      • BIN_RADIX

        private static final int BIN_RADIX
        Constant for the radix of binary numbers.
        See Also:
        Constant Field Values
      • CONSTR_ARGS

        private static final java.lang.Class<?>[] CONSTR_ARGS
        Constant for the argument classes of the Number constructor that takes a String.
      • INTERNET_ADDRESS_CLASSNAME_JAVAX

        private static final java.lang.String INTERNET_ADDRESS_CLASSNAME_JAVAX
        The fully qualified name of javax.mail.internet.InternetAddress, as used in the javamail-1.* API.
        See Also:
        Constant Field Values
      • INTERNET_ADDRESS_CLASSNAME_JAKARTA

        private static final java.lang.String INTERNET_ADDRESS_CLASSNAME_JAKARTA
        The fully qualified name of jakarta.mail.internet.InternetAddress, as used in the javamail-2.0+ API.
        See Also:
        Constant Field Values
    • Constructor Detail

      • PropertyConverter

        private PropertyConverter()
        Private constructor prevents instances from being created.
    • Method Detail

      • convertToEnum

        private static java.lang.Object convertToEnum​(java.lang.Class<?> enumClass,
                                                      java.lang.Object value)
        Converts a value to a constant of an enumeration class.
        Parameters:
        enumClass - the enumeration class
        value - the value to be converted
        Returns:
        the converted value
      • to

        public static java.lang.Object to​(java.lang.Class<?> cls,
                                          java.lang.Object value,
                                          DefaultConversionHandler convHandler)
                                   throws ConversionException
        Converts the specified value object to the given target data class. If additional information is required for this conversion, it is obtained from the passed in DefaultConversionHandler object. If the class is a primitive type (Integer.TYPE, Boolean.TYPE, etc), the value returned will use the wrapper type (Integer.class, Boolean.class, etc).
        Parameters:
        cls - the target class of the converted value
        value - the value to convert
        convHandler - the conversion handler object
        Returns:
        the converted value
        Throws:
        ConversionException - if the value is not compatible with the requested type
      • toBigDecimal

        public static java.math.BigDecimal toBigDecimal​(java.lang.Object value)
                                                 throws ConversionException
        Converts the specified object into a BigDecimal.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a BigDecimal
      • toBigInteger

        public static java.math.BigInteger toBigInteger​(java.lang.Object value)
                                                 throws ConversionException
        Converts the specified object into a BigInteger.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a BigInteger
      • toBoolean

        public static java.lang.Boolean toBoolean​(java.lang.Object value)
                                           throws ConversionException
        Converts the specified object into a Boolean. Internally the org.apache.commons.lang.BooleanUtils class from the Commons Lang project is used to perform this conversion. This class accepts some more tokens for the boolean value of true, e.g. yes and on. Please refer to the documentation of this class for more details.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a boolean
      • toByte

        public static java.lang.Byte toByte​(java.lang.Object value)
                                     throws ConversionException
        Converts the specified object into a Byte.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a byte
      • toCalendar

        public static java.util.Calendar toCalendar​(java.lang.Object value,
                                                    java.lang.String format)
                                             throws ConversionException
        Converts the specified object into a Calendar.
        Parameters:
        value - the value to convert
        format - the DateFormat pattern to parse String values
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a Calendar
      • toCharacter

        public static java.lang.Character toCharacter​(java.lang.Object value)
                                               throws ConversionException
        Converts the specified value object to a Character. This method converts the passed in object to a string. If the string has exactly one character, this character is returned as result. Otherwise, conversion fails.
        Parameters:
        value - the value to be converted
        Returns:
        the resulting Character object
        Throws:
        ConversionException - if the conversion is not possible
      • toColor

        public static java.awt.Color toColor​(java.lang.Object value)
                                      throws ConversionException
        Converts the specified object into a Color. If the value is a String, the format allowed is (#)?[0-9A-F]{6}([0-9A-F]{2})?. Examples:
        • FF0000 (red)
        • 0000FFA0 (semi transparent blue)
        • #CCCCCC (gray)
        • #00FF00A0 (semi transparent green)
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a Color
      • toDate

        public static java.util.Date toDate​(java.lang.Object value,
                                            java.lang.String format)
                                     throws ConversionException
        Converts the specified object into a Date.
        Parameters:
        value - the value to convert
        format - the DateFormat pattern to parse String values
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a Calendar
      • toDouble

        public static java.lang.Double toDouble​(java.lang.Object value)
                                         throws ConversionException
        Converts the specified object into a Double.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a Double
      • toDuration

        public static java.time.Duration toDuration​(java.lang.Object value)
                                             throws ConversionException
        Converts the specified object into a Duration.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a Duration
        Since:
        2.8.0
      • toEnum

        static <E extends java.lang.Enum<E>> E toEnum​(java.lang.Object value,
                                                      java.lang.Class<E> cls)
                                               throws ConversionException
        Converts the specified value into an Enum.
        Parameters:
        value - the value to convert
        cls - the type of the enumeration
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to an enumeration
        Since:
        1.5
      • toFile

        public static java.io.File toFile​(java.lang.Object value)
                                   throws ConversionException
        Converts the specified object into a File.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a File
        Since:
        2.3
      • toFloat

        public static java.lang.Float toFloat​(java.lang.Object value)
                                       throws ConversionException
        Converts the specified object into a Float.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a Float
      • toInetAddress

        static java.net.InetAddress toInetAddress​(java.lang.Object value)
                                           throws ConversionException
        Converts the specified value into an internet address.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a InetAddress
        Since:
        1.5
      • toInteger

        public static java.lang.Integer toInteger​(java.lang.Object value)
                                           throws ConversionException
        Converts the specified object into an Integer.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to an integer
      • toInternetAddress

        static java.lang.Object toInternetAddress​(java.lang.Object value,
                                                  java.lang.String targetClassName)
                                           throws ConversionException
        Converts the specified value into an email address with the given class name.
        Parameters:
        value - the value to convert
        targetClassName - the fully qualified name of the InternetAddress class to convert to, e.g., "javax.mail.internet.InternetAddress" or "jakarta.mail.internet.InternetAddress"
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to an email address
        Since:
        1.5
      • toLocale

        public static java.util.Locale toLocale​(java.lang.Object value)
                                         throws ConversionException
        Converts the specified object into a Locale.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a Locale
      • toLong

        public static java.lang.Long toLong​(java.lang.Object value)
                                     throws ConversionException
        Converts the specified object into a Long.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a Long
      • toNumber

        static java.lang.Number toNumber​(java.lang.Object value,
                                         java.lang.Class<?> targetClass)
                                  throws ConversionException
        Tries to convert the specified object into a number object. This method is used by the conversion methods for number types. Note that the return value is not in always of the specified target class, but only if a new object has to be created.
        Parameters:
        value - the value to be converted (must not be null)
        targetClass - the target class of the conversion (must be derived from Number)
        Returns:
        the converted number
        Throws:
        ConversionException - if the object cannot be converted
      • toPath

        public static java.nio.file.Path toPath​(java.lang.Object value)
                                         throws ConversionException
        Converts the specified object into a Path.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a Path
        Since:
        2.3
      • toPattern

        public static java.util.regex.Pattern toPattern​(java.lang.Object value)
                                                 throws ConversionException
        Converts the specified object into a Pattern.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a Pattern
      • toShort

        public static java.lang.Short toShort​(java.lang.Object value)
                                       throws ConversionException
        Converts the specified object into a Short.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to a short
      • toURI

        public static java.net.URI toURI​(java.lang.Object value)
                                  throws ConversionException
        Converts the specified object into an URI.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to an URI
      • toURL

        public static java.net.URL toURL​(java.lang.Object value)
                                  throws ConversionException
        Converts the specified object into an URL.
        Parameters:
        value - the value to convert
        Returns:
        the converted value
        Throws:
        ConversionException - thrown if the value cannot be converted to an URL