Class ConfigHandler

java.lang.Object
de.paull.lib.files.ConfigHandler

public final class ConfigHandler extends Object
Handles the creation, loading, and access of configuration key-value pairs from a config file. Useful for managing persistent application settings across sessions.

This class automatically initializes a configuration file using default values provided via the InitializeConfig interface. It reads existing config values from the file, overrides defaults where applicable, and ensures missing values are restored to defaults. Values can be retrieved using static getter methods, and the full configuration is printed in a readable table format.

  • Constructor Details

    • ConfigHandler

      public ConfigHandler(String path, String fileHeader, ConfigHandler.InitializeConfig initializeConfig)
      Initializes the ConfigHandler
      Parameters:
      path - the path the config file will be generated of found in
      fileHeader - the Header for the file
      initializeConfig - an Interface in wich the HashMap with the default values will be initialized. The Method should contain all key-value pairs which should be saved in the config.
    • ConfigHandler

      public ConfigHandler(String path, String fileHeader, ConfigHandler.InitializeConfig initializeConfig, ConfigHandler.InitializeHiddenConfig iniHidCfg)
      Initializes the ConfigHandler
      Parameters:
      path - the path the config file will be generated of found in
      fileHeader - the Header for the file
      initializeConfig - an Interface in wich the HashMap with the default values will be initialized. The Method should contain all key-value pairs which should be saved in the config.
      iniHidCfg - same functionality as the initializeConfig, but the value of the fields added here will not be shown in plain text during the printConfig().
  • Method Details

    • get

      public static String get(String key)
      Returns the value for the specified key
      Parameters:
      key - key of config pair
      Returns:
      Value from the config file if the key-value pair and the file exist. Otherwise, the default value defined in the initializeConfig.
    • getInteger

      public static int getInteger(String key)
      Returns the value as an int for the specified key
      Parameters:
      key - key of config pair
      Returns:
      Value from the config file if the key-value pair and the file exist. Otherwise, the default value defined in the initializeConfig. If a NumberFormat exception occurs, -1 will be returned.
    • getDefault

      public static String getDefault(String key)
      Returns the default value for the given key
      Parameters:
      key - key of config pair
      Returns:
      the corresponding value of the key-value pair defined in the initializeConfig.
    • printConfig

      public static void printConfig()
      Gibt alle Config-Daten in einer Tabelle aus. Wenn keine manuelle änderung vorgenommen wurde, wird der Wert aus defaultConfig genommen