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.
Path configurationFile = Path.of(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 character propertycharacterValue
(String propertyName, char defaultValue) Creates a value for the given character 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
(Predicate<String> predicate) Returns the values associated with property names fulfilling the given predicatepropertyNames
(Predicate<String> predicate) Returns all property names fulfilling the given predicatestatic PropertyStore
Creates a new empy PropertyStore.static PropertyStore
propertyStore
(InputStream inputStream) Creates a new PropertyStore initialized with the properties found in the given file.static PropertyStore
propertyStore
(Path propertiesFile) 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 which names fulfill the given predicate.void
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
(Path 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:
IllegalStateException
- in case a value has already been created for the given property
-
booleanValue
Creates a value for the given boolean property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
doubleValue
Creates a value for the given double property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
doubleValue
Creates a value for the given double property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
integerValue
Creates a value for the given integer property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
integerValue
Creates a value for the given integer property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
longValue
Creates a value for the given long property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
longValue
Creates a value for the given long property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
characterValue
Creates a value for the given character property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
characterValue
Creates a value for the given character property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
stringValue
Creates a value for the given string property- Parameters:
propertyName
- the property name- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
stringValue
Creates a value for the given string property- Parameters:
propertyName
- the property namedefaultValue
- the default value- Returns:
- a new
PropertyValue
instance - Throws:
IllegalStateException
- in case a value has already been created for the given property
-
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 property names fulfilling the given predicate- Parameters:
predicate
- the predicate for the properties which values to return- Returns:
- all values associated with the properties with the given prefix
-
propertyNames
Returns all property names fulfilling the given predicate- Parameters:
predicate
- the predicate used to filter the property names to return- 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 which names fulfill the given predicate. Note that properties which are value bound cannot be removed.- Parameters:
predicate
- the predicate used to filter the properties to be removed- Throws:
IllegalArgumentException
- in case any of the properties to remove 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
-