Package dev.polv.taleapi.config
Class ConfigLoader
java.lang.Object
dev.polv.taleapi.config.ConfigLoader
Main entry point for loading and saving configuration files.
ConfigLoader provides both synchronous and asynchronous operations for
reading and writing configuration files. It uses ConfigProvider
implementations to handle different file formats.
Two loading modes are supported:
- Object mapping: Load directly into a typed class
- Dynamic access: Load into a
ConfigNodefor key-based traversal
Example Usage
ConfigLoader loader = new ConfigLoader(new JsonProvider());
// Object mapping - load into a typed class
MyConfig config = loader.load(new File("config.json"), MyConfig.class);
// Dynamic access - load into a traversable node
ConfigNode node = loader.load(new File("config.json"));
String name = node.getString("serverName");
int port = node.getInt("database.port", 5432);
// Async operations
loader.loadAsync(file, MyConfig.class)
.thenAccept(cfg -> System.out.println("Loaded: " + cfg));
// Save configuration
loader.save(new File("config.json"), config);
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConfigLoader(ConfigProvider provider) Creates a new ConfigLoader with the given provider.ConfigLoader(ConfigProvider provider, Executor defaultExecutor) Creates a new ConfigLoader with the given provider and executor. -
Method Summary
Modifier and TypeMethodDescriptionReturns the configuration provider used by this loader.booleanChecks if a file exists and can be loaded as a ConfigNode.<T> booleanChecks if a file exists and can be loaded as the specified type.booleanChecks if a path exists and can be loaded as a ConfigNode.<T> booleanChecks if a path exists and can be loaded as the specified type.Loads configuration from a file into a traversableConfigNode.<T> TLoads configuration from a file into a typed object.<T> TLoads configuration from a reader into a typed object.Loads configuration from a path into a traversableConfigNode.<T> TLoads configuration from a path into a typed object.Loads configuration from a file into aConfigNodeasynchronously.<T> CompletableFuture<T>Loads configuration from a file asynchronously using the default executor.<T> CompletableFuture<T>Loads configuration from a file asynchronously using the specified executor.Loads configuration from a file into aConfigNodeasynchronously.Loads configuration from a path into aConfigNodeasynchronously.<T> CompletableFuture<T>Loads configuration from a path asynchronously using the default executor.<T> CompletableFuture<T>Loads configuration from a path asynchronously using the specified executor.Loads configuration from a path into aConfigNodeasynchronously.<T> TloadFromString(String content, Class<T> type) Loads configuration from a string into a typed object.Loads configuration from a reader into a traversableConfigNode.loadNodeFromString(String content) Loads configuration from a string into a traversableConfigNode.<T> TloadOrDefault(File file, Class<T> type, T defaultValue) Loads configuration from a file, or returns a default value if the file doesn't exist.<T> TloadOrDefault(Path path, Class<T> type, T defaultValue) Loads configuration from a path, or returns a default value if the file doesn't exist.voidsave(File file, ConfigNode node) Saves aConfigNodeto a file.<T> voidSaves configuration to a file.<T> voidSaves configuration to a writer.voidsave(Path path, ConfigNode node) Saves aConfigNodeto a path.<T> voidSaves configuration to a path.<T> CompletableFuture<Void>Saves configuration to a file asynchronously using the default executor.<T> CompletableFuture<Void>Saves configuration to a file asynchronously using the specified executor.<T> CompletableFuture<Void>Saves configuration to a path asynchronously using the default executor.<T> CompletableFuture<Void>Saves configuration to a path asynchronously using the specified executor.<T> StringsaveToString(T value) Serializes a configuration object to a string.
-
Constructor Details
-
ConfigLoader
Creates a new ConfigLoader with the given provider.Uses the common ForkJoinPool for async operations by default.
- Parameters:
provider- the configuration provider to use for serialization- Throws:
NullPointerException- if provider is null
-
ConfigLoader
Creates a new ConfigLoader with the given provider and executor.- Parameters:
provider- the configuration provider to use for serializationdefaultExecutor- the default executor for async operations- Throws:
NullPointerException- if provider or defaultExecutor is null
-
-
Method Details
-
getProvider
Returns the configuration provider used by this loader.- Returns:
- the configuration provider
-
load
Loads configuration from a file into a typed object.- Type Parameters:
T- the configuration type- Parameters:
file- the file to load fromtype- the configuration class type- Returns:
- the loaded configuration object
- Throws:
ConfigException- if loading or parsing failsNullPointerException- if file or type is null
-
load
Loads configuration from a path into a typed object.- Type Parameters:
T- the configuration type- Parameters:
path- the path to load fromtype- the configuration class type- Returns:
- the loaded configuration object
- Throws:
ConfigException- if loading or parsing failsNullPointerException- if path or type is null
-
load
Loads configuration from a reader into a typed object.- Type Parameters:
T- the configuration type- Parameters:
reader- the reader to load fromtype- the configuration class type- Returns:
- the loaded configuration object
- Throws:
ConfigException- if parsing failsNullPointerException- if reader or type is null
-
loadFromString
Loads configuration from a string into a typed object.- Type Parameters:
T- the configuration type- Parameters:
content- the string content to parsetype- the configuration class type- Returns:
- the loaded configuration object
- Throws:
ConfigException- if parsing failsNullPointerException- if content or type is null
-
load
Loads configuration from a file into a traversableConfigNode.Use this when you want dynamic access to configuration values without defining a specific class structure.
- Parameters:
file- the file to load from- Returns:
- the configuration as a traversable node
- Throws:
ConfigException- if loading or parsing failsNullPointerException- if file is null
-
load
Loads configuration from a path into a traversableConfigNode.- Parameters:
path- the path to load from- Returns:
- the configuration as a traversable node
- Throws:
ConfigException- if loading or parsing failsNullPointerException- if path is null
-
loadNode
Loads configuration from a reader into a traversableConfigNode.- Parameters:
reader- the reader to load from- Returns:
- the configuration as a traversable node
- Throws:
ConfigException- if parsing failsNullPointerException- if reader is null
-
loadNodeFromString
Loads configuration from a string into a traversableConfigNode.- Parameters:
content- the string content to parse- Returns:
- the configuration as a traversable node
- Throws:
ConfigException- if parsing failsNullPointerException- if content is null
-
loadAsync
Loads configuration from a file asynchronously using the default executor.- Type Parameters:
T- the configuration type- Parameters:
file- the file to load fromtype- the configuration class type- Returns:
- a CompletableFuture that completes with the loaded configuration
- Throws:
NullPointerException- if file or type is null
-
loadAsync
Loads configuration from a file asynchronously using the specified executor.- Type Parameters:
T- the configuration type- Parameters:
file- the file to load fromtype- the configuration class typeexecutor- the executor to use- Returns:
- a CompletableFuture that completes with the loaded configuration
- Throws:
NullPointerException- if any argument is null
-
loadAsync
Loads configuration from a path asynchronously using the default executor.- Type Parameters:
T- the configuration type- Parameters:
path- the path to load fromtype- the configuration class type- Returns:
- a CompletableFuture that completes with the loaded configuration
- Throws:
NullPointerException- if path or type is null
-
loadAsync
Loads configuration from a path asynchronously using the specified executor.- Type Parameters:
T- the configuration type- Parameters:
path- the path to load fromtype- the configuration class typeexecutor- the executor to use- Returns:
- a CompletableFuture that completes with the loaded configuration
- Throws:
NullPointerException- if any argument is null
-
loadAsync
Loads configuration from a file into aConfigNodeasynchronously.- Parameters:
file- the file to load from- Returns:
- a CompletableFuture that completes with the ConfigNode
- Throws:
NullPointerException- if file is null
-
loadAsync
Loads configuration from a file into aConfigNodeasynchronously.- Parameters:
file- the file to load fromexecutor- the executor to use- Returns:
- a CompletableFuture that completes with the ConfigNode
- Throws:
NullPointerException- if any argument is null
-
loadAsync
Loads configuration from a path into aConfigNodeasynchronously.- Parameters:
path- the path to load from- Returns:
- a CompletableFuture that completes with the ConfigNode
- Throws:
NullPointerException- if path is null
-
loadAsync
Loads configuration from a path into aConfigNodeasynchronously.- Parameters:
path- the path to load fromexecutor- the executor to use- Returns:
- a CompletableFuture that completes with the ConfigNode
- Throws:
NullPointerException- if any argument is null
-
save
Saves configuration to a file.Creates parent directories if they don't exist.
- Type Parameters:
T- the configuration type- Parameters:
file- the file to save tovalue- the configuration object to save- Throws:
ConfigException- if saving failsNullPointerException- if file or value is null
-
save
Saves configuration to a path.Creates parent directories if they don't exist.
- Type Parameters:
T- the configuration type- Parameters:
path- the path to save tovalue- the configuration object to save- Throws:
ConfigException- if saving failsNullPointerException- if path or value is null
-
save
Saves configuration to a writer.- Type Parameters:
T- the configuration type- Parameters:
writer- the writer to save tovalue- the configuration object to save- Throws:
ConfigException- if serialization failsNullPointerException- if writer or value is null
-
save
Saves aConfigNodeto a file.Creates parent directories if they don't exist.
- Parameters:
file- the file to save tonode- the configuration node to save- Throws:
ConfigException- if saving failsNullPointerException- if file or node is null
-
save
Saves aConfigNodeto a path.- Parameters:
path- the path to save tonode- the configuration node to save- Throws:
ConfigException- if saving failsNullPointerException- if path or node is null
-
saveToString
Serializes a configuration object to a string.- Type Parameters:
T- the configuration type- Parameters:
value- the configuration object to serialize- Returns:
- the serialized string
- Throws:
ConfigException- if serialization failsNullPointerException- if value is null
-
saveAsync
Saves configuration to a file asynchronously using the default executor.- Type Parameters:
T- the configuration type- Parameters:
file- the file to save tovalue- the configuration object to save- Returns:
- a CompletableFuture that completes when saving is done
- Throws:
NullPointerException- if file or value is null
-
saveAsync
Saves configuration to a file asynchronously using the specified executor.- Type Parameters:
T- the configuration type- Parameters:
file- the file to save tovalue- the configuration object to saveexecutor- the executor to use- Returns:
- a CompletableFuture that completes when saving is done
- Throws:
NullPointerException- if any argument is null
-
saveAsync
Saves configuration to a path asynchronously using the default executor.- Type Parameters:
T- the configuration type- Parameters:
path- the path to save tovalue- the configuration object to save- Returns:
- a CompletableFuture that completes when saving is done
- Throws:
NullPointerException- if path or value is null
-
saveAsync
Saves configuration to a path asynchronously using the specified executor.- Type Parameters:
T- the configuration type- Parameters:
path- the path to save tovalue- the configuration object to saveexecutor- the executor to use- Returns:
- a CompletableFuture that completes when saving is done
- Throws:
NullPointerException- if any argument is null
-
loadOrDefault
Loads configuration from a file, or returns a default value if the file doesn't exist.If the file doesn't exist, the default value is saved to the file and returned.
- Type Parameters:
T- the configuration type- Parameters:
file- the file to load fromtype- the configuration class typedefaultValue- the default value to use if file doesn't exist- Returns:
- the loaded configuration or the default value
- Throws:
ConfigException- if loading fails (but not if file doesn't exist)NullPointerException- if any argument is null
-
loadOrDefault
Loads configuration from a path, or returns a default value if the file doesn't exist.If the file doesn't exist, the default value is saved to the file and returned.
- Type Parameters:
T- the configuration type- Parameters:
path- the path to load fromtype- the configuration class typedefaultValue- the default value to use if file doesn't exist- Returns:
- the loaded configuration or the default value
- Throws:
ConfigException- if loading fails (but not if file doesn't exist)NullPointerException- if any argument is null
-
isValid
Checks if a file exists and can be loaded as the specified type.- Type Parameters:
T- the configuration type- Parameters:
file- the file to checktype- the configuration class type- Returns:
- true if the file exists and can be parsed, false otherwise
-
isValid
Checks if a path exists and can be loaded as the specified type.- Type Parameters:
T- the configuration type- Parameters:
path- the path to checktype- the configuration class type- Returns:
- true if the file exists and can be parsed, false otherwise
-
isValid
Checks if a file exists and can be loaded as a ConfigNode.- Parameters:
file- the file to check- Returns:
- true if the file exists and can be parsed, false otherwise
-
isValid
Checks if a path exists and can be loaded as a ConfigNode.- Parameters:
path- the path to check- Returns:
- true if the file exists and can be parsed, false otherwise
-