Class Properties

  • All Implemented Interfaces:
    Serializable, Cloneable, Map<Object,​Object>

    public final class Properties
    extends Properties
    Properties extends java.util.Properties to include:
    • Loading properties from a named file and returning a net.sf.eBus.util.Properties object.
    • Storing a Properties object into a named file.
    • Returning property values as a boolean, int, double and String array.
    • Setting property values as a boolean, int, double and String array.
    • Informs registered PropertiesListeners when the underlying properties file has changed and been automatically reloaded.

      Note: automatic properties file reloading is done only if there is a registered properties listener. If there are no registered listeners, then Properties does not watch the underlying file for changes and so does not automatically reload the properties file if it should change.

    Author:
    Charles Rapp
    See Also:
    Serialized Form
    • Constructor Detail

      • Properties

        public Properties()
        Creates an empty property list with no default values. Because there is no associated file, property listening is not allowed.
      • Properties

        public Properties​(Properties defaults)
        Creates an empty property list with default values. Because there is no associated file, property listening is not allowed.
        Parameters:
        defaults - the default property values.
    • Method Detail

      • getBooleanProperty

        public boolean getBooleanProperty​(String key)
        Returns the named property value as a boolean. If key is an unknown property or is neither "true" nor "false" then returns false.
        Parameters:
        key - The property key.
        Returns:
        the named property value as a boolean. If the value is "true", then true is returned, otherwise returns false.
        See Also:
        getBooleanProperty(String, boolean), setBooleanProperty(String, boolean)
      • getIntProperty

        public int getIntProperty​(String key)
        Returns the named property value as an int. If key is an unknown property or is not a valid integer, then returns 0.
        Parameters:
        key - The property key.
        Returns:
        the named property value as an int.
        See Also:
        getIntProperty(String, int), setIntProperty(String, int)
      • getArrayProperty

        public String[] getArrayProperty​(String key,
                                         char ifs)
        Returns the named property value as a String[]. ifs is used as the interfield separator character. If the property value does not exist, then returns an empty array.
        Parameters:
        key - The property key.
        ifs - The interfield separator.
        Returns:
        the named property value as a String[]. If the property value does not exist, then returns an empty array.
        See Also:
        setArrayProperty(String, String[], char)
      • getBooleanProperty

        public boolean getBooleanProperty​(String key,
                                          boolean defaultValue)
        Returns the named property value as a boolean. If key is an unknown property or an invalid boolean string, then returns the default value.
        Parameters:
        key - The property key.
        defaultValue - The default value.
        Returns:
        the named property value as a boolean. If key is an unknown property or an invalid boolean string, then returns defaultValue.
        See Also:
        getBooleanProperty(String), setBooleanProperty(String, boolean)
      • getIntProperty

        public int getIntProperty​(String key,
                                  int defaultValue)
        Returns the named property value as an int. If either the property does not exist or does exist but is not an integer, then returns the default value.
        Parameters:
        key - The property key.
        defaultValue - The default value.
        Returns:
        the named property value as an int or defaultValue.
        See Also:
        getIntProperty(String), setIntProperty(String, int)
      • getDoubleProperty

        public double getDoubleProperty​(String key,
                                        double defaultValue)
        Returns the named property value as a double. If the property value does not exist or is not a valid double, then returns the default value.
        Parameters:
        key - The property key.
        defaultValue - The default value.
        Returns:
        the named property value as a double. If the property value does not exist or is not a valid double, then returns defaultValue.
        See Also:
        getDoubleProperty(String), setDoubleProperty(String, double)
      • stringPropertyNames

        public Set<String> stringPropertyNames​(Pattern p)
        Returns a set of keys in this property list whose key matches the given regular expression pattern p and the corresponding values are strings. Includes distinct keys in the default property list if a key of the same name is not in the main properties list. Properties whose key or value is not of type String are omitted.

        The returned set is not backed by the Properties object. Changes to this Properties are not reflected in the set or vice versa.

        Parameters:
        p - match property keys against this pattern.
        Returns:
        String property keys matching the regular expression pattern.
        See Also:
        Properties.stringPropertyNames(), Properties.defaults
      • setArrayProperty

        public void setArrayProperty​(String key,
                                     String[] value,
                                     char ifs)
        Sets the named property value to the string array. The array is converted into a single string by concatenating the strings together separated by ifs.
        Parameters:
        key - The property key.
        value - The string array.
        ifs - The interfield separator.
        See Also:
        getArrayProperty(java.lang.String, char)
      • addListener

        public void addListener​(PropertiesListener listener)
        Adds a properties listener. If this is the first listener and the watchRate > zero, then starts the watch timer.

        Note: when the underlying properties file changes, it will be automatically reloaded prior to calling back to the registered PropertiesListeners.

        Parameters:
        listener - Add this properties listener.
        Throws:
        IllegalArgumentException - if listener is null.
        IllegalStateException - if this is no underlying property file.
      • removeListener

        public void removeListener​(PropertiesListener listener)
        Removes a properties listener. If there are no more listeners and the watch timer is running, then the timer is canceled.

        Note: when the watch timer is canceled, Properties will no longer determine if the underlying properties file has changed and so will not automatically reload said file if it should change.

        Parameters:
        listener - Remove this listener.
        Throws:
        IllegalStateException - if this is no underlying property file.
      • loadProperties

        public static Properties loadProperties​(String fileName)
                                         throws IOException
        Returns a properties list loaded with the values found in the named properties file. If the file does not exist, an empty properties object is returned. This allows new properties to be created and stored.
        Parameters:
        fileName - the properties file name.
        Returns:
        A properties list.
        Throws:
        IllegalArgumentException - if fileName is either null or an empty string.
        IOException - if fileName is not a valid properties file.
        See Also:
        loadProperties(File), load(), store(String)
      • loadProperties

        public static Properties loadProperties​(File file)
                                         throws IOException
        Returns a properties list loaded with the values found in the properties file. If the file does not exist, an empty properties object is returned. This allows new properties to be created and stored.
        Parameters:
        file - the properties file object.
        Returns:
        A properties list.
        Throws:
        IllegalArgumentException - if file is null.
        IOException - if file is not a valid properties file.
        See Also:
        loadProperties(String), load(), store(String)