Class FunctionalStore

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

public class FunctionalStore extends Object implements SettingStore
A writable SettingStore implementation created from functional interfaces.

This class allows you to create writable setting stores from lambda expressions or method references, making it easy to wrap existing property systems (e.g., custom configuration systems) as SettingStore instances.

Return Value Semantics:
  • If the reader function returns null, this store returns null (key doesn't exist).
  • If the reader function returns a non-null value, this store returns Optional.of(value).

Note: This store cannot distinguish between a key that doesn't exist and a key that exists with a null value, since the reader function only returns a String. If you need to distinguish these cases, use MapStore instead.

Example:

// Create a writable functional store FunctionalStore store = FunctionalStore.of( System::getProperty, // reader (k, v) -> System.setProperty(k, v), // writer k -> System.clearProperty(k), // unset () -> { // clear // Clear all properties logic } ); // Use it store.set("my.property", "value"); Optional<String> value = store.get("my.property"); store.unset("my.property");

  • Constructor Details

    • FunctionalStore

      public FunctionalStore(Function<String,String> reader, BiConsumer<String,String> writer, Consumer<String> unsetter, Snippet clearer)
      Creates a new writable functional store.
      Parameters:
      reader - The function to read property values. Must not be null.
      writer - The function to write property values. Must not be null.
      unsetter - The function to remove property values. Must not be null.
      clearer - The snippet to clear all property values. Must not be null.
  • Method Details

    • get

      public Optional<String> get(String name)
      Returns a setting by applying the reader function.

      If the reader function returns null, this method returns null (indicating the key doesn't exist). If the reader function returns a non-null value, this method returns Optional.of(value).

      Specified by:
      get in interface SettingSource
      Parameters:
      name - The property name.
      Returns:
      The property value, or null if the reader function returns null.
    • set

      public void set(String name, String value)
      Sets a setting by applying the writer function.
      Specified by:
      set in interface SettingStore
      Parameters:
      name - The property name.
      value - The property value, or null to set an empty override.
    • unset

      public void unset(String name)
      Removes a setting by applying the unsetter function.
      Specified by:
      unset in interface SettingStore
      Parameters:
      name - The property name to remove.
    • clear

      public void clear()
      Clears all settings by invoking the clearer snippet.

      If the clearer snippet throws an exception, it will be wrapped in a RuntimeException.

      Specified by:
      clear in interface SettingStore
    • of

      public static FunctionalStore of(Function<String,String> reader, BiConsumer<String,String> writer, Consumer<String> unsetter, Snippet clearer)
      Creates a writable functional store from four functions.

      This is a convenience factory method for creating writable functional stores.

      Example:

      // Create from lambdas FunctionalStore store = FunctionalStore.of( System::getProperty, (k, v) -> System.setProperty(k, v), k -> System.clearProperty(k), () -> { // Clear all properties } );

      Parameters:
      reader - The function to read property values. Must not be null.
      writer - The function to write property values. Must not be null.
      unsetter - The function to remove property values. Must not be null.
      clearer - The snippet to clear all property values. Must not be null.
      Returns:
      A new writable functional store instance.