Class PropertiesStore

java.lang.Object
org.opentrafficsim.swing.gui.PropertiesStore

public class PropertiesStore extends Object
Class that can be used within a program to load and save properties. This class adheres to the XDG Base Directory Specification regarding where setting files are stored.

Copyright (c) 2026-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Author:
Alexander Verbraeck, Peter Knoppers, Wouter Schakel
See Also:
  • Constructor Details

    • PropertiesStore

      public PropertiesStore(Properties properties, String context, String description)
      Constructor. To populate the default Properties use the various static valueToString methods.
      Parameters:
      properties - properties pre-loaded with defaults
      context - context of the properties, e.g. "appearance" or "editor"
      description - description of the properties, which is saved as a comment in the file that stores the properties
  • Method Details

    • setMaxSubContexts

      public void setMaxSubContexts(int maxSubContexts)
      Sets the maximum number of sub-contexts. The default value is 50.
      Parameters:
      maxSubContexts - maximum number of sub-contexts
    • save

      public void save()
      Saves properties.
    • getProperty

      public String getProperty(String key)
      Returns the property value. If the program has not saved any value, a default value should have been given via the input properties.
      Parameters:
      key - key
      Returns:
      property value
    • getOptionalProperty

      public Optional<String> getOptionalProperty(String key)
      Returns property that might not be given.
      Parameters:
      key - key
      Returns:
      property that might not be given
    • getPropertyOrDefault

      public String getPropertyOrDefault(String key, String defaultValue)
      Returns the property value, or the provided default if there is no value mapped to the key. In the latter case, the default value will be stored as the property value.
      Parameters:
      key - key
      defaultValue - default value
      Returns:
      property value, or the provided default if there is no value mapped to the key
    • setProperty

      public void setProperty(String key, String value, boolean save)
      Sets a property value.
      Parameters:
      key - key
      value - value
      save - whether to save the properties (typically yes, but only on last if multiple properties are set)
    • setProperty

      public void setProperty(String key, String value)
      Sets a property value.
      Parameters:
      key - key
      value - value
    • clearProperty

      public void clearProperty(String key)
      Removes key from the store.
      Parameters:
      key - key
    • key

      public static String key(String key)
      Returns a key that complies to upper/lower case convention.
      Parameters:
      key - key
      Returns:
      key that complies to upper/lower case convention
    • contextKey

      public String contextKey(String key, Object subContext)
      Returns a key specific for the sub-context. The resulting key is context.{hashCode}.{key}, using the hash code of the sub-context. This method also deals with limiting the number of saved sub-contexts.
      Parameters:
      key - key
      subContext - sub-context
      Returns:
      contextual key
    • getList

      public List<String> getList(String key)
      Returns list property.
      Parameters:
      key - key under which list is stored
      Returns:
      list (recent to old)
    • addToList

      public void addToList(String key, String value, int maxNumber)
      Add value to list. If the value is already in the list, it is moved to the front. If the list does not exist it will be created. The resulting list is saved.
      Parameters:
      key - key under which list is stored
      value - value to add to the list
      maxNumber - maximum number of elements in the list
      Throws:
      IllegalArgumentException - when the value contains a '|'
    • removeFromList

      public void removeFromList(String key, String value)
      Remove value from list. The resulting list is saved.
      Parameters:
      key - key
      value - value
    • getColor

      public Color getColor(String key)
      Returns color of given key.
      Parameters:
      key - key
      Returns:
      color
    • getOptionalColor

      public Optional<Color> getOptionalColor(String key)
      Returns color that might not be given for given key.
      Parameters:
      key - key
      Returns:
      color that might not be given
    • getColorOrDefault

      public Color getColorOrDefault(String key, Color defaultValue)
      Returns the property value, or the provided default if there is no value mapped to the key. In the latter case, the default value will be stored as the property value.
      Parameters:
      key - key
      defaultValue - default value
      Returns:
      property value, or the provided default if there is no value mapped to the key
    • stringToColor

      public static Color stringToColor(String colorString)
      Returns color from string.
      Parameters:
      colorString - color as string
      Returns:
      color
    • setColor

      public void setColor(String key, Color color)
      Set color.
      Parameters:
      key - key
      color - color
    • valueToString

      public static String valueToString(Color color)
      Returns string from color.
      Parameters:
      color - color
      Returns:
      string from color
    • getInteger

      public Integer getInteger(String key)
      Returns int for given key.
      Parameters:
      key - key
      Returns:
      int
    • getOptionalInteger

      public Optional<Integer> getOptionalInteger(String key)
      Returns int that might not be given for given key.
      Parameters:
      key - key
      Returns:
      int that might not be given
    • getIntegerOrDefault

      public Integer getIntegerOrDefault(String key, int defaultValue)
      Returns the property value, or the provided default if there is no value mapped to the key. In the latter case, the default value will be stored as the property value.
      Parameters:
      key - key
      defaultValue - default value
      Returns:
      property value, or the provided default if there is no value mapped to the key
    • setInt

      public void setInt(String key, int value)
      Set int value.
      Parameters:
      key - key
      value - value
    • valueToString

      public static String valueToString(int value)
      Converts int to String.
      Parameters:
      value - value
      Returns:
      string
    • getBoolean

      public Boolean getBoolean(String key)
      Returns boolean for given key.
      Parameters:
      key - key
      Returns:
      boolean
    • getOptionalBoolean

      public Optional<Boolean> getOptionalBoolean(String key)
      Returns boolean that might not be given for given key.
      Parameters:
      key - key
      Returns:
      boolean that might not be given
    • getIntegerOrDefault

      public Boolean getIntegerOrDefault(String key, boolean defaultValue)
      Returns the property value, or the provided default if there is no value mapped to the key. In the latter case, the default value will be stored as the property value.
      Parameters:
      key - key
      defaultValue - default value
      Returns:
      property value, or the provided default if there is no value mapped to the key
    • setBoolean

      public void setBoolean(String key, boolean value)
      Set boolean value.
      Parameters:
      key - key
      value - value
    • valueToString

      public static String valueToString(boolean value)
      Converts boolean to String.
      Parameters:
      value - value
      Returns:
      string