Class YamlSource

All Implemented Interfaces:
CreatableConfig<String>, CRUDOperations<String>, DeletableConfig<String>, ReadableConfig<String>, UpdatableConfig<String>
Direct Known Subclasses:
AutoReadYamlSource

public class YamlSource extends BaseConfig<String>
A robust YAML implementation of BaseConfig that supports hierarchical data structures and persistent merging.

This class handles the conversion between flat in-memory data (used for easy access) and nested YAML structures (used for human-readable files). It implements a "Read-Modify-Write" pattern to ensure that existing data in the file is preserved unless explicitly overwritten by the application.

* @author a8kj7sea
Since:
0.3
  • Constructor Details

    • YamlSource

      public YamlSource()
      Initializes the YAML source with standard block-style formatting. Block style is used to ensure the generated YAML is clean and easy for users to edit.
  • Method Details

    • read

      public ConfigOperation<String> read()
      Reads the YAML file from disk and flattens it into the configuration memory. Existing memory data is cleared before loading to ensure state consistency with the disk.
      Specified by:
      read in interface ReadableConfig<String>
      Specified by:
      read in class BaseConfig<String>
      Returns:
      A ConfigOperation representing the read process.
    • update

      public ConfigOperation<String> update()
      Synchronizes the in-memory data with the physical file using a merge strategy.

      Logic flow: 1. Load the current file content into a temporary map. 2. Unflatten the in-memory storage into a nested map structure. 3. Merge the memory structure into the file structure (memory takes precedence). 4. Save the combined structure back to the disk.

      Specified by:
      update in interface UpdatableConfig<String>
      Specified by:
      update in class BaseConfig<String>
      Returns:
      A ConfigOperation representing the update and merge process.