Class ConfigUtil

java.lang.Object
info.u_team.u_team_core.util.ConfigUtil

public class ConfigUtil extends Object
Utility methods for loading and reading json config files with gson
  • Field Details

  • Constructor Details

    • ConfigUtil

      public ConfigUtil()
  • Method Details

    • loadConfig

      public static <T> T loadConfig(Path directory, String name, com.google.gson.Gson gson, Function<com.google.gson.stream.JsonWriter,T> write, Function<BufferedReader,T> read) throws IOException
      Load and create a config file if not already there from a default value. Use Gson for serialization and deserialization. The extension of the file is automatically JSON_EXTENSION
      Type Parameters:
      T - Class to load with gson
      Parameters:
      directory - Path of the directory where the file is
      name - The file without the extension
      gson - Gson instance with possible registered serializer
      write - Function that should write the default values to the JsonWriter and return the the default instance
      read - Function that should read the values from BufferedReader and deserialize them and return a new instance of the class with the new values
      Returns:
      Either the default instance if the file was created or an instance with loaded value from the config
      Throws:
      IOException
    • loadConfig

      public static <T> T loadConfig(Path directory, String name, String extension, com.google.gson.Gson gson, Function<com.google.gson.stream.JsonWriter,T> write, Function<BufferedReader,T> read) throws IOException
      Load and create a config file if not already there from a default value. Use Gson for serialization and deserialization.
      Type Parameters:
      T - Class to load with gson
      Parameters:
      directory - Path of the directory where the file is
      name - The file without the extension
      extension - Extension of the file
      gson - Gson instance with possible registered serializer
      write - Function that should write the default values to the JsonWriter and return the the default instance
      read - Function that should read the values from BufferedReader and deserialize them and return a new instance of the class with the new values
      Returns:
      Either the default instance if the file was created or an instance with loaded value from the config
      Throws:
      IOException
    • loadConfig

      public static <T> T loadConfig(Path directory, String name, com.google.gson.Gson gson, T defaultValue, BiConsumer<com.google.gson.stream.JsonWriter,T> write, Function<BufferedReader,T> read) throws IOException
      Load and create a config file if not already there from a default value. Use Gson for serialization and deserialization. The extension of the file is automatically JSON_EXTENSION
      Type Parameters:
      T - Class to load with gson
      Parameters:
      directory - Path of the directory where the file is
      name - The file without the extension
      gson - Gson instance with possible registered serializer
      defaultValue - The default instance of the class that should be written if no config file is present
      write - BiConsumer that should write the default values to the JsonWriter
      read - Function that should read the values from BufferedReader and deserialize them and return a new instance of the class with the new values
      Returns:
      Either the default instance if the file was created or an instance with loaded value from the config
      Throws:
      IOException
    • loadConfig

      public static <T> T loadConfig(Path directory, String name, String extension, com.google.gson.Gson gson, T defaultValue, BiConsumer<com.google.gson.stream.JsonWriter,T> write, Function<BufferedReader,T> read) throws IOException
      Load and create a config file if not already there from a default value. Use Gson for serialization and deserialization.
      Type Parameters:
      T - Class to load with gson
      Parameters:
      directory - Path of the directory where the file is
      name - The file without the extension
      extension - Extension of the file
      gson - Gson instance with possible registered serializer
      defaultValue - The default instance of the class that should be written if no config file is present
      write - BiConsumer that should write the default values to the JsonWriter
      read - Function that should read the values from BufferedReader and deserialize them and return a new instance of the class with the new values
      Returns:
      Either the default instance if the file was created or an instance with loaded value from the config
      Throws:
      IOException