com.bossletsplays.frost.utils.config.Configuration.java Source code

Java tutorial

Introduction

Here is the source code for com.bossletsplays.frost.utils.config.Configuration.java

Source

/*   Copyright 2015 Matthew Rogers "BossLetsPlays"
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package com.bossletsplays.frost.utils.config;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.bossletsplays.frost.utils.Util;
import com.bossletsplays.frost.utils.collections.MultiMap;
import com.bossletsplays.frost.utils.debug.Logger;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.DoubleNode;
import com.fasterxml.jackson.databind.node.FloatNode;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.LongNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ShortNode;
import com.fasterxml.jackson.databind.node.TextNode;

/**
 * <strong>Project:</strong> FrostAPI <br>
 * <strong>File:</strong> Configuration.java
 * 
 * <p>
 * Abstract class to create and read a file for configurable options in the API.
 * Extend this to create a config for your own game.
 * </p>
 *
 * @author <a href = "http://bossletsplays.com"> Matthew Rogers</a>
 */
public abstract class Configuration {

    private ArrayList<Option> options = new ArrayList<>();

    protected final class Option {

        private final String group;
        private final String name;
        private final Object value;
        private final String option;

        public Option(String option, Object defaultValue) {
            this.option = option;
            this.group = option.substring(0, option.lastIndexOf('.'));
            this.name = option.substring(option.lastIndexOf('.') + 1);
            this.value = defaultValue;
            options.add(this);
        }

        public String getOption() {
            return option;
        }

        public String getGroup() {
            return group;
        }

        public String getName() {
            return name;
        }

        public Object getValue() {
            return value;
        }

        public ConfigObject getConfigObject() {
            return new ConfigObject(option, name, value);
        }
    }

    private JsonWrapper writer;
    private JsonWrapper reader;
    private MultiMap<String, ConfigObject> elements;
    private Map<String, Object> map;

    public Configuration(String path) {
        map = new HashMap<String, Object>();
        createFile(path);
    }

    protected abstract void init();

    public int getInteger(String option) {
        IntNode node = (IntNode) map.get(option);
        return node.asInt();
    }

    public double getDouble(String option) {
        DoubleNode node = (DoubleNode) map.get(option);
        return node.asDouble();
    }

    public float getFloat(String option) {
        FloatNode node = (FloatNode) map.get(option);
        return node.floatValue();
    }

    public long getLong(String option) {
        LongNode node = (LongNode) map.get(option);
        return node.asLong();
    }

    public short getShort(String option) {
        ShortNode node = (ShortNode) map.get(option);
        return node.shortValue();
    }

    public byte getByte(String option) {
        ObjectNode node = (ObjectNode) map.get(option);
        return Byte.parseByte(node.asText());
    }

    public boolean getBoolean(String option) {
        BooleanNode node = (BooleanNode) map.get(option);
        return node.asBoolean();
    }

    public String getString(String option) {
        String ret = map.get(option).toString().replaceAll("\"", "");
        return ret;
    }

    private void createFile(String path) {
        init();
        elements = new MultiMap<String, ConfigObject>();
        for (Option o : options) {
            elements.put(o.getGroup(), o.getConfigObject());
            map.put(o.getOption(), o.getValue());
        }
        try {
            //            String fName = path.substring(path.lastIndexOf('/') + 1);
            String dirName = path.substring(0, path.lastIndexOf('/'));
            File dir = new File(dirName);
            dir.mkdirs();
            File f = new File(path);
            if (!f.exists()) {
                writer = new JsonWrapper(path, JsonWrapper.GENERATE);
                writer.setElements(elements);
                writer.generate();
            } else {
                //                elements.print();
                JsonWrapper firstPass = new JsonWrapper(path, JsonWrapper.PARSE);
                MultiMap<String, ConfigObject> objs = firstPass.parse();
                for (String s : objs.get()) {
                    for (ConfigObject o : objs.get(s)) {
                        ConfigObject oo = new ConfigObject(o.getKey(), o.getName(), o.getValue());
                        if (o.getValue() instanceof TextNode)
                            oo = new ConfigObject(o.getKey(), o.getName(),
                                    Util.removeChar(o.getValue().toString(), '"'));
                        elements.put(s, oo);
                    }
                }
                //                elements.print();
                writer = new JsonWrapper(path, JsonWrapper.GENERATE);
                writer.setElements(elements);
                writer.generate();
                reader = new JsonWrapper(path, JsonWrapper.PARSE);
                elements = reader.parse();
                //                elements.print();
                for (String s : elements.get()) {
                    for (ConfigObject co : elements.get(s)) {
                        for (String ss : map.keySet()) {
                            if (!map.get(ss).equals(co.getValue()) && ss.equals(co.getKey())) {
                                map.put(ss, co.getValue());
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            Logger.error(e);
        }
    }

}