Class PropertiesSingleton
java.lang.Object
java.util.Dictionary<K,V>
java.util.Hashtable<Object,Object>
java.util.Properties
io.github.ajevans.dbcode.utilities.PropertiesSingleton
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<Object,Object>
public final class PropertiesSingleton extends Properties
Singleton to hold all the properties files associated with an application.
Having this as a singleton cuts down on class coupling and makes default properties available in testing.
Set up the singleton by calling addProperties
one or more times
with a properties file. You can then call the standard Properties methods on
its instance, e.g.
String value = PropertiesSingleton.getInstance().getProperty("key");
- Author:
- Andy Evans
- See Also:
Properties
, Serialized Form- Version: 1.0 01 Mar 2021
-
Field Summary
Fields Modifier and Type Field Description private static PropertiesSingleton
instance
Lazy initialization instance object.Fields inherited from class java.util.Properties
defaults
-
Constructor Summary
Constructors Modifier Constructor Description private
PropertiesSingleton()
Default constructor set to private in line with singleton pattern. -
Method Summary
Modifier and Type Method Description void
addProperties(String propertiesFilePath)
Add a file of properties to the singleton.static PropertiesSingleton
getInstance()
Method to get instance in line with singleton pattern.void
setDefaults(Properties defaults)
Sets a default set of properties.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
-
Field Details
-
instance
Lazy initialization instance object.
-
-
Constructor Details
-
PropertiesSingleton
private PropertiesSingleton()Default constructor set to private in line with singleton pattern.
-
-
Method Details
-
getInstance
Method to get instance in line with singleton pattern.- Returns:
- PropertiesSingleton Single instance of this class.
-
addProperties
Add a file of properties to the singleton.- Parameters:
propertiesFilePath
- Properties file to read.- Throws:
IOException
- If the file isn't found, though there is the opportunity to load defaults later throughsetDefaults
.
-
setDefaults
Sets a default set of properties.We can't use the usual way of setting these via the constructor.
- Parameters:
defaults
- Default values to add where missing. If the key already exists, the default will not be used.
-