Class PropertyStore

java.lang.Object
is.codion.common.property.PropertyStore

public final class PropertyStore extends Object
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"
  • Method Details

    • booleanValue

      public PropertyValue<Boolean> booleanValue(String propertyName)
      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

      public 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:
      IllegalStateException - in case a value has already been created for the given property
    • doubleValue

      public PropertyValue<Double> doubleValue(String propertyName)
      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

      public 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:
      IllegalStateException - in case a value has already been created for the given property
    • integerValue

      public PropertyValue<Integer> integerValue(String propertyName)
      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

      public 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:
      IllegalStateException - in case a value has already been created for the given property
    • longValue

      public PropertyValue<Long> longValue(String propertyName)
      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

      public 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:
      IllegalStateException - in case a value has already been created for the given property
    • characterValue

      public PropertyValue<Character> characterValue(String propertyName)
      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

      public PropertyValue<Character> characterValue(String propertyName, char defaultValue)
      Creates a value for the given character property
      Parameters:
      propertyName - the property name
      defaultValue - the default value
      Returns:
      a new PropertyValue instance
      Throws:
      IllegalStateException - in case a value has already been created for the given property
    • stringValue

      public PropertyValue<String> stringValue(String propertyName)
      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

      public PropertyValue<String> stringValue(String propertyName, @Nullable 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:
      IllegalStateException - in case a value has already been created for the given property
    • enumValue

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

      public <T extends Enum<T>> PropertyValue<T> enumValue(String propertyName, Class<T> enumClass, @Nullable 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

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

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

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

      public <T> PropertyValue<T> value(String propertyName, Function<String,T> decoder, Function<T,String> encoder, @Nullable 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

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

      public void setProperty(String propertyName, @Nullable 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

      public @Nullable 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

      public Collection<String> properties(Predicate<String> predicate)
      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

      public Collection<String> propertyNames(Predicate<String> predicate)
      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

      public 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

      public void removeAll(Predicate<String> predicate)
      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

      public void writeToFile(Path 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

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

      public 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

      public static PropertyStore propertyStore(Path 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

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

      public static String systemProperties()
      Note that class and module paths are displayed as one item per line.
      Returns:
      a String containing all system properties, one per line