Class ConfigLoader

java.lang.Object
dev.polv.taleapi.config.ConfigLoader

public class ConfigLoader extends Object
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 ConfigNode for 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 Details

    • ConfigLoader

      public ConfigLoader(ConfigProvider provider)
      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

      public ConfigLoader(ConfigProvider provider, Executor defaultExecutor)
      Creates a new ConfigLoader with the given provider and executor.
      Parameters:
      provider - the configuration provider to use for serialization
      defaultExecutor - the default executor for async operations
      Throws:
      NullPointerException - if provider or defaultExecutor is null
  • Method Details

    • getProvider

      public ConfigProvider getProvider()
      Returns the configuration provider used by this loader.
      Returns:
      the configuration provider
    • load

      public <T> T load(File file, Class<T> type)
      Loads configuration from a file into a typed object.
      Type Parameters:
      T - the configuration type
      Parameters:
      file - the file to load from
      type - the configuration class type
      Returns:
      the loaded configuration object
      Throws:
      ConfigException - if loading or parsing fails
      NullPointerException - if file or type is null
    • load

      public <T> T load(Path path, Class<T> type)
      Loads configuration from a path into a typed object.
      Type Parameters:
      T - the configuration type
      Parameters:
      path - the path to load from
      type - the configuration class type
      Returns:
      the loaded configuration object
      Throws:
      ConfigException - if loading or parsing fails
      NullPointerException - if path or type is null
    • load

      public <T> T load(Reader reader, Class<T> type)
      Loads configuration from a reader into a typed object.
      Type Parameters:
      T - the configuration type
      Parameters:
      reader - the reader to load from
      type - the configuration class type
      Returns:
      the loaded configuration object
      Throws:
      ConfigException - if parsing fails
      NullPointerException - if reader or type is null
    • loadFromString

      public <T> T loadFromString(String content, Class<T> type)
      Loads configuration from a string into a typed object.
      Type Parameters:
      T - the configuration type
      Parameters:
      content - the string content to parse
      type - the configuration class type
      Returns:
      the loaded configuration object
      Throws:
      ConfigException - if parsing fails
      NullPointerException - if content or type is null
    • load

      public ConfigNode load(File file)
      Loads configuration from a file into a traversable ConfigNode.

      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 fails
      NullPointerException - if file is null
    • load

      public ConfigNode load(Path path)
      Loads configuration from a path into a traversable ConfigNode.
      Parameters:
      path - the path to load from
      Returns:
      the configuration as a traversable node
      Throws:
      ConfigException - if loading or parsing fails
      NullPointerException - if path is null
    • loadNode

      public ConfigNode loadNode(Reader reader)
      Loads configuration from a reader into a traversable ConfigNode.
      Parameters:
      reader - the reader to load from
      Returns:
      the configuration as a traversable node
      Throws:
      ConfigException - if parsing fails
      NullPointerException - if reader is null
    • loadNodeFromString

      public ConfigNode loadNodeFromString(String content)
      Loads configuration from a string into a traversable ConfigNode.
      Parameters:
      content - the string content to parse
      Returns:
      the configuration as a traversable node
      Throws:
      ConfigException - if parsing fails
      NullPointerException - if content is null
    • loadAsync

      public <T> CompletableFuture<T> loadAsync(File file, Class<T> type)
      Loads configuration from a file asynchronously using the default executor.
      Type Parameters:
      T - the configuration type
      Parameters:
      file - the file to load from
      type - the configuration class type
      Returns:
      a CompletableFuture that completes with the loaded configuration
      Throws:
      NullPointerException - if file or type is null
    • loadAsync

      public <T> CompletableFuture<T> loadAsync(File file, Class<T> type, Executor executor)
      Loads configuration from a file asynchronously using the specified executor.
      Type Parameters:
      T - the configuration type
      Parameters:
      file - the file to load from
      type - the configuration class type
      executor - the executor to use
      Returns:
      a CompletableFuture that completes with the loaded configuration
      Throws:
      NullPointerException - if any argument is null
    • loadAsync

      public <T> CompletableFuture<T> loadAsync(Path path, Class<T> type)
      Loads configuration from a path asynchronously using the default executor.
      Type Parameters:
      T - the configuration type
      Parameters:
      path - the path to load from
      type - the configuration class type
      Returns:
      a CompletableFuture that completes with the loaded configuration
      Throws:
      NullPointerException - if path or type is null
    • loadAsync

      public <T> CompletableFuture<T> loadAsync(Path path, Class<T> type, Executor executor)
      Loads configuration from a path asynchronously using the specified executor.
      Type Parameters:
      T - the configuration type
      Parameters:
      path - the path to load from
      type - the configuration class type
      executor - the executor to use
      Returns:
      a CompletableFuture that completes with the loaded configuration
      Throws:
      NullPointerException - if any argument is null
    • loadAsync

      public CompletableFuture<ConfigNode> loadAsync(File file)
      Loads configuration from a file into a ConfigNode asynchronously.
      Parameters:
      file - the file to load from
      Returns:
      a CompletableFuture that completes with the ConfigNode
      Throws:
      NullPointerException - if file is null
    • loadAsync

      public CompletableFuture<ConfigNode> loadAsync(File file, Executor executor)
      Loads configuration from a file into a ConfigNode asynchronously.
      Parameters:
      file - the file to load from
      executor - the executor to use
      Returns:
      a CompletableFuture that completes with the ConfigNode
      Throws:
      NullPointerException - if any argument is null
    • loadAsync

      public CompletableFuture<ConfigNode> loadAsync(Path path)
      Loads configuration from a path into a ConfigNode asynchronously.
      Parameters:
      path - the path to load from
      Returns:
      a CompletableFuture that completes with the ConfigNode
      Throws:
      NullPointerException - if path is null
    • loadAsync

      public CompletableFuture<ConfigNode> loadAsync(Path path, Executor executor)
      Loads configuration from a path into a ConfigNode asynchronously.
      Parameters:
      path - the path to load from
      executor - the executor to use
      Returns:
      a CompletableFuture that completes with the ConfigNode
      Throws:
      NullPointerException - if any argument is null
    • save

      public <T> void save(File file, T value)
      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 to
      value - the configuration object to save
      Throws:
      ConfigException - if saving fails
      NullPointerException - if file or value is null
    • save

      public <T> void save(Path path, T value)
      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 to
      value - the configuration object to save
      Throws:
      ConfigException - if saving fails
      NullPointerException - if path or value is null
    • save

      public <T> void save(Writer writer, T value)
      Saves configuration to a writer.
      Type Parameters:
      T - the configuration type
      Parameters:
      writer - the writer to save to
      value - the configuration object to save
      Throws:
      ConfigException - if serialization fails
      NullPointerException - if writer or value is null
    • save

      public void save(File file, ConfigNode node)
      Saves a ConfigNode to a file.

      Creates parent directories if they don't exist.

      Parameters:
      file - the file to save to
      node - the configuration node to save
      Throws:
      ConfigException - if saving fails
      NullPointerException - if file or node is null
    • save

      public void save(Path path, ConfigNode node)
      Saves a ConfigNode to a path.
      Parameters:
      path - the path to save to
      node - the configuration node to save
      Throws:
      ConfigException - if saving fails
      NullPointerException - if path or node is null
    • saveToString

      public <T> String saveToString(T value)
      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 fails
      NullPointerException - if value is null
    • saveAsync

      public <T> CompletableFuture<Void> saveAsync(File file, T value)
      Saves configuration to a file asynchronously using the default executor.
      Type Parameters:
      T - the configuration type
      Parameters:
      file - the file to save to
      value - the configuration object to save
      Returns:
      a CompletableFuture that completes when saving is done
      Throws:
      NullPointerException - if file or value is null
    • saveAsync

      public <T> CompletableFuture<Void> saveAsync(File file, T value, Executor executor)
      Saves configuration to a file asynchronously using the specified executor.
      Type Parameters:
      T - the configuration type
      Parameters:
      file - the file to save to
      value - the configuration object to save
      executor - the executor to use
      Returns:
      a CompletableFuture that completes when saving is done
      Throws:
      NullPointerException - if any argument is null
    • saveAsync

      public <T> CompletableFuture<Void> saveAsync(Path path, T value)
      Saves configuration to a path asynchronously using the default executor.
      Type Parameters:
      T - the configuration type
      Parameters:
      path - the path to save to
      value - the configuration object to save
      Returns:
      a CompletableFuture that completes when saving is done
      Throws:
      NullPointerException - if path or value is null
    • saveAsync

      public <T> CompletableFuture<Void> saveAsync(Path path, T value, Executor executor)
      Saves configuration to a path asynchronously using the specified executor.
      Type Parameters:
      T - the configuration type
      Parameters:
      path - the path to save to
      value - the configuration object to save
      executor - the executor to use
      Returns:
      a CompletableFuture that completes when saving is done
      Throws:
      NullPointerException - if any argument is null
    • loadOrDefault

      public <T> T loadOrDefault(File file, Class<T> type, T defaultValue)
      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 from
      type - the configuration class type
      defaultValue - 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

      public <T> T loadOrDefault(Path path, Class<T> type, T defaultValue)
      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 from
      type - the configuration class type
      defaultValue - 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

      public <T> boolean isValid(File file, Class<T> type)
      Checks if a file exists and can be loaded as the specified type.
      Type Parameters:
      T - the configuration type
      Parameters:
      file - the file to check
      type - the configuration class type
      Returns:
      true if the file exists and can be parsed, false otherwise
    • isValid

      public <T> boolean isValid(Path path, Class<T> type)
      Checks if a path exists and can be loaded as the specified type.
      Type Parameters:
      T - the configuration type
      Parameters:
      path - the path to check
      type - the configuration class type
      Returns:
      true if the file exists and can be parsed, false otherwise
    • isValid

      public boolean isValid(File file)
      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

      public boolean isValid(Path path)
      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