Class ConfigurationFile

java.lang.Object
me.neznamy.tab.api.config.ConfigurationFile
Direct Known Subclasses:
YamlConfigurationFile

public abstract class ConfigurationFile extends Object
Abstract class for configuration file
  • Field Details

    • values

      protected Map<String,Object> values
      Configuration file content
    • file

      protected final File file
      File to use
  • Constructor Details

    • ConfigurationFile

      protected ConfigurationFile(InputStream source, File destination) throws IOException
      Constructs new instance and attempts to load specified configuration file. If file does not exist, default file is copied from source.
      Parameters:
      source - Source to copy file from if it does not exist
      destination - File destination to use
      Throws:
      IllegalArgumentException - if destination is null
      IllegalStateException - if file does not exist and source is null
      IOException - if I/O operation with the file unexpectedly fails
  • Method Details

    • save

      public abstract void save()
      Saves values from map to the file
    • getName

      public String getName()
      Returns simple name of the file
      Returns:
      simple name of the file
    • getValues

      public Map<String,Object> getValues()
      Returns the root value map
      Returns:
      the root value map
    • setValues

      public void setValues(Map<String,Object> values)
    • getObject

      public Object getObject(String path, Object defaultValue)
      Gets config option with specified path. If the option is not present and defaultValue is not null, value is inserted, save() called and defaultValue returned.
      Parameters:
      path - Path to the option with sections separated with "."
      defaultValue - Value to be inserted and returned if option is not present
      Returns:
      value from configuration file
    • getObject

      public Object getObject(String path)
      Returns config option with specified path. If option is not present, null is returned.
      Parameters:
      path - Path to the option with sections separated with "."
      Returns:
      value from configuration file or null if not present
    • getObject

      public Object getObject(String[] path)
    • getString

      public String getString(String path)
      Returns config option with specified path as String. Returns null if option is not present.
      Parameters:
      path - Path to the option with sections separated with "."
      Returns:
      value from file or null if not present
    • getString

      public String getString(String path, String defaultValue)
      Returns config option with specified path as String. If the option is not present and defaultValue is not null, value is inserted, save() called and defaultValue returned.
      Parameters:
      path - Path to the option with sections separated with "."
      defaultValue - Value to be inserted and returned if option is not present
      Returns:
      value from configuration file as String
    • getStringList

      public List<String> getStringList(String path)
      Returns config option with specified path as List<String>. Returns null if option is not present.
      Parameters:
      path - Path to the option with sections separated with "."
      Returns:
      value from file or null if not present
    • getStringList

      public List<String> getStringList(String path, List<String> defaultValue)
      Returns config option with specified path as List<String>. If the option is not present and defaultValue is not null, value is inserted, save() called and defaultValue returned.
      Parameters:
      path - Path of the option with sections separated with "."
      defaultValue - Value to be inserted and returned if option is not present
      Returns:
      value from configuration file as List<String>
    • getInt

      public Integer getInt(String path)
      Returns config option with specified path as Integer. Returns null if option is not present.
      Parameters:
      path - Path to the option with sections separated with "."
      Returns:
      value from file or null if not present
    • getInt

      public Integer getInt(String path, Integer defaultValue)
      Returns config option with specified path as Integer. If the option is not present and defaultValue is not null, value is inserted, save() called and defaultValue returned.
      Parameters:
      path - Path to the option with sections separated with "."
      defaultValue - Value to be inserted and returned if option is not present
      Returns:
      value from configuration file as Integer
    • getBoolean

      public boolean getBoolean(String path, boolean defaultValue)
      Returns config option with specified path as Boolean. If the option is not present and defaultValue is not null, value is inserted, save() called and defaultValue returned.
      Parameters:
      path - Path to the option with sections separated with "."
      defaultValue - Value to be inserted and returned if option is not present
      Returns:
      value from configuration file as Boolean
    • getDouble

      public Double getDouble(String path, double defaultValue)
      Returns config option with specified path as Double. If the option is not present and defaultValue is not null, value is inserted, save() called and defaultValue returned.
      Parameters:
      path - Path to the option with sections separated with "."
      defaultValue - Value to be inserted and returned if option is not present
      Returns:
      value from configuration file as Double
    • getConfigurationSection

      public <K, V> Map<K,V> getConfigurationSection(String path)
      Returns config option with specified path as Map<K, V>. If the option is not present or value is not a Map, new empty Map is returned.
      Parameters:
      path - Path to the option with sections separated with "."
      Returns:
      value from configuration file as Map<K, V>
    • hasConfigOption

      public boolean hasConfigOption(String path)
      Returns true if the file has option with specified path, false if not.
      Parameters:
      path - Path to the option with sections separated with "."
      Returns:
      true if present, false if not
    • set

      public void set(String path, Object value)
      Sets value to the specified path and saves the file to disk by calling save().
      Parameters:
      path - Path to the option with sections separated with "."
      value - Value to save
    • fixHeader

      public void fixHeader() throws IOException
      Inserts header back into file. This is required after calling save(), because it destroys the header.
      Throws:
      IOException - if I/O operation fails
    • getFile

      public File getFile()