public interface PropertyStore
Provides configuration values which sync with system properties when set. Note that setting the value via
System.setProperty(String, String)
does not affect the property store value, so the value should only be modified via the property store value instance.
If no value is found in a configuration file or in a system property, the default property value is used as the inital value.
When the value is set to null via Value.set(Object)
the default value is used, if one has been specified.
File configurationFile = new File(System.getProperty("user.home") + "/app.properties"); PropertyStore store = PropertyStore.propertyStore(configurationFile); Value<Boolean> featureEnabled = store.booleanValue("feature.enabled", false); Value<String> defaultUsername = store.stringValue("default.username", System.getProperty("user.name")); featureEnabled.set(true); defaultUsername.set("scott"); store.writeToFile(configurationFile); //reverts to the default value featureEnabled.set(null); defaultUsername.set(null); String isFeatureEnabled = System.getProperty("feature.enabled"); // "false"
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Formats a property value, can f.ex. -
Method Summary
Modifier and TypeMethodDescriptionbooleanValue
(String propertyName) Creates a value for the given boolean propertybooleanValue
(String propertyName, boolean defaultValue) Creates a value for the given boolean propertycharacterValue
(String propertyName) Creates a value for the given long propertycharacterValue
(String propertyName, char defaultValue) Creates a value for the given long propertyboolean
containsProperty
(String propertyName) Returns true if this PropertyStore contains a value for the given propertydoubleValue
(String propertyName) Creates a value for the given double propertydoubleValue
(String propertyName, double defaultValue) Creates a value for the given double property<T extends Enum<T>>
PropertyValue<T>Creates a value for the given enum property<T extends Enum<T>>
PropertyValue<T>Creates a value for the given enum propertygetProperty
(String propertyName) Retrieves the value for the given property, null if no value is presentintegerValue
(String propertyName) Creates a value for the given integer propertyintegerValue
(String propertyName, int defaultValue) Creates a value for the given integer property<T> PropertyValue<List<T>>
Creates a value for the given list property.<T> PropertyValue<List<T>>
listValue
(String propertyName, Function<String, T> decoder, Function<T, String> encoder, List<T> defaultValue) Creates a value for the given list property.Creates a value for the given long propertyCreates a value for the given long propertyproperties
(String prefix) Returns the values associated with the properties with the given prefixpropertyNames
(String prefix) Returns all property names with the given prefixstatic PropertyStore
Creates a new empy PropertyStore.static PropertyStore
propertyStore
(File propertiesFile) Creates a new PropertyStore initialized with the properties found in the given file.static PropertyStore
propertyStore
(InputStream inputStream) Creates a new PropertyStore initialized with the properties found in the given file.static PropertyStore
propertyStore
(Properties properties) Creates a new PropertyStore initialized with the given properties.<T> Optional<PropertyValue<T>>
propertyValue
(String propertyName) Returns the Value associated with the given property, an empty Optional if no such Value has been created.void
Removes all properties with the given prefixvoid
setProperty
(String propertyName, String value) Sets the value of the given propertystringValue
(String propertyName) Creates a value for the given string propertystringValue
(String propertyName, String defaultValue) Creates a value for the given string propertystatic String
static String
systemProperties
(PropertyStore.PropertyFormatter propertyFormatter) Returns a String containing all system properties, sorted by name, written by the givenPropertyStore.PropertyFormatter
.<T> PropertyValue<T>
Creates a value representing the given property name.<T> PropertyValue<T>
Creates a value representing the given property name.void
writeToFile
(File propertiesFile) Writes the stored properties to a file
-
Method Details
-
booleanValue
Creates a value for the given boolean property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
booleanValue
Creates a value for the given boolean property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
doubleValue
Creates a value for the given double property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
doubleValue
Creates a value for the given double property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
integerValue
Creates a value for the given integer property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
integerValue
Creates a value for the given integer property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
longValue
Creates a value for the given long property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
longValue
Creates a value for the given long property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
characterValue
Creates a value for the given long property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
characterValue
Creates a value for the given long property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
stringValue
Creates a value for the given string property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
stringValue
Creates a value for the given string property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
is null
-
enumValue
Creates a value for the given enum property- Type Parameters:
T
- the enum type- Parameters:
propertyName
- the property nameenumClass
- the enum class- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
orenumClass
is null
-
enumValue
<T extends Enum<T>> PropertyValue<T> enumValue(String propertyName, Class<T> enumClass, T defaultValue) Creates a value for the given enum property- Type Parameters:
T
- the enum type- Parameters:
propertyName
- the property nameenumClass
- the enum classdefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
orenumClass
is null
-
listValue
<T> PropertyValue<List<T>> listValue(String propertyName, Function<String, T> decoder, Function<T, String> encoder) Creates a value for the given list property. Note that a list property automatically gets anCollections.emptyList()
as its default value.- Type Parameters:
T
- the value type- Parameters:
propertyName
- the property namedecoder
- a decoder for decoding the value from a stringencoder
- an encoder for encoding the value to a string- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
,decoder
orencoder
is null
-
listValue
<T> PropertyValue<List<T>> listValue(String propertyName, Function<String, T> decoder, Function<T, String> encoder, List<T> defaultValue) Creates a value for the given list property. Note that a list property automatically gets anCollections.emptyList()
as its default value.- Type Parameters:
T
- the value type- Parameters:
propertyName
- the property namedecoder
- a decoder for decoding the value from a stringencoder
- an encoder for encoding the value to a stringdefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
NullPointerException
- ifpropertyName
,decoder
orencoder
is null
-
value
<T> PropertyValue<T> value(String propertyName, Function<String, T> decoder, Function<T, String> encoder) Creates a value representing the given property name.- Type Parameters:
T
- the value type- Parameters:
propertyName
- the configuration property name identifying this valuedecoder
- a decoder for decoding the value from a stringencoder
- an encoder for encoding the value to a string- Returns:
- the configuration value
- Throws:
NullPointerException
- ifpropertyName
,decoder
orencoder
is null
-
value
<T> PropertyValue<T> value(String propertyName, Function<String, T> decoder, Function<T, String> encoder, T defaultValue) Creates a value representing the given property name.- Type Parameters:
T
- the value type- Parameters:
propertyName
- the configuration property name identifying this valuedecoder
- a decoder for decoding the value from a stringencoder
- an encoder for encoding the value to a stringdefaultValue
- the default value- Returns:
- the configuration value
- Throws:
NullPointerException
- ifpropertyName
,decoder
orencoder
is null
-
propertyValue
Returns the Value associated with the given property, an empty Optional if no such Value has been created.- Type Parameters:
T
- the value type- Parameters:
propertyName
- the property name- Returns:
- the configuration value for the given name or an empty Optional if none exists
-
setProperty
Sets the value of the given property- Parameters:
propertyName
- the property namevalue
- the value- Throws:
IllegalArgumentException
- if the property is value bound
-
getProperty
Retrieves the value for the given property, null if no value is present- Parameters:
propertyName
- the property name- Returns:
- the value or null if no value is present
-
properties
Returns the values associated with the properties with the given prefix- Parameters:
prefix
- the property prefix- Returns:
- all values associated with the properties with the given prefix
-
propertyNames
Returns all property names with the given prefix- Parameters:
prefix
- the property name prefix- Returns:
- all property names with the given prefix
-
containsProperty
Returns true if this PropertyStore contains a value for the given property- Parameters:
propertyName
- the property- Returns:
- true if a value for the given property exists
-
removeAll
Removes all properties with the given prefix- Parameters:
prefix
- the prefix- Throws:
IllegalArgumentException
- in case any of the properties with the given prefix are value bound
-
writeToFile
Writes the stored properties to a file- Parameters:
propertiesFile
- the properties file to write to- Throws:
IOException
- in case writing the file was not successful
-
propertyStore
Creates a new empy PropertyStore.- Returns:
- a new empty PropertyStore instance
-
propertyStore
Creates a new PropertyStore initialized with the properties found in the given file.- Parameters:
inputStream
- the input stream to read from- Returns:
- a new PropertyStore
- Throws:
IOException
- in case the given input stream could not be read
-
propertyStore
Creates a new PropertyStore initialized with the properties found in the given file.- Parameters:
propertiesFile
- the file to read from initially- Returns:
- a new PropertyStore
- Throws:
IOException
- in case the given properties file exists but reading it failedFileNotFoundException
- in case the file does not exist
-
propertyStore
Creates a new PropertyStore initialized with the given properties.- Parameters:
properties
- the initial properties- Returns:
- a new PropertyStore
-
systemProperties
- Returns:
- a String containing all system properties, one per line
-
systemProperties
Returns a String containing all system properties, sorted by name, written by the givenPropertyStore.PropertyFormatter
.- Parameters:
propertyFormatter
- for specific property formatting or exclusions- Returns:
- a String containing all system properties, one per line
-