Class PropertiesUtilities


  • public class PropertiesUtilities
    extends java.lang.Object
    Provides convenience access routines to Properties.
    Author:
    Garret Wilson
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String FALSE_STRING
      A string representation of boolean false.
      static java.lang.String PROPERTIES_NAME_EXTENSION
      The name extension for properties files, such as Java properties files.
      static java.lang.String TRUE_STRING
      A string representation of boolean true.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean getBooleanProperty​(java.util.Properties properties, java.lang.String propertyName)
      Retrieves a property as a boolean value.
      static boolean getBooleanProperty​(java.util.Properties properties, java.lang.String propertyName, boolean defaultValue)
      Retrieves a property as a boolean value, returning the default if no value is present.
      static java.io.File getFileProperty​(java.util.Properties properties, java.lang.String propertyName, java.io.File defaultValue)
      Retrieves a property as a file, returning the default if no value is present.
      static int getIntProperty​(java.util.Properties properties, java.lang.String propertyName, int defaultValue)
      Retrieves a property as an integer value, returning the default if no value is present.
      static java.util.Properties loadPropertiesResource​(java.lang.Class<?> objectClass)
      Loads properties for the given class from a properties resource.
      static java.util.Properties loadPropertiesResource​(java.lang.Class<?> objectClass, java.lang.String baseName)
      Loads properties for the given class from a properties resource determined from a given base name.
      static void setProperty​(java.util.Properties properties, java.lang.String propertyName, boolean propertyValue)
      Sets a boolean property by converting the boolean value to a string and storing it in the underlying properties.
      static void setProperty​(java.util.Properties properties, java.lang.String propertyName, int propertyValue)
      Sets an integer property by converting the integer value to a string and storing it in the underlying properties.
      static void setProperty​(java.util.Properties properties, java.lang.String propertyName, java.io.File propertyValue)
      Sets a file property by converting the file to a string and storing it in the underlying properties.
      static java.util.Properties toProperties​(java.util.Map<?,​?> map)
      Converts the given map to a properties object.
      • Methods inherited from class java.lang.Object

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

      • PROPERTIES_NAME_EXTENSION

        public static final java.lang.String PROPERTIES_NAME_EXTENSION
        The name extension for properties files, such as Java properties files.
        See Also:
        Constant Field Values
      • TRUE_STRING

        public static final java.lang.String TRUE_STRING
        A string representation of boolean true.
      • FALSE_STRING

        public static final java.lang.String FALSE_STRING
        A string representation of boolean false.
    • Constructor Detail

      • PropertiesUtilities

        public PropertiesUtilities()
    • Method Detail

      • getBooleanProperty

        public static boolean getBooleanProperty​(java.util.Properties properties,
                                                 java.lang.String propertyName)
        Retrieves a property as a boolean value.
        Parameters:
        properties - The properties object from which to retrieve the value.
        propertyName - The name of the property to access.
        Returns:
        The boolean value of the named property, or false if there is no value or the value does not represent a boolean value.
        See Also:
        Properties.getProperty(java.lang.String)
      • getBooleanProperty

        public static boolean getBooleanProperty​(java.util.Properties properties,
                                                 java.lang.String propertyName,
                                                 boolean defaultValue)
        Retrieves a property as a boolean value, returning the default if no value is present.
        Parameters:
        properties - The properties object from which to retrieve the value.
        propertyName - The name of the property to access.
        defaultValue - The value to return if there is no value present.
        Returns:
        The boolean value of the named property or, if there is no value or the value does not represent a boolean value, the default value.
        See Also:
        Properties.getProperty(java.lang.String)
      • setProperty

        public static void setProperty​(java.util.Properties properties,
                                       java.lang.String propertyName,
                                       boolean propertyValue)
        Sets a boolean property by converting the boolean value to a string and storing it in the underlying properties.
        Parameters:
        properties - The properties object in which to set the value.
        propertyName - The name of the property to set.
        propertyValue - The new boolean value of the property.
        See Also:
        Properties.setProperty(java.lang.String, java.lang.String)
      • getFileProperty

        public static java.io.File getFileProperty​(java.util.Properties properties,
                                                   java.lang.String propertyName,
                                                   java.io.File defaultValue)
        Retrieves a property as a file, returning the default if no value is present.
        Parameters:
        properties - The properties object from which to retrieve the value.
        propertyName - The name of the property to access.
        defaultValue - The value to return if there is no value present.
        Returns:
        The value of the named property or, if there is no value, the default value.
        See Also:
        Properties.getProperty(java.lang.String)
      • setProperty

        public static void setProperty​(java.util.Properties properties,
                                       java.lang.String propertyName,
                                       java.io.File propertyValue)
        Sets a file property by converting the file to a string and storing it in the underlying properties.
        Parameters:
        properties - The properties object in which to set the value.
        propertyName - The name of the property to set.
        propertyValue - The new file value of the property.
        See Also:
        Properties.setProperty(java.lang.String, java.lang.String)
      • getIntProperty

        public static int getIntProperty​(java.util.Properties properties,
                                         java.lang.String propertyName,
                                         int defaultValue)
        Retrieves a property as an integer value, returning the default if no value is present.
        Parameters:
        properties - The properties object from which to retrieve the value.
        propertyName - The name of the property to access.
        defaultValue - The value to return if there is no value present.
        Returns:
        The integer value of the named property or, if there is no value or the value does not represent a integer value, the default value.
        See Also:
        Properties.getProperty(java.lang.String)
      • setProperty

        public static void setProperty​(java.util.Properties properties,
                                       java.lang.String propertyName,
                                       int propertyValue)
        Sets an integer property by converting the integer value to a string and storing it in the underlying properties.
        Parameters:
        properties - The properties object in which to set the value.
        propertyName - The name of the property to set.
        propertyValue - The new integer value of the property.
        See Also:
        Properties.setProperty(java.lang.String, java.lang.String)
      • toProperties

        public static java.util.Properties toProperties​(java.util.Map<?,​?> map)
        Converts the given map to a properties object. If the map is already a properties object, it is returned. Otherwise, a new properties object is created and populated with the entries of the given map.
        Parameters:
        map - The map to convert to a properties object.
        Returns:
        A properties object, potentially the same instance, containing entries from the given map.
      • loadPropertiesResource

        public static java.util.Properties loadPropertiesResource​(java.lang.Class<?> objectClass)
                                                           throws java.io.FileNotFoundException,
                                                                  java.io.IOException
        Loads properties for the given class from a properties resource.

        The simple name of the class is used to determine the properties resource, either classSimpleName.properties for the default properties file format or classSimpleName.xml for the XML properties file format. The classSimpleName.properties charset will reflect its BOM, if any; if no BOM is present, the ISO-8859-1 charset will be used as per the properties file format specification.

        The class loader of the given class is used for loading the resource.

        Parameters:
        objectClass - The class in relation to which the properties should be loaded.
        Returns:
        The loaded properties.
        Throws:
        java.io.FileNotFoundException - if no properties resource could be found to load.
        java.io.IOException - if there is an error loading the properties.
        See Also:
        Class.getSimpleName(), ClassLoader.getResourceAsStream(String)
      • loadPropertiesResource

        public static java.util.Properties loadPropertiesResource​(java.lang.Class<?> objectClass,
                                                                  java.lang.String baseName)
                                                           throws java.io.FileNotFoundException,
                                                                  java.io.IOException
        Loads properties for the given class from a properties resource determined from a given base name.

        The base name is used to determine the properties resource, either baseName.properties for the default properties file format or baseName.xml for the XML properties file format. The baseName.properties charset will reflect its BOM, if any; if no BOM is present, the ISO-8859-1 charset will be used as per the properties file format specification.

        The class loader of the given class is used for loading the resource.

        Parameters:
        objectClass - The class in relation to which the properties should be loaded.
        baseName - The base name for determining the properties resource to load.
        Returns:
        The loaded properties.
        Throws:
        java.io.FileNotFoundException - if no properties resource could be found to load.
        java.io.IOException - if there is an error loading the properties.
        See Also:
        ClassLoader.getResourceAsStream(String)