public final class PropertyStore
extends java.lang.Object
File configurationFile = new File(System.getProperty("user.home") + "/app.properties"); PropertyStore store = new PropertyStore(configurationFile); Value<Boolean> featureEnabled = store.propertyValue("feature.enabled", false); Value<String> defaultUsername = store.propertyValue("default.username", System.getProperty("user.name")); featureEnabled.set(true); defaultUsername.set("scott"); store.writeToFile(configurationFile);
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
VALUE_SEPARATOR
The separator used to separate multiple values.
|
Constructor and Description |
---|
PropertyStore(java.io.File propertiesFile)
Instantiates a new PropertyStore backed by the given file.
|
PropertyStore(java.util.Properties properties)
Instantiates a new PropertyStore initialized with the given properties.
|
Modifier and Type | Method and Description |
---|---|
boolean |
containsProperty(java.lang.String property)
Returns true if this PropertyStore contains a value for the given property
|
java.util.List<java.lang.String> |
getProperties(java.lang.String prefix)
Returns the values associated with the properties with the given prefix
|
java.lang.String |
getProperty(java.lang.String property)
Retrieves the value for the given property, null if no value is present
|
java.util.List<java.lang.String> |
getPropertyNames(java.lang.String prefix)
Returns all property names with the given prefix
|
<V> PropertyValue<V> |
getPropertyValue(java.lang.String property)
Returns the Value associated with the given property, null if none has been created.
|
<V> PropertyValue<java.util.List<V>> |
propertyListValue(java.lang.String property,
java.util.List<V> defaultValue,
java.util.function.Function<java.lang.String,V> decoder,
java.util.function.Function<V,java.lang.String> encoder)
Instantiates a Value representing the given property.
|
PropertyValue<java.lang.Boolean> |
propertyValue(java.lang.String property,
java.lang.Boolean defaultValue)
Instantiates a Value representing the given property.
|
PropertyValue<java.lang.Double> |
propertyValue(java.lang.String property,
java.lang.Double defaultValue)
Instantiates a Value representing the given property.
|
PropertyValue<java.lang.Integer> |
propertyValue(java.lang.String property,
java.lang.Integer defaultValue)
Instantiates a Value representing the given property.
|
PropertyValue<java.lang.String> |
propertyValue(java.lang.String property,
java.lang.String defaultValue)
Instantiates a Value representing the given property.
|
<V> PropertyValue<V> |
propertyValue(java.lang.String property,
V defaultValue,
java.util.function.Function<java.lang.String,V> decoder,
java.util.function.Function<V,java.lang.String> encoder)
Instantiates a Value representing the given property.
|
static java.util.Properties |
readFromFile(java.io.File propertiesFile)
Reads all properties from the given properties file if it exists
|
void |
removeAll(java.lang.String prefix)
Removes all properties with the given prefix
|
void |
setProperty(java.lang.String property,
java.lang.String value)
Sets the value of the given property
|
void |
writeToFile(java.io.File propertiesFile)
Writes the stored properties to a file
|
public static final java.lang.String VALUE_SEPARATOR
public PropertyStore(java.io.File propertiesFile) throws java.io.IOException
propertiesFile
- the file to read from initiallyjava.io.IOException
- in case the given properties file exists but reading it failedpublic PropertyStore(java.util.Properties properties)
properties
- the initial propertiespublic PropertyValue<java.lang.Boolean> propertyValue(java.lang.String property, java.lang.Boolean defaultValue)
property
- the configuration property identifying this valuedefaultValue
- the default value to use if no value is present and when the value is set to nulljava.lang.NullPointerException
- if property
is nulljava.lang.IllegalArgumentException
- in case a Value for the given property has already been createdpublic PropertyValue<java.lang.String> propertyValue(java.lang.String property, java.lang.String defaultValue)
property
- the configuration property identifying this valuedefaultValue
- the default value to use if no value is present and when the value is set to nulljava.lang.NullPointerException
- if property
is nulljava.lang.IllegalArgumentException
- in case a Value for the given property has already been createdpublic PropertyValue<java.lang.Integer> propertyValue(java.lang.String property, java.lang.Integer defaultValue)
property
- the configuration property identifying this valuedefaultValue
- the default value to use if no value is present and when the value is set to nulljava.lang.NullPointerException
- if property
is nulljava.lang.IllegalArgumentException
- in case a Value for the given property has already been createdpublic PropertyValue<java.lang.Double> propertyValue(java.lang.String property, java.lang.Double defaultValue)
property
- the configuration property identifying this valuedefaultValue
- the default value to use if no value is present and when the value is set to nulljava.lang.NullPointerException
- if property
is nulljava.lang.IllegalArgumentException
- in case a Value for the given property has already been createdpublic <V> PropertyValue<V> propertyValue(java.lang.String property, V defaultValue, java.util.function.Function<java.lang.String,V> decoder, java.util.function.Function<V,java.lang.String> encoder)
V
- the value typeproperty
- the configuration property identifying this valuedefaultValue
- the default value to use if no initial value is presentdecoder
- a decoder for decoding the value from a stringencoder
- an encoder for encoding the value to a stringjava.lang.NullPointerException
- if property
, decoder
or encoder
is nulljava.lang.IllegalArgumentException
- in case a Value for the given property has already been createdpublic <V> PropertyValue<java.util.List<V>> propertyListValue(java.lang.String property, java.util.List<V> defaultValue, java.util.function.Function<java.lang.String,V> decoder, java.util.function.Function<V,java.lang.String> encoder)
V
- the value typeproperty
- the configuration property identifying this valuedefaultValue
- the default value to use if no initial value is presentdecoder
- a decoder for decoding the value from a stringencoder
- an encoder for encoding the value to a stringjava.lang.NullPointerException
- if property
, decoder
or encoder
is nulljava.lang.IllegalArgumentException
- in case a Value for the given property has already been createdpublic <V> PropertyValue<V> getPropertyValue(java.lang.String property)
V
- the value typeproperty
- the propertypublic void setProperty(java.lang.String property, java.lang.String value)
property
- the propertyvalue
- the valuejava.lang.IllegalArgumentException
- in the property is value boundpublic java.lang.String getProperty(java.lang.String property)
property
- the propertypublic java.util.List<java.lang.String> getProperties(java.lang.String prefix)
prefix
- the property prefixpublic java.util.List<java.lang.String> getPropertyNames(java.lang.String prefix)
prefix
- the property name prefixpublic boolean containsProperty(java.lang.String property)
property
- the propertypublic void removeAll(java.lang.String prefix)
prefix
- the prefixjava.lang.IllegalArgumentException
- in case any of the properties with the given prefix are value boundpublic void writeToFile(java.io.File propertiesFile) throws java.io.IOException
propertiesFile
- the properties file to write tojava.io.IOException
- in case writing the file was not successfulpublic static java.util.Properties readFromFile(java.io.File propertiesFile) throws java.io.IOException
propertiesFile
- the properties file to read fromjava.io.IOException
- in case the file exists but can not be read