Class PropertyConverter


  • public final class PropertyConverter
    extends java.lang.Object
    A utility class to convert the configuration properties into any type.
    Since:
    1.1
    Version:
    $Id: PropertyConverter.java 1534376 2013-10-21 21:14:18Z henning $
    Author:
    Emmanuel Bourg
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String escapeDelimiters​(java.lang.String s, char delimiter)
      Escapes the delimiters that might be contained in the given string.
      static java.lang.String escapeListDelimiter​(java.lang.String s, char delimiter)
      Escapes the list delimiter if it is contained in the given string.
      static java.lang.Object interpolate​(java.lang.Object value, AbstractConfiguration config)
      Performs interpolation of the specified value.
      static java.util.List<java.lang.String> split​(java.lang.String s, char delimiter)
      Split a string on the specified delimiter always trimming the elements.
      static java.util.List<java.lang.String> split​(java.lang.String s, char delimiter, boolean trim)
      Split a string on the specified delimiter.
      static java.math.BigDecimal toBigDecimal​(java.lang.Object value)
      Convert the specified object into a BigDecimal.
      static java.math.BigInteger toBigInteger​(java.lang.Object value)
      Convert the specified object into a BigInteger.
      static java.lang.Boolean toBoolean​(java.lang.Object value)
      Convert the specified object into a Boolean.
      static java.lang.Byte toByte​(java.lang.Object value)
      Convert the specified object into a Byte.
      static java.util.Calendar toCalendar​(java.lang.Object value, java.lang.String format)
      Convert 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)
      Convert the specified object into a Color.
      static java.util.Date toDate​(java.lang.Object value, java.lang.String format)
      Convert the specified object into a Date.
      static java.lang.Double toDouble​(java.lang.Object value)
      Convert the specified object into a Double.
      static java.lang.Float toFloat​(java.lang.Object value)
      Convert the specified object into a Float.
      static java.lang.Integer toInteger​(java.lang.Object value)
      Convert the specified object into an Integer.
      static java.util.Iterator<?> toIterator​(java.lang.Object value, char delimiter)
      Returns an iterator over the simple values of a composite value.
      static java.util.Locale toLocale​(java.lang.Object value)
      Convert the specified object into a Locale.
      static java.lang.Long toLong​(java.lang.Object value)
      Convert the specified object into a Long.
      static java.lang.Short toShort​(java.lang.Object value)
      Convert the specified object into a Short.
      static java.net.URL toURL​(java.lang.Object value)
      Convert the specified object into an URL.
      • Methods inherited from class java.lang.Object

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

      • toBoolean

        public static java.lang.Boolean toBoolean​(java.lang.Object value)
                                           throws ConversionException
        Convert 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
      • 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
      • toByte

        public static java.lang.Byte toByte​(java.lang.Object value)
                                     throws ConversionException
        Convert 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
      • toShort

        public static java.lang.Short toShort​(java.lang.Object value)
                                       throws ConversionException
        Convert 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
      • toInteger

        public static java.lang.Integer toInteger​(java.lang.Object value)
                                           throws ConversionException
        Convert 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
      • toLong

        public static java.lang.Long toLong​(java.lang.Object value)
                                     throws ConversionException
        Convert 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
      • toFloat

        public static java.lang.Float toFloat​(java.lang.Object value)
                                       throws ConversionException
        Convert 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
      • toDouble

        public static java.lang.Double toDouble​(java.lang.Object value)
                                         throws ConversionException
        Convert 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
      • toBigInteger

        public static java.math.BigInteger toBigInteger​(java.lang.Object value)
                                                 throws ConversionException
        Convert 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
      • toBigDecimal

        public static java.math.BigDecimal toBigDecimal​(java.lang.Object value)
                                                 throws ConversionException
        Convert 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
      • toURL

        public static java.net.URL toURL​(java.lang.Object value)
                                  throws ConversionException
        Convert 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
      • toLocale

        public static java.util.Locale toLocale​(java.lang.Object value)
                                         throws ConversionException
        Convert 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
      • split

        public static java.util.List<java.lang.String> split​(java.lang.String s,
                                                             char delimiter,
                                                             boolean trim)
        Split a string on the specified delimiter. To be removed when commons-lang has a better replacement available (Tokenizer?). todo: replace with a commons-lang equivalent
        Parameters:
        s - the string to split
        delimiter - the delimiter
        trim - a flag whether the single elements should be trimmed
        Returns:
        a list with the single tokens
      • split

        public static java.util.List<java.lang.String> split​(java.lang.String s,
                                                             char delimiter)
        Split a string on the specified delimiter always trimming the elements. This is a shortcut for split(s, delimiter, true).
        Parameters:
        s - the string to split
        delimiter - the delimiter
        Returns:
        a list with the single tokens
      • escapeDelimiters

        public static java.lang.String escapeDelimiters​(java.lang.String s,
                                                        char delimiter)
        Escapes the delimiters that might be contained in the given string. This method works like escapeListDelimiter(String, char). In addition, a single backslash will also be escaped.
        Parameters:
        s - the string with the value
        delimiter - the list delimiter to use
        Returns:
        the correctly escaped string
      • escapeListDelimiter

        public static java.lang.String escapeListDelimiter​(java.lang.String s,
                                                           char delimiter)
        Escapes the list delimiter if it is contained in the given string. This method ensures that list delimiter characters that are part of a property's value are correctly escaped when a configuration is saved to a file. Otherwise when loaded again the property will be treated as a list property.
        Parameters:
        s - the string with the value
        delimiter - the list delimiter to use
        Returns:
        the escaped string
        Since:
        1.7
      • toColor

        public static java.awt.Color toColor​(java.lang.Object value)
                                      throws ConversionException
        Convert 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
        Convert 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
      • toCalendar

        public static java.util.Calendar toCalendar​(java.lang.Object value,
                                                    java.lang.String format)
                                             throws ConversionException
        Convert 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
      • toIterator

        public static java.util.Iterator<?> toIterator​(java.lang.Object value,
                                                       char delimiter)
        Returns an iterator over the simple values of a composite value. This implementation calls #flatten(Object, char) and returns an iterator over the returned collection.
        Parameters:
        value - the value to "split"
        delimiter - the delimiter for String values
        Returns:
        an iterator for accessing the single values
      • interpolate

        public static java.lang.Object interpolate​(java.lang.Object value,
                                                   AbstractConfiguration config)
        Performs interpolation of the specified value. This method checks if the given value contains variables of the form ${...}. If this is the case, all occurrences will be substituted by their current values.
        Parameters:
        value - the value to be interpolated
        config - the current configuration object
        Returns:
        the interpolated value