package me.liamdodds.framework.configuration;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Liam Cristoforo-Dodds on 17/05/15.
 */
public class Configuration {
    private static HashMap<String, ConfigObject> configObjects;
    private static ArrayList<Configurable> configurables;
    static {
        configObjects = new HashMap<>();
        configurables = new ArrayList<>();
    }

    public static void notifyConfigurables(ConfigObject configObject) {
        configurables.forEach((configurable -> configurable.onConfigurationChange(configObject)));
    }

    public static void subscribe(Configurable configurable) {
        configurables.add(configurable);
    }

    public static void add(String key, Object object) {
        ConfigObject configObject = new ConfigObject(key, object);
        configObjects.put(key, configObject);
        notifyConfigurables(configObject);
    }

    public static ConfigObject get(String key) {
        return configObjects.get(key);
    }

    public static Object getValue(String key) {
        return getValue(key, null);
    }

    public static <T> T cast(String key, Class<T> type) {
        return cast(key, type, null);
    }

    public static Object getValue(String key, Object def) {
        if(configObjects.containsKey(key)) {
            return configObjects.get(key).getValue();
        }

        return def;
    }

    public static <T> T cast(String key, Class<T> type, T def) {
        if(configObjects.containsKey(key)) {
            return configObjects.get(key).cast(type);
        }
        return def;
    }
}

@@ -99,16 +99,97 @@
 p;%0A%0A/**%0A
+ * A way to store and use configurations. Can be subscribed to by Configurables.%0A
  * Creat
@@ -496,46 +496,130 @@
     
-public static void notifyConfigurables
+/**%0A     * Publishes out changes to configurable objects%0A     * @param configObject%0A     */%0A    public static void publish
 (Con
@@ -742,32 +742,132 @@
 ject)));%0A    %7D%0A%0A
+    /**%0A     * Subscribes a configurable to all published events%0A     * @param configurable%0A     */%0A
     public stati
@@ -952,32 +952,134 @@
 urable);%0A    %7D%0A%0A
+    /**%0A     * Adds a key / value to the configuration%0A     * @param key%0A     * @param object%0A     */%0A
     public stati
@@ -1243,27 +1243,15 @@
     
-notifyConfigurables
+publish
 (con
@@ -1261,32 +1261,119 @@
 Object);%0A    %7D%0A%0A
+    /**%0A     * Returns a configuration object%0A     * @param key%0A     * @return%0A     */%0A
     public stati
@@ -1459,197 +1459,603 @@
     
-public static Object getValue(String key) %7B%0A        return getValue(key, null);%0A    %7D%0A%0A    public static %3CT%3E T cast(String key, Class%3CT%3E type) %7B%0A        return cast(key, type, null);%0A    %7D%0A
+/**%0A     * Returns the value of a configuration object (or null)%0A     * @param key%0A     * @return%0A     */%0A    public static Object getValue(String key) %7B%0A        return getValue(key, null);%0A    %7D%0A%0A    /**%0A     * Return the value of a configuration object cast to T (or null)%0A     * @param key%0A     * @param type%0A     * @param %3CT%3E%0A     * @return%0A     */%0A    public static %3CT%3E T cast(String key, Class%3CT%3E type) %7B%0A        return cast(key, type, null);%0A    %7D%0A%0A    /**%0A     * Returns the value of a configuration object (or the value of def)%0A     * @param key%0A     * @param def%0A     * @return%0A     */
 %0A   
@@ -2244,24 +2244,211 @@
 def;%0A    %7D%0A%0A
+    /**%0A     * Retursn the value of a configuration object cast to T (or the value of def)%0A     * @param key%0A     * @param type%0A     * @param def%0A     * @param %3CT%3E%0A     * @return%0A     */%0A
     public s
@@ -2596,16 +2596,21 @@
 ast(type
+, def
 );%0A     

Add comments to Configuration