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 Details

  • Constructor Details

    • PropertiesSingleton

      private PropertiesSingleton()
      Default constructor set to private in line with singleton pattern.
  • Method Details

    • getInstance

      public static PropertiesSingleton getInstance()
      Method to get instance in line with singleton pattern.
      Returns:
      PropertiesSingleton Single instance of this class.
    • addProperties

      public void addProperties​(String propertiesFilePath) throws IOException
      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 through setDefaults.
    • setDefaults

      public void setDefaults​(Properties defaults)
      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.