Package net.storm.api.plugins.config
Class ConfigData
java.lang.Object
net.storm.api.plugins.config.ConfigData
Thread-safe storage for configuration data with persistent file backing.
ConfigData manages configuration properties in memory with support for incremental patching and atomic file operations. It handles concurrent access from multiple threads and supports safe merging of changes from multiple client instances.
The class uses a patch-based approach for saving:
- Changes are tracked incrementally
- On save, the file is loaded, patches are applied, and the result is saved atomically
- This prevents data loss when multiple clients edit the same file
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConfigData(File configPath) Creates a new ConfigData instance and loads existing data from the file. -
Method Summary
Modifier and TypeMethodDescriptionget()Gets an unmodifiable view of all configuration properties.getProperty(String key) Gets a configuration property value.keySet()Gets all configuration keys.voidApplies a patch of changes to the configuration file.voidSets multiple configuration properties at once.setProperty(String key, String value) Sets a configuration property value.Atomically retrieves and clears pending changes.Removes a configuration property.
-
Constructor Details
-
ConfigData
Creates a new ConfigData instance and loads existing data from the file.If the file doesn't exist, an empty configuration is created.
- Parameters:
configPath- the path to the configuration file- Throws:
RuntimeException- if the file exists but cannot be read
-
-
Method Details
-
getProperty
Gets a configuration property value.- Parameters:
key- the property key- Returns:
- the property value, or null if not set
-
setProperty
Sets a configuration property value.The change is tracked for later patching to the file.
- Parameters:
key- the property keyvalue- the value to set- Returns:
- the previous value, or null if the key was not set
-
unset
Removes a configuration property.The removal is tracked for later patching to the file.
- Parameters:
key- the property key to remove- Returns:
- the previous value, or null if the key was not set
-
putAll
Sets multiple configuration properties at once.All changes are tracked for later patching to the file.
- Parameters:
values- the map of key-value pairs to set
-
keySet
Gets all configuration keys.- Returns:
- a set of all keys in the configuration
-
get
Gets an unmodifiable view of all configuration properties.- Returns:
- an unmodifiable map of all key-value pairs
-
swapChanges
Atomically retrieves and clears pending changes.This method is used before saving to get the list of changes that need to be written to the file.
- Returns:
- a map of pending changes (value is null for deletions), or an empty map if no changes are pending
-
patch
Applies a patch of changes to the configuration file.This method performs a safe merge operation:
- Acquires a file lock
- Loads the current file contents
- Applies the patch changes
- Saves the result atomically
- Parameters:
patch- the changes to apply (null values indicate deletion)
-