Class BaseConfig<K>

java.lang.Object
me.a8kj.config.file.BaseConfig<K>
Type Parameters:
K - The type of data keys used in the configuration.
All Implemented Interfaces:
CreatableConfig<K>, CRUDOperations<K>, DeletableConfig<K>, ReadableConfig<K>, UpdatableConfig<K>
Direct Known Subclasses:
BasicVelocityConfig, BukkitConfig, YamlSource

public abstract class BaseConfig<K> extends Object implements CRUDOperations<K>
An abstract base implementation of CRUDOperations providing core file management logic.

This class serves as the foundation for all configuration formats (YAML, JSON, etc.). It handles the physical file lifecycle on the disk, including creation, resource extraction, and deletion, while providing non-mandatory lifecycle hooks for extended logic.

Since:
0.1
Author:
a8kj7sea
  • Constructor Details

    • BaseConfig

      public BaseConfig()
  • Method Details

    • onPreCreate

      protected void onPreCreate(ConfigFile<K> config)
      Optional hook triggered immediately before the configuration file is created. * @param config The configuration file instance to be created.
    • onPostCreate

      protected void onPostCreate(ConfigFile<K> config)
      Optional hook triggered after the configuration file has been successfully written to disk, either from a resource or as a new empty file. * @param config The created configuration file instance.
    • onPreDelete

      protected void onPreDelete(ConfigFile<K> config)
      Optional hook triggered immediately before the configuration file is deleted from disk. * @param config The configuration file instance to be deleted.
    • onPostDelete

      protected void onPostDelete(ConfigFile<K> config)
      Optional hook triggered after the configuration file and its memory buffer have been successfully cleared and removed. * @param config The deleted configuration file instance.
    • create

      public ConfigOperation<K> create()
      Creates a ConfigOperation for initializing the configuration file.

      The process includes pre-hook execution, parent directory validation, resource extraction, and post-hook execution upon success.

      Specified by:
      create in interface CreatableConfig<K>
      Returns:
      A ConfigOperation for file creation and resource mapping.
    • getReader

      protected Reader getReader(File file) throws IOException
      Provides a Reader configured with UTF-8 encoding.
      Parameters:
      file - The file to read from.
      Returns:
      A UTF-8 encoded Reader.
      Throws:
      IOException - If the file is inaccessible.
    • getWriter

      protected Writer getWriter(File file) throws IOException
      Provides a Writer configured with UTF-8 encoding.
      Parameters:
      file - The file to write to.
      Returns:
      A UTF-8 encoded Writer.
      Throws:
      IOException - If the file cannot be opened for writing.
    • delete

      public ConfigOperation<K> delete()
      Creates a ConfigOperation for deleting the file and clearing memory.

      Includes pre/post hooks to allow for cleanup logic or event dispatching.

      Specified by:
      delete in interface DeletableConfig<K>
      Returns:
      A ConfigOperation for file deletion and memory cleanup.
    • read

      public abstract ConfigOperation<K> read()
      Description copied from interface: ReadableConfig
      Provides the operation logic for reading the configuration data. This typically involves deserializing the file (e.g., YAML, JSON, Properties) and populating the DataMemory.
      Specified by:
      read in interface ReadableConfig<K>
      Returns:
      a ConfigOperation representing the reading/loading process.
    • update

      public abstract ConfigOperation<K> update()
      Description copied from interface: UpdatableConfig
      Provides the operation logic for updating (saving) the configuration data. This typically involves taking the current state from DataMemory and writing it to the file, ensuring all changes are synchronized.
      Specified by:
      update in interface UpdatableConfig<K>
      Returns:
      a ConfigOperation representing the updating/saving process.