Class PropertiesUtil


  • public final class PropertiesUtil
    extends java.lang.Object
    The PropertiesUtil is a utility class providing some useful utility methods for converting property types.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void populate​(java.lang.Object instance, java.util.Map<java.lang.String,​?> config, boolean validate)
      Populates the bean properties from config instance.
      static boolean toBoolean​(java.lang.Object propValue, boolean defaultValue)
      Returns the boolean value of the parameter or the defaultValue if the parameter is null.
      static double toDouble​(java.lang.Object propValue, double defaultValue)
      Returns the parameter as a double or the defaultValue if the parameter is null or if the parameter is not a Double and cannot be converted to a Double from the parameter's string value.
      static int toInteger​(java.lang.Object propValue, int defaultValue)
      Returns the parameter as an integer or the defaultValue if the parameter is null or if the parameter is not an Integer and cannot be converted to an Integer from the parameter's string value.
      static long toLong​(java.lang.Object propValue, long defaultValue)
      Returns the parameter as a long or the defaultValue if the parameter is null or if the parameter is not a Long and cannot be converted to a Long from the parameter's string value.
      static java.lang.Object toObject​(java.lang.Object propValue)
      Returns the parameter as a single value.
      static java.lang.String toString​(java.lang.Object propValue, java.lang.String defaultValue)
      Returns the parameter as a string or the defaultValue if the parameter is null.
      static java.lang.String[] toStringArray​(java.lang.Object propValue)
      Returns the parameter as an array of Strings.
      static java.lang.String[] toStringArray​(java.lang.Object propValue, java.lang.String[] defaultArray)
      Returns the parameter as an array of Strings.
      • Methods inherited from class java.lang.Object

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

      • toBoolean

        public static boolean toBoolean​(java.lang.Object propValue,
                                        boolean defaultValue)
        Returns the boolean value of the parameter or the defaultValue if the parameter is null. If the parameter is not a Boolean it is converted by calling Boolean.valueOf on the string value of the object.
        Parameters:
        propValue - the property value or null
        defaultValue - the default boolean value
      • toString

        public static java.lang.String toString​(java.lang.Object propValue,
                                                java.lang.String defaultValue)
        Returns the parameter as a string or the defaultValue if the parameter is null.
        Parameters:
        propValue - the property value or null
        defaultValue - the default string value
      • toLong

        public static long toLong​(java.lang.Object propValue,
                                  long defaultValue)
        Returns the parameter as a long or the defaultValue if the parameter is null or if the parameter is not a Long and cannot be converted to a Long from the parameter's string value.
        Parameters:
        propValue - the property value or null
        defaultValue - the default long value
      • toInteger

        public static int toInteger​(java.lang.Object propValue,
                                    int defaultValue)
        Returns the parameter as an integer or the defaultValue if the parameter is null or if the parameter is not an Integer and cannot be converted to an Integer from the parameter's string value.
        Parameters:
        propValue - the property value or null
        defaultValue - the default integer value
      • toDouble

        public static double toDouble​(java.lang.Object propValue,
                                      double defaultValue)
        Returns the parameter as a double or the defaultValue if the parameter is null or if the parameter is not a Double and cannot be converted to a Double from the parameter's string value.
        Parameters:
        propValue - the property value or null
        defaultValue - the default double value
      • toObject

        public static java.lang.Object toObject​(java.lang.Object propValue)
        Returns the parameter as a single value. If the parameter is neither an array nor a java.util.Collection the parameter is returned unmodified. If the parameter is a non-empty array, the first array element is returned. If the property is a non-empty java.util.Collection, the first collection element is returned. Otherwise null is returned.
        Parameters:
        propValue - the parameter to convert.
      • toStringArray

        public static java.lang.String[] toStringArray​(java.lang.Object propValue)
        Returns the parameter as an array of Strings. If the parameter is a scalar value its string value is returned as a single element array. If the parameter is an array, the elements are converted to String objects and returned as an array. If the parameter is a collection, the collection elements are converted to String objects and returned as an array. Otherwise (if the parameter is null) null is returned.
        Parameters:
        propValue - The object to convert.
      • toStringArray

        public static java.lang.String[] toStringArray​(java.lang.Object propValue,
                                                       java.lang.String[] defaultArray)
        Returns the parameter as an array of Strings. If the parameter is a scalar value its string value is returned as a single element array. If the parameter is an array, the elements are converted to String objects and returned as an array. If the parameter is a collection, the collection elements are converted to String objects and returned as an array. Otherwise (if the property is null) a provided default value is returned.
        Parameters:
        propValue - The object to convert.
        defaultArray - The default array to return.
      • populate

        public static void populate​(java.lang.Object instance,
                                    java.util.Map<java.lang.String,​?> config,
                                    boolean validate)
        Populates the bean properties from config instance. It supports coercing values for simple types like Number, Integer, Long, Boolean etc. Complex objects are not supported
        Parameters:
        instance - bean to populate
        config - properties to set in the passed bean
        validate - Flag to validate the configured bean property names against the configured bean class