Class PropertiesUtils


  • public final class PropertiesUtils
    extends Object
    Static utility methods for common tasks having to do with java.util.Properties.
    • Constructor Detail

      • PropertiesUtils

        public PropertiesUtils()
    • Method Detail

      • clone

        public static Properties clone​(Properties props)
        Performs a correct deep clone of the properties object by capturing all properties in the default(s) and placing them directly into the new Properties object.
      • clone

        public static Properties clone​(Properties props,
                                       Properties defaults,
                                       boolean deepClone)
        Performs a correct deep clone of the properties object by capturing all properties in the default(s) and placing them directly into the new Properties object.
      • putAll

        public static void putAll​(Properties addToThis,
                                  Properties withThese)

        This method is intended to replace the use of the putAll method of Properties inherited from java.util.Hashtable. The problem with that method is that, since it is inherited from Hashtable, default properties are lost.

        For example, the following code

        
         Properties a;
         Properties b;
         //initialize ...
         a.putAll(b);
         
        will fail if b had been constructed with a default Properties object. Those defaults would be lost and not added to a.

        The above code could be correctly performed with this method, like this:

        
         Properties a;
         Properties b;
         //initialize ...
         PropertiesUtils.putAll(a,b);
         
        In the above example, a is modified - properties are added to it (note that if a has defaults they will remain unaffected.) The properties from b, including defaults, will be added to a using its setProperty method - these new properties will overwrite any pre-existing ones of the same name.
        Parameters:
        addToThis - This Properties object is modified; the properties of the other parameter are added to this. The added property values will replace any current property values of the same names.
        withThese - The properties (including defaults) of this object are added to the "addToThis" parameter.
      • getLongProperty

        public static long getLongProperty​(Properties props,
                                           String propName,
                                           long defaultValue)
      • getFloatProperty

        public static float getFloatProperty​(Properties props,
                                             String propName,
                                             float defaultValue)
      • getDoubleProperty

        public static double getDoubleProperty​(Properties props,
                                               String propName,
                                               double defaultValue)
      • getBooleanProperty

        public static boolean getBooleanProperty​(Properties props,
                                                 String propName,
                                                 boolean defaultValue)
      • toHex

        public static char toHex​(int nibble)
        Convert a nibble to a hex character
        Parameters:
        nibble - the nibble to convert.
      • toHex

        public static String toHex​(byte[] bytes)
      • fromHex

        public static byte[] fromHex​(String hex)
        Return the bytes for a given hex string, or throw an IllegalArgumentException
        Parameters:
        hex -
        Returns:
      • setBeanProperties

        public static void setBeanProperties​(Object bean,
                                             Properties props,
                                             String prefix)
      • setBeanProperties

        public static void setBeanProperties​(Object bean,
                                             Properties props,
                                             String prefix,
                                             boolean includeEnv)
      • setBeanProperty

        public static void setBeanProperty​(Object bean,
                                           String name,
                                           Object value)
      • getHierarchicalProperty

        public static String getHierarchicalProperty​(String key,
                                                     String defaultValue)
        Search for the property first in the environment, then in the system properties
        Parameters:
        key -
        defaultValue -
        Returns:
      • getHierarchicalProperty

        public static <T> T getHierarchicalProperty​(String key,
                                                    T defaultValue,
                                                    Class<T> clazz)
        Search for the property first in the environment, then in the system properties
        Parameters:
        key -
        defaultValue -
        clazz -
        Returns:
      • getDefaultProperties

        public static Properties getDefaultProperties()