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
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceFormats 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 propertybooleancontainsProperty(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 PropertyStoreCreates a new empy PropertyStore.static PropertyStorepropertyStore(File propertiesFile) Creates a new PropertyStore initialized with the properties found in the given file.static PropertyStorepropertyStore(InputStream inputStream) Creates a new PropertyStore initialized with the properties found in the given file.static PropertyStorepropertyStore(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.voidRemoves all properties with the given prefixvoidsetProperty(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 Stringstatic StringsystemProperties(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.voidwriteToFile(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
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
booleanValue
Creates a value for the given boolean property- Parameters:
propertyName- the property namedefaultValue- the default value- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
doubleValue
Creates a value for the given double property- Parameters:
propertyName- the property name- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
doubleValue
Creates a value for the given double property- Parameters:
propertyName- the property namedefaultValue- the default value- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
integerValue
Creates a value for the given integer property- Parameters:
propertyName- the property name- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
integerValue
Creates a value for the given integer property- Parameters:
propertyName- the property namedefaultValue- the default value- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
longValue
Creates a value for the given long property- Parameters:
propertyName- the property name- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
longValue
Creates a value for the given long property- Parameters:
propertyName- the property namedefaultValue- the default value- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
characterValue
Creates a value for the given long property- Parameters:
propertyName- the property name- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
characterValue
Creates a value for the given long property- Parameters:
propertyName- the property namedefaultValue- the default value- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
stringValue
Creates a value for the given string property- Parameters:
propertyName- the property name- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis null
-
stringValue
Creates a value for the given string property- Parameters:
propertyName- the property namedefaultValue- the default value- Returns:
- a new
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameis 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
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameorenumClassis 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
PropertyValueinstance - Throws:
NullPointerException- ifpropertyNameorenumClassis 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
PropertyValueinstance - Throws:
NullPointerException- ifpropertyName,decoderorencoderis 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
PropertyValueinstance - Throws:
NullPointerException- ifpropertyName,decoderorencoderis 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,decoderorencoderis 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,decoderorencoderis 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
-