Interface FunctionalSource

All Superinterfaces:
SettingSource
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

A functional interface for creating read-only SettingSource instances from a function.

This functional interface allows you to create setting sources directly from lambda expressions or method references, making it easy to wrap existing property sources (e.g., System.getProperty(String), System.getenv(String)) as SettingSource instances.

Functional sources are read-only and do not implement SettingStore. If you need a writable source, use MapStore or FunctionalStore instead.

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

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

Example:

// Create a read-only source directly from a lambda (returns Optional) Settings.get().addSource(name -> opt(System.getProperty(name))); // Using the static factory method (takes Function<String, String>) Settings.get().addSource(FunctionalSource.of(System::getProperty)); // Create a read-only source from System.getenv Settings.get().addSource(FunctionalSource.of(System::getenv)); // Explicit creation for reuse FunctionalSource sysProps = FunctionalSource.of(System::getProperty); Settings.get().addSource(sysProps);

  • Method Summary

    Modifier and Type
    Method
    Description
    get(String name)
    Returns a setting by applying the function.
    of(Function<String,String> function)
    Creates a functional source from a function that returns a string.
  • Method Details

    • get

      Returns a setting by applying the function.

      If the function returns null, this method returns null (indicating the key doesn't exist). If the 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 function returns null.
    • of

      Creates a functional source from a function that returns a string.

      This is a convenience factory method for creating functional sources from functions that return String values. The function's return value is converted to an Optional as follows:

      • If the function returns null, the source returns null (key doesn't exist).
      • If the function returns a non-null value, the source returns Optional.of(value).
      Example:

      // Create from a lambda FunctionalSource source1 = FunctionalSource.of(name -> System.getProperty(name)); // Create from a method reference FunctionalSource source2 = FunctionalSource.of(System::getProperty);

      Parameters:
      function - The function to delegate property lookups to. Must not be null.
      Returns:
      A new functional source instance.