Class FunctionalStore
- All Implemented Interfaces:
SettingSource,SettingStore
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 returnsnull (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 MapStore instead.
Example:
-
Constructor Summary
ConstructorsConstructorDescriptionFunctionalStore(Function<String, String> reader, BiConsumer<String, String> writer, Consumer<String> unsetter, Snippet clearer) Creates a new writable functional store. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all settings by invoking the clearer snippet.Returns a setting by applying the reader function.static FunctionalStoreof(Function<String, String> reader, BiConsumer<String, String> writer, Consumer<String> unsetter, Snippet clearer) Creates a writable functional store from four functions.voidSets a setting by applying the writer function.voidRemoves a setting by applying the unsetter function.
-
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 benull .writer- The function to write property values. Must not benull .unsetter- The function to remove property values. Must not benull .clearer- The snippet to clear all property values. Must not benull .
-
-
Method Details
-
get
Returns a setting by applying the reader function.If the reader function returns
null , this method returnsnull (indicating the key doesn't exist). If the reader function returns a non-null value, this method returnsOptional.of(value) .- Specified by:
getin interfaceSettingSource- Parameters:
name- The property name.- Returns:
- The property value, or
null if the reader function returnsnull .
-
set
Sets a setting by applying the writer function.- Specified by:
setin interfaceSettingStore- Parameters:
name- The property name.value- The property value, ornull to set an empty override.
-
unset
Removes a setting by applying the unsetter function.- Specified by:
unsetin interfaceSettingStore- Parameters:
name- The property name to remove.
-
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:
clearin interfaceSettingStore
-
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 FunctionalStorestore = 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 benull .writer- The function to write property values. Must not benull .unsetter- The function to remove property values. Must not benull .clearer- The snippet to clear all property values. Must not benull .- Returns:
- A new writable functional store instance.
-