Class ConfigHandler<Config>

  • Type Parameters:
    Config - A simple getter and setter class containing all your config values.

    public class ConfigHandler<Config>
    extends java.lang.Object
    Config handler for easy managing of config files with the json format.
    • Constructor Summary

      Constructors 
      Constructor Description
      ConfigHandler​(Config defaultConfig, java.lang.String path)
      Create a config handler.
      ConfigHandler​(Config defaultConfig, java.lang.String path, java.lang.Boolean writeDefaultConfigAutomatically)
      Create a config handler.
    • Method Summary

      Modifier and Type Method Description
      boolean checkIfExists()
      Check if the config file is already existing.
      boolean delete()
      Delete the file.
      Config getDefaultConfig()
      Get the default config object.
      java.io.File getFile()
      Get the config file as an File object.
      java.lang.String getPath()
      Get the path to the config file.
      static java.lang.String getSystemConfigFolder​(java.lang.String projectName)
      Get an path to an folder where you can store system wide configurations.
      static java.lang.String getUserConfigFolder()
      Get an path to an folder where you can store user configurations.
      static java.lang.String getUserConfigFolder​(java.lang.String projectName)
      Get an path to an folder where you can store user configurations.
      Config loadConfig()
      Load the config.
      void saveDefaultConfig()
      Save the default config in the config file.
      void storeConfig​(Config config)
      Save the config object as an JSON string in the config file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ConfigHandler

        public ConfigHandler​(Config defaultConfig,
                             java.lang.String path)
                      throws java.io.IOException
        Create a config handler. Writes automatically the default config if not existing.
        Parameters:
        defaultConfig - The default config.
        path - The path to the file with the file name and extension. In normal cases should be an ".json" file.
        Throws:
        java.io.IOException - If file is not read- or writeable an IOException is thrown.
      • ConfigHandler

        public ConfigHandler​(Config defaultConfig,
                             java.lang.String path,
                             java.lang.Boolean writeDefaultConfigAutomatically)
                      throws java.io.IOException
        Create a config handler.
        Parameters:
        defaultConfig - The default config.
        path - The path to the file with the file name and extension. In normal cases should be an ".json" file.
        writeDefaultConfigAutomatically - If true the default config will be written automatically if no config exists. If this is enabled the config will also be updated every time the handler is created.
        Throws:
        java.io.IOException - If file is not read- or writeable an IOException is thrown.
    • Method Detail

      • checkIfExists

        public boolean checkIfExists()
        Check if the config file is already existing.
        Returns:
        Returns true if the file is existing.
      • delete

        public boolean delete()
        Delete the file.
        Returns:
        Returns true if the file was deleted successfully.
      • saveDefaultConfig

        public void saveDefaultConfig()
                               throws java.io.IOException
        Save the default config in the config file. WARNING: this also overrides normal configs. You should check before this if the config exists.
        Throws:
        java.io.IOException - If the writing to the file fails an IOException will be thrown.
      • loadConfig

        public Config loadConfig()
                          throws java.io.IOException
        Load the config.
        Returns:
        The config that was loaded from the file.
        Throws:
        java.io.IOException - If the reading of the file fails an IOException will be thrown.
      • storeConfig

        public void storeConfig​(Config config)
                         throws java.io.IOException
        Save the config object as an JSON string in the config file.
        Parameters:
        config - Config object to save.
        Throws:
        java.io.IOException - If the writing to the file fails an IOException will be thrown.
      • getPath

        public java.lang.String getPath()
        Get the path to the config file.
        Returns:
        Returns the path to the config file.
      • getFile

        public java.io.File getFile()
        Get the config file as an File object.
        Returns:
        Returns the config file as an File object.
      • getDefaultConfig

        public Config getDefaultConfig()
        Get the default config object.
        Returns:
        Returns the default config object.
      • getUserConfigFolder

        public static java.lang.String getUserConfigFolder()
        Get an path to an folder where you can store user configurations. This function is OS optimized for Windows, Linux and Mac.
        Returns:
        An path to an folder where you can store user configurations. It is recommended to create an folder with your application's name in this folder.
      • getUserConfigFolder

        public static java.lang.String getUserConfigFolder​(java.lang.String projectName)
        Get an path to an folder where you can store user configurations. This function is OS optimized for Windows, Linux and Mac.
        Parameters:
        projectName - The project name. If this value is not null an folder will also be created with the project name in the configurations folder.
        Returns:
        An path to an folder where you can store user configurations.
      • getSystemConfigFolder

        public static java.lang.String getSystemConfigFolder​(@NotNull
                                                             java.lang.String projectName)
        Get an path to an folder where you can store system wide configurations. This function is OS optimized for Windows, Linux and Mac.
        Parameters:
        projectName - The project name. An folder will also be created with the project name.
        Returns:
        An path to an folder where you can store system wide configurations.