Class Config

  • All Implemented Interfaces:
    java.io.Serializable

    public class Config
    extends java.lang.Object
    implements java.io.Serializable
    This class represents a generic Configuration class, with change listeners. This class supports a ConfigListener that is fired when key-values are updated, added or removed on a per config basis.
    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addListener​(ConfigListener listener)
      Adds a new ConfigListener to this config, this listeners will be notified of all the changes that happen to the non-volatile field of this config.
      static java.util.Set<java.lang.String> configSet()
      Retrieves a Set with all the available Config objects
      static Config from​(java.lang.String name)
      A wrapper for on(String)
      java.lang.Object get​(java.lang.String key)
      Retrieves a raw value for a given key
      boolean getBool​(java.lang.String key)
      Retrieves the value for a given key as a boolean
      java.awt.Color getColor​(java.lang.String key)
      Retrieves the value for a given key as a Color
      double getDouble​(java.lang.String key)
      Retrieves the value for a given key as a double
      javax.swing.ImageIcon getIcon​(java.lang.String key)
      Retrieves the value for a given key as an ImageIcon
      int getInt​(java.lang.String key)
      Retrieves the value for a given key as an int
      java.lang.String getString​(java.lang.String key)
      Retrieves the value for a given key as a String
      static Config on​(java.lang.String name)
      Retrieves a Config instance associated with a given name, this is usually the module/plugin name.
      void put​(java.lang.String key, java.lang.Object value)
      Set's a config key,value pair.
      static void read​(java.io.InputStream is, java.lang.String name)
      Read a Config or Map of configs from the given InputStream.
      static void remove​(java.lang.String name)
      Destroys an instance of Config for a given name, this is useful when you have more than one application using this Class, and they don't start and stop at the same time.
      void removeListener​(ConfigListener listener)
      Removes a previously registered ConfigListener
      void save​(java.io.OutputStream os)
      Saves this Config in the given OutputStream.
      static void saveAll​(java.io.OutputStream os)
      Saves all the available Configs in the given OutputStream.
      void set​(java.lang.String key, java.lang.Object value)
      A wrapper for Config.put(String,Object)
      static int size()
      Retrieves the number of created Config objects.
      • Methods inherited from class java.lang.Object

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

      • from

        public static Config from​(java.lang.String name)
        A wrapper for on(String)
        Parameters:
        name - config name
        Returns:
        Config object
        See Also:
        on(String)
      • on

        public static Config on​(java.lang.String name)
        Retrieves a Config instance associated with a given name, this is usually the module/plugin name.
         Config myApp = Config.on("my.config");
         myApp.set("key.1", "value.1");
         ...
         
        Parameters:
        name - config name
        Returns:
        Config object
      • remove

        public static void remove​(java.lang.String name)
        Destroys an instance of Config for a given name, this is useful when you have more than one application using this Class, and they don't start and stop at the same time.
        Parameters:
        name - config name
      • configSet

        public static java.util.Set<java.lang.String> configSet()
        Retrieves a Set with all the available Config objects
        Returns:
        Set of configs
      • size

        public static int size()
        Retrieves the number of created Config objects.
        Returns:
        number of configs
      • put

        public void put​(java.lang.String key,
                        java.lang.Object value)
        Set's a config key,value pair. This values can be exported and imported.
        Note: every change in this values will trigger a ConfigEvent on all the registered ConfigListeners.
        Parameters:
        key - key
        value - value
        See Also:
        addListener(ConfigListener)
      • get

        public java.lang.Object get​(java.lang.String key)
        Retrieves a raw value for a given key
        Parameters:
        key - key
        Returns:
        value
      • getColor

        public java.awt.Color getColor​(java.lang.String key)
                                throws java.lang.ClassCastException
        Retrieves the value for a given key as a Color
        Parameters:
        key - key
        Returns:
        value as Color
        Throws:
        java.lang.ClassCastException - if the value isn't a Color
      • getBool

        public boolean getBool​(java.lang.String key)
                        throws java.lang.ClassCastException
        Retrieves the value for a given key as a boolean
        Parameters:
        key - key
        Returns:
        value as Boolean
        Throws:
        java.lang.ClassCastException - if the value isn't a Boolean
      • getDouble

        public double getDouble​(java.lang.String key)
                         throws java.lang.ClassCastException
        Retrieves the value for a given key as a double
        Parameters:
        key - key
        Returns:
        value as Double
        Throws:
        java.lang.ClassCastException - if the value isn't a Double
      • getIcon

        public javax.swing.ImageIcon getIcon​(java.lang.String key)
                                      throws java.lang.ClassCastException
        Retrieves the value for a given key as an ImageIcon
        Parameters:
        key - key
        Returns:
        value as ImageIcon
        Throws:
        java.lang.ClassCastException - if the value isn't a ImageIcon
      • getInt

        public int getInt​(java.lang.String key)
                   throws java.lang.ClassCastException
        Retrieves the value for a given key as an int
        Parameters:
        key - key
        Returns:
        value as Integer
        Throws:
        java.lang.ClassCastException - if the value isn't a Integer
      • getString

        public java.lang.String getString​(java.lang.String key)
                                   throws java.lang.ClassCastException
        Retrieves the value for a given key as a String
        Parameters:
        key - key
        Returns:
        value as String
        Throws:
        java.lang.ClassCastException - if the value isn't a String
      • addListener

        public void addListener​(ConfigListener listener)
        Adds a new ConfigListener to this config, this listeners will be notified of all the changes that happen to the non-volatile field of this config.
        Parameters:
        listener - ConfigListener
      • saveAll

        public static void saveAll​(java.io.OutputStream os)
                            throws java.io.IOException
        Saves all the available Configs in the given OutputStream.

        Note: all of the values of the Config object must be Serializable for this to work

        Parameters:
        os - OutputStream on which to write
        Throws:
        java.io.IOException - in case an I/O error occurs
        See Also:
        save(java.io.OutputStream), read(java.io.InputStream, java.lang.String)
      • save

        public void save​(java.io.OutputStream os)
                  throws java.io.IOException
        Saves this Config in the given OutputStream.

        Note: all of the values of the Config object must be Serializable for this to work

        Parameters:
        os - OutputStream on which to write
        Throws:
        java.io.IOException - in case an I/O error occurs
        See Also:
        saveAll(java.io.OutputStream), read(java.io.InputStream, java.lang.String)
      • read

        public static void read​(java.io.InputStream is,
                                java.lang.String name)
                         throws java.io.IOException,
                                java.lang.ClassNotFoundException
        Read a Config or Map of configs from the given InputStream.
        Parameters:
        is - InputStream from which to read
        name - If reading a single Config this is the name it will have when calling from(java.lang.String), it should be null when reading a set of configs.
        Throws:
        java.io.IOException - in case an I/O error occurs
        java.lang.ClassNotFoundException - If the InputStream doesn't point to an appropriate Config
        See Also:
        save(java.io.OutputStream), saveAll(java.io.OutputStream)