Class MapStore

java.lang.Object
org.apache.juneau.commons.settings.MapStore
All Implemented Interfaces:
SettingSource, SettingStore

public class MapStore extends Object implements SettingStore
A writable SettingStore implementation backed by a thread-safe map.

This class provides a mutable store for settings that can be modified at runtime. It's particularly useful for creating custom property stores (e.g., Spring properties, configuration files) that can be added to Settings as sources.

Thread Safety:

This class is thread-safe. The internal map is lazily initialized using AtomicReference and ConcurrentHashMap for thread-safe operations.

Null Value Handling:

Setting a value to null stores Optional.empty() in the map, which means get(String) will return Optional.empty() (not null). This allows you to explicitly override system properties with null values. Use unset(String) if you want to remove a key entirely (so get(String) returns null).

Example:

// Create a store and add properties MapStore store = new MapStore(); store.set("my.property", "value"); store.set("another.property", "another-value"); // Add to Settings as a source (stores can be used as sources) Settings.get().addSource(store); // Override a system property with null store.set("system.property", null); // get() will now return Optional.empty() for "system.property" // Remove a property entirely store.unset("my.property"); // get() will now return null for "my.property"

  • Constructor Details

  • Method Details

    • get

      public Optional<String> get(String key)
      Returns a setting from this store.

      Returns null if the key doesn't exist in the map, or the stored value (which may be Optional.empty() if the value was explicitly set to null).

      Specified by:
      get in interface SettingSource
      Parameters:
      key - The property name.
      Returns:
      The property value, null if the key doesn't exist, or Optional.empty() if the key exists but has a null value.
    • set

      public void set(String key, String value)
      Sets a setting in this store.

      The internal map is lazily initialized on the first call to this method. Setting a value to null stores Optional.empty() in the map, which means get(String) will return Optional.empty() (not null). This allows you to explicitly override system properties with null values.

      Specified by:
      set in interface SettingStore
      Parameters:
      key - The property name.
      value - The property value, or null to set an empty override.
    • clear

      public void clear()
      Clears all entries from this store.

      After calling this method, all keys will be removed from the map, and get(String) will return null for all keys.

      Specified by:
      clear in interface SettingStore
    • unset

      public void unset(String name)
      Removes a setting from this store.

      After calling this method, get(String) will return null for the specified key, indicating that the key doesn't exist in this store (as opposed to returning Optional.empty(), which would indicate the key exists but has a null value).

      Specified by:
      unset in interface SettingStore
      Parameters:
      name - The property name to remove.