Class ConfigurationLoader.Builder

java.lang.Object
de.bsommerfeld.jshepherd.core.ConfigurationLoader.Builder
Enclosing class:
ConfigurationLoader

public static final class ConfigurationLoader.Builder extends Object
Fluent builder for constructing and loading configuration.
  • Method Details

    • withComments

      public ConfigurationLoader.Builder withComments()
      Enables comment preservation and generation. This is the default behavior.
    • withoutComments

      public ConfigurationLoader.Builder withoutComments()
      Disables comment handling for faster, simpler serialization.
    • withAutoReload

      public ConfigurationLoader.Builder withAutoReload()
      Enables automatic reloading: the configuration file is watched on a background daemon thread and the POJO is reloaded whenever the file changes on disk. Polls once per second.

      Use ConfigurablePojo.setOnAutoReload(Runnable) to get notified after a reload and ConfigurablePojo.stopAutoReload() to stop watching.

    • withAutoReload

      public ConfigurationLoader.Builder withAutoReload(Duration pollInterval)
      Enables automatic reloading with a custom poll interval.
      Parameters:
      pollInterval - how often to check the file for changes; must be positive
    • load

      public <T extends ConfigurablePojo<T>> T load(Supplier<T> defaultPojoSupplier)
      Loads the configuration using the specified default POJO supplier. If the file exists, values are loaded from it. Otherwise, a new file is created with default values from the supplier.
      Type Parameters:
      T - The configuration POJO type
      Parameters:
      defaultPojoSupplier - Supplier for creating default POJO instances
      Returns:
      The loaded (or newly created) configuration instance
    • loadPlain

      public <T> Config<T> loadPlain(Supplier<T> defaultPojoSupplier)
      Loads a plain @Configuration-annotated POJO (no need to extend ConfigurablePojo) and returns a Config handle that carries the lifecycle operations:
      
       Config<AppConfig> config = ConfigurationLoader.from(path).loadPlain(AppConfig::new);
       AppConfig app = config.get();
       config.save();
       
      Type Parameters:
      T - The plain configuration POJO type
      Parameters:
      defaultPojoSupplier - Supplier for creating default POJO instances
      Returns:
      A handle wrapping the loaded (or newly created) configuration