Interface PropertyStore


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"
 
  • Method Details

    • booleanValue

      PropertyValue<Boolean> booleanValue(String propertyName)
      Creates a value for the given boolean property
      Parameters:
      propertyName - the property name
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • booleanValue

      PropertyValue<Boolean> booleanValue(String propertyName, boolean defaultValue)
      Creates a value for the given boolean property
      Parameters:
      propertyName - the property name
      defaultValue - the default value
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • doubleValue

      PropertyValue<Double> doubleValue(String propertyName)
      Creates a value for the given double property
      Parameters:
      propertyName - the property name
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • doubleValue

      PropertyValue<Double> doubleValue(String propertyName, double defaultValue)
      Creates a value for the given double property
      Parameters:
      propertyName - the property name
      defaultValue - the default value
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • integerValue

      PropertyValue<Integer> integerValue(String propertyName)
      Creates a value for the given integer property
      Parameters:
      propertyName - the property name
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • integerValue

      PropertyValue<Integer> integerValue(String propertyName, int defaultValue)
      Creates a value for the given integer property
      Parameters:
      propertyName - the property name
      defaultValue - the default value
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • longValue

      PropertyValue<Long> longValue(String propertyName)
      Creates a value for the given long property
      Parameters:
      propertyName - the property name
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • longValue

      PropertyValue<Long> longValue(String propertyName, long defaultValue)
      Creates a value for the given long property
      Parameters:
      propertyName - the property name
      defaultValue - the default value
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • characterValue

      PropertyValue<Character> characterValue(String propertyName)
      Creates a value for the given long property
      Parameters:
      propertyName - the property name
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • characterValue

      PropertyValue<Character> characterValue(String propertyName, char defaultValue)
      Creates a value for the given long property
      Parameters:
      propertyName - the property name
      defaultValue - the default value
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • stringValue

      PropertyValue<String> stringValue(String propertyName)
      Creates a value for the given string property
      Parameters:
      propertyName - the property name
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • stringValue

      PropertyValue<String> stringValue(String propertyName, String defaultValue)
      Creates a value for the given string property
      Parameters:
      propertyName - the property name
      defaultValue - the default value
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName is null
    • enumValue

      <T extends Enum<T>> PropertyValue<T> enumValue(String propertyName, Class<T> enumClass)
      Creates a value for the given enum property
      Type Parameters:
      T - the enum type
      Parameters:
      propertyName - the property name
      enumClass - the enum class
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName or enumClass 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 name
      enumClass - the enum class
      defaultValue - the default value
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName or enumClass 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 an Collections.emptyList() as its default value.
      Type Parameters:
      T - the value type
      Parameters:
      propertyName - the property name
      decoder - a decoder for decoding the value from a string
      encoder - an encoder for encoding the value to a string
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName, decoder or encoder 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 an Collections.emptyList() as its default value.
      Type Parameters:
      T - the value type
      Parameters:
      propertyName - the property name
      decoder - a decoder for decoding the value from a string
      encoder - an encoder for encoding the value to a string
      defaultValue - the default value
      Returns:
      a new PropertyValue instance
      Throws:
      NullPointerException - if propertyName, decoder or encoder 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 value
      decoder - a decoder for decoding the value from a string
      encoder - an encoder for encoding the value to a string
      Returns:
      the configuration value
      Throws:
      NullPointerException - if propertyName, decoder or encoder 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 value
      decoder - a decoder for decoding the value from a string
      encoder - an encoder for encoding the value to a string
      defaultValue - the default value
      Returns:
      the configuration value
      Throws:
      NullPointerException - if propertyName, decoder or encoder is null
    • propertyValue

      <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.
      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

      void setProperty(String propertyName, String value)
      Sets the value of the given property
      Parameters:
      propertyName - the property name
      value - the value
      Throws:
      IllegalArgumentException - if the property is value bound
    • getProperty

      String getProperty(String propertyName)
      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

      Collection<String> properties(String prefix)
      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

      Collection<String> propertyNames(String prefix)
      Returns all property names with the given prefix
      Parameters:
      prefix - the property name prefix
      Returns:
      all property names with the given prefix
    • containsProperty

      boolean containsProperty(String propertyName)
      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

      void removeAll(String prefix)
      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

      void writeToFile(File propertiesFile) throws IOException
      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

      static PropertyStore propertyStore()
      Creates a new empy PropertyStore.
      Returns:
      a new empty PropertyStore instance
    • propertyStore

      static PropertyStore propertyStore(InputStream inputStream) throws IOException
      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

      static PropertyStore propertyStore(File propertiesFile) throws IOException
      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 failed
      FileNotFoundException - in case the file does not exist
    • propertyStore

      static PropertyStore propertyStore(Properties properties)
      Creates a new PropertyStore initialized with the given properties.
      Parameters:
      properties - the initial properties
      Returns:
      a new PropertyStore
    • systemProperties

      static String systemProperties()
      Returns:
      a String containing all system properties, one per line
    • systemProperties

      static String systemProperties(PropertyStore.PropertyFormatter propertyFormatter)
      Returns a String containing all system properties, sorted by name, written by the given PropertyStore.PropertyFormatter.
      Parameters:
      propertyFormatter - for specific property formatting or exclusions
      Returns:
      a String containing all system properties, one per line