Class ConfigurablePojo<SELF extends ConfigurablePojo<SELF>>

java.lang.Object
de.bsommerfeld.jshepherd.core.ConfigurablePojo<SELF>
Type Parameters:
SELF - The concrete type extending this class (pass your own class name)

public abstract class ConfigurablePojo<SELF extends ConfigurablePojo<SELF>> extends Object
Abstract base class for configuration POJOs that can be saved and reloaded.

This class uses a self-referential generic type parameter (SELF) to ensure type-safe save() and reload() operations. The pattern allows the persistence layer to work with your concrete type without requiring casts.

Usage:


 // Pass your own class as the type parameter
 public class AppConfig extends ConfigurablePojo<AppConfig> {
     // ...
 }
 

Why this pattern? Without it, reload() would only know about ConfigurablePojo, not your specific fields. The self-reference ensures the persistence delegate can properly populate your concrete class.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the values from the last load or reload that could not be applied to their fields (e.g.
    boolean
    Returns whether this configuration is being watched for external file changes.
    void
    Reloads the state of this configuration object from its persistent store.
    void
    Saves the current state of this configuration object to its persistent store.
    void
    Registers a listener that is invoked after this configuration has been automatically reloaded because the file changed on disk.
    void
    Stops watching the configuration file for external changes.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ConfigurablePojo

      public ConfigurablePojo()
  • Method Details

    • getLastLoadIssues

      public List<LoadIssue> getLastLoadIssues()
      Returns the values from the last load or reload that could not be applied to their fields (e.g. port = "abc" for an int field). The affected fields keep their previous/default values.

      This list is populated before @PostInject methods run, so it can be used for custom validation:

      
       @PostInject
       private void validate() {
           if (!getLastLoadIssues().isEmpty()) {
               throw new IllegalStateException("Invalid config values: " + getLastLoadIssues());
           }
       }
       

      Note: per-key issue reporting applies to the TOML and Properties formats. YAML and JSON bind the whole document at once — a type mismatch there fails the entire load loudly instead.

      Returns:
      an immutable list of issues; empty if the last load was clean
    • save

      public void save()
      Saves the current state of this configuration object to its persistent store.
    • reload

      public void reload()
      Reloads the state of this configuration object from its persistent store. The fields of this instance will be updated with the reloaded values.
    • setOnAutoReload

      public void setOnAutoReload(Runnable listener)
      Registers a listener that is invoked after this configuration has been automatically reloaded because the file changed on disk. Requires auto-reload to be enabled via ConfigurationLoader.from(path).withAutoReload().

      The listener runs on the watcher thread — keep it short and thread-safe. Pass null to remove a previously registered listener.

    • isAutoReloadActive

      public boolean isAutoReloadActive()
      Returns whether this configuration is being watched for external file changes.
    • stopAutoReload

      public void stopAutoReload()
      Stops watching the configuration file for external changes. Has no effect if auto-reload was never enabled. Manual save() and reload() continue to work.