// Settings
//
// Douglas Thrift
//
// Settings.java

/*  Copyright 2011 Douglas Thrift
 *
 *  This file is part of Big Screen Bot.
 *
 *  Big Screen Bot is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Big Screen Bot is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Big Screen Bot.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.douglasthrift.bigscreenbot;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

public class Settings extends Properties
{
    private static final String SETTINGS = "bigscreenbot.properties";

    public boolean getBooleanProperty(String key, boolean defaultValue)
    {
        String value = getProperty(key);

        if (value != null)
            return Boolean.parseBoolean(value);
        else
            return defaultValue;
    }

    public int getIntegerProperty(String key, int defaultValue)
    {
        String value = getProperty(key);

        if (value != null)
            return Integer.parseInt(value);
        else
            return defaultValue;
    }

    public List<String> getListProperty(String key)
    {
        String value = getProperty(key);

        if (value != null)
            return Arrays.asList(StringUtils.split(value));
        else
            return null;
    }

    public List<String> getListProperty(String key, List<String> defaultValue)
    {
        String value = getProperty(key);

        if (value != null)
            return Arrays.asList(StringUtils.split(value));
        else
            return defaultValue;
    }

    public List<String> setListProperty(String key, Collection<String> value)
    {
        String oldValue = (String)setProperty(key, StringUtils.join(value, ' '));

        if (oldValue != null)
            return Arrays.asList(StringUtils.split(oldValue));
        else
            return null;
    }

    public void load() throws FileNotFoundException, IOException
    {
        FileReader reader = null;

        try
        {
            reader = new FileReader(SETTINGS);

            load(reader);
        }
        finally
        {
            if (reader != null)
                reader.close();
        }
    }

    public void store() throws IOException
    {
        FileWriter writer = null;

        try
        {
            writer = new FileWriter(SETTINGS);

            store(writer, null);
        }
        finally
        {
            if (writer != null)
                writer.close();
        }
    }
}

// vim: expandtab
