package com.wizzardo.tools.evaluation;

import com.wizzardo.tools.collections.CollectionTools;

import java.util.HashMap;

/**
 * Created by wizzardo on 22.12.15.
 */
public class Config extends HashMap<String, Object> implements CollectionTools.Closure<Object, ClosureExpression> {
    @Override
    public Object get(Object key) {
        Object value = super.get(key);
        if (value == null) {
            put((String) key, value = new Config());
        }
        return value;
    }

    @Override
    public Object execute(ClosureExpression it) {
        it.get(this);
        return this;
    }

    public <T> T get(String key, T def) {
        Object value = super.get(key);
        if (value == null)
            return def;

        if (value instanceof Config && ((Config) value).isEmpty() && !(def instanceof Config))
            return def;

        return (T) value;
    }
}

@@ -114,16 +114,38 @@
 HashMap;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -906,11 +906,630 @@
 e;%0A    %7D
+%0A%0A    public void merge(Map%3CString, Object%3E scr) %7B%0A        merge(this, scr);%0A    %7D%0A%0A    private void merge(Map into, Map%3C?, ?%3E from) %7B%0A        for (Map.Entry%3C?, ?%3E entry : from.entrySet()) %7B%0A            if (entry.getValue() instanceof Map) %7B%0A                Object in = into.get(entry.getKey());%0A                if (in instanceof Map) %7B%0A                    merge(((Map) in), ((Map) entry.getValue()));%0A                %7D else %7B%0A                    into.put(entry.getKey(), entry.getValue());%0A                %7D%0A            %7D else %7B%0A                into.put(entry.getKey(), entry.getValue());%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

add ability to merge configs