Class MapStore

java.lang.Object
org.apache.juneau.commons.settings.MapStore
All Implemented Interfaces:
PropertySource, PropertyStore

public class MapStore extends Object implements PropertyStore
A writable PropertyStore 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 a PropertyLookupResult.present(Optional) result with an empty optional. 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 PropertyLookupResult.missing()).

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 present(Optional.empty()) for "system.property" // Remove a property entirely store.unset("my.property"); // get() will now return missing() for "my.property"

  • Constructor Details

  • Method Details

    • get

      Returns a setting from this store.

      Returns PropertyLookupResult.missing() if the key doesn't exist in the map, or a present result (which may contain Optional.empty() if the value was explicitly set to null).

      Specified by:
      get in interface PropertySource
      Parameters:
      key - The property name.
      Returns:
      The property lookup result.
    • 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 a present result with an empty optional. This allows you to explicitly override system properties with null values.

      Specified by:
      set in interface PropertyStore
      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 PropertyLookupResult.missing() for all keys.

      Specified by:
      clear in interface PropertyStore
    • unset

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

      After calling this method, get(String) will return PropertyLookupResult.missing() for the specified key, indicating that the key doesn't exist in this store.

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