Class Properties
- java.lang.Object
-
- java.util.Dictionary<K,V>
-
- java.util.Hashtable<Object,Object>
-
- java.util.Properties
-
- net.sf.eBus.util.Properties
-
- All Implemented Interfaces:
Serializable,Cloneable,Map<Object,Object>
public final class Properties extends Properties
Propertiesextendsjava.util.Propertiesto include:-
Loading properties from a named file and returning
a
net.sf.eBus.util.Propertiesobject. -
Storing a
Propertiesobject 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
-
-
Field Summary
-
Fields inherited from class java.util.Properties
defaults
-
-
Constructor Summary
Constructors Constructor Description Properties()Creates an empty property list with no default values.Properties(Properties defaults)Creates an empty property list with default values.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(PropertiesListener listener)Adds a properties listener.String[]getArrayProperty(String key, char ifs)Returns the named property value as aString[].booleangetBooleanProperty(String key)Returns the named property value as aboolean.booleangetBooleanProperty(String key, boolean defaultValue)Returns the named property value as aboolean.doublegetDoubleProperty(String key)Returns the named property value as adouble.doublegetDoubleProperty(String key, double defaultValue)Returns the named property value as adouble.intgetIntProperty(String key)Returns the named property value as anint.intgetIntProperty(String key, int defaultValue)Returns the named property value as anint.voidload()Reloads properties from the properties file.static PropertiesloadProperties(File file)Returns a properties list loaded with the values found in the properties file.static PropertiesloadProperties(String fileName)Returns a properties list loaded with the values found in the named properties file.voidremoveListener(PropertiesListener listener)Removes a properties listener.voidsetArrayProperty(String key, String[] value, char ifs)Sets the named property value to the string array.voidsetBooleanProperty(String key, boolean value)Sets the named property value to the specified boolean.voidsetDoubleProperty(String key, double value)Sets the named property value to the specified double.voidsetIntProperty(String key, int value)Sets the named property value to the specified integer.voidstore(File file, String header)Stores properties in the named properties file using the provided header comment.voidstore(String header)Stores properties in properties file using the provided header comment.voidstore(String fileName, String header)Stores properties in the named properties file using the provided header comment.Set<String>stringPropertyNames(Pattern p)Returns a set of keys in this property list whose key matches the given regular expression patternpand the corresponding values are strings.-
Methods inherited from class java.util.Properties
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, getProperty, getProperty, hashCode, isEmpty, keys, keySet, list, list, load, load, loadFromXML, merge, propertyNames, put, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, save, setProperty, size, store, store, storeToXML, storeToXML, storeToXML, stringPropertyNames, toString, values
-
-
-
-
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 aboolean. Ifkeyis an unknown property or is neither"true"nor"false"then returnsfalse.- Parameters:
key- The property key.- Returns:
- the named property value as a
boolean. If the value is"true", thentrueis returned, otherwise returnsfalse. - See Also:
getBooleanProperty(String, boolean),setBooleanProperty(String, boolean)
-
getIntProperty
public int getIntProperty(String key)
Returns the named property value as anint. Ifkeyis an unknown property or is not a valid integer, then returns0.- Parameters:
key- The property key.- Returns:
- the named property value as an
int. - See Also:
getIntProperty(String, int),setIntProperty(String, int)
-
getDoubleProperty
public double getDoubleProperty(String key)
Returns the named property value as adouble. Ifkeyis an unknown property or is not a value double, then returns0.0.- Parameters:
key- The property key.- Returns:
- the named property value as a
double. - Throws:
NumberFormatException- if the property value is not a valid double.- See Also:
getDoubleProperty(String, double),setDoubleProperty(String, double)
-
getArrayProperty
public String[] getArrayProperty(String key, char ifs)
Returns the named property value as aString[].ifsis 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 aboolean. Ifkeyis 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. Ifkeyis an unknown property or an invalid boolean string, then returnsdefaultValue. - See Also:
getBooleanProperty(String),setBooleanProperty(String, boolean)
-
getIntProperty
public int getIntProperty(String key, int defaultValue)
Returns the named property value as anint. 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
intordefaultValue. - See Also:
getIntProperty(String),setIntProperty(String, int)
-
getDoubleProperty
public double getDoubleProperty(String key, double defaultValue)
Returns the named property value as adouble. If the property value does not exist or is not a validdouble, 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 validdouble, then returnsdefaultValue. - 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 patternpand 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 typeStringare omitted.The returned set is not backed by the
Propertiesobject. Changes tothis Propertiesare 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
-
setBooleanProperty
public void setBooleanProperty(String key, boolean value)
Sets the named property value to the specified boolean.- Parameters:
key- The property key.value- The boolean value.- See Also:
getBooleanProperty(String),getBooleanProperty(String, boolean)
-
setIntProperty
public void setIntProperty(String key, int value)
Sets the named property value to the specified integer.- Parameters:
key- The property key.value- The integer value.- See Also:
getIntProperty(String),getIntProperty(String, int)
-
setDoubleProperty
public void setDoubleProperty(String key, double value)
Sets the named property value to the specified double.- Parameters:
key- The property key.value- The double value.- See Also:
getDoubleProperty(String),getDoubleProperty(String, double)
-
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 byifs.- Parameters:
key- The property key.value- The string array.ifs- The interfield separator.- See Also:
getArrayProperty(java.lang.String, char)
-
load
public void load() throws IOExceptionReloads properties from the properties file.- Throws:
IOException- if there are errors reading in the properties file.- See Also:
store(String),loadProperties(String),loadProperties(File)
-
store
public void store(String header) throws IOException
Stores properties in properties file using the provided header comment. The header comment is placed at the property file's start.- Parameters:
header- File header comment. May benull.- Throws:
FileNotFoundException- if the properties file could not be created.IOException- if there is an error storing the properties into the file.- See Also:
load(),loadProperties(String),loadProperties(File)
-
store
public void store(String fileName, String header) throws IOException
Stores properties in the named properties file using the provided header comment. The header comment is placed at the property file's start.- Parameters:
fileName- Property file's name.header- File header comment. May benull.- Throws:
IllegalArgumentException- iffileNameis eithernullor an empty string.FileNotFoundException- if the properties file could not be created.IOException- if there is an error storing the properties into the file.- See Also:
store(String),store(File, String),load(),loadProperties(String),loadProperties(File)
-
store
public void store(File file, String header) throws IOException
Stores properties in the named properties file using the provided header comment. The header comment is placed at the property file's start.- Parameters:
file- Property file.header- File header comment. May benull.- Throws:
IllegalArgumentException- iffileisnull.FileNotFoundException- if the properties file could not be created.IOException- if there is an error storing the properties into the file.- See Also:
store(String),store(String, String),load(),loadProperties(String),loadProperties(File)
-
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- iflistenerisnull.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- iffileNameis eithernullor an empty string.IOException- iffileNameis 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- iffileisnull.IOException- iffileis not a valid properties file.- See Also:
loadProperties(String),load(),store(String)
-
-