Package de.paull.lib.files
Class ConfigHandler
java.lang.Object
de.paull.lib.files.ConfigHandler
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe Method should contain all key-value pairs which should be saved in the config.static interfaceThe Method should contain all key-value pairs which should be saved in the config but without showing the values' content inside of the print function. -
Constructor Summary
ConstructorsConstructorDescriptionConfigHandler(String path, String fileHeader, ConfigHandler.InitializeConfig initializeConfig) Initializes the ConfigHandlerConfigHandler(String path, String fileHeader, ConfigHandler.InitializeConfig initializeConfig, ConfigHandler.InitializeHiddenConfig iniHidCfg) Initializes the ConfigHandler -
Method Summary
Modifier and TypeMethodDescriptionstatic StringReturns the value for the specified keystatic StringgetDefault(String key) Returns the default value for the given keystatic intgetInteger(String key) Returns the value as an int for the specified keystatic voidGibt alle Config-Daten in einer Tabelle aus.
-
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 infileHeader- the Header for the fileinitializeConfig- 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 infileHeader- the Header for the fileinitializeConfig- 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
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
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
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
-