/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Karl Hasselström

This program 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 2 of the License, or (at
your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.util;

import java.io.*;
import java.util.*;

/**
 *  Description of the Class
 *
 * @author     Karl Hasselström
 * @created    June 27, 2001
 */
public class CommandLineProperties extends Properties {
    /**
     *  Constructor for the CommandLineProperties object
     */
    public CommandLineProperties() {
        super();
    }


    /**
     *  Constructor for the CommandLineProperties object
     *
     * @param  args  Description of Parameter
     */
    public CommandLineProperties(String[] args) {
        super();
        loadCommandLine(args);
    }


    /**
     *  Gets the DoubleProperty attribute of the CommandLineProperties object
     *
     * @param  key         Description of Parameter
     * @param  defaultVal  Description of Parameter
     * @return             The DoubleProperty value
     */
    public double getDoubleProperty(String key, double defaultVal) {
        try {
            return Double.parseDouble(getProperty(key));
        } catch (Exception e) {
            return defaultVal;
        }
    }


    /**
     *  Gets the IntProperty attribute of the CommandLineProperties object
     *
     * @param  key         Description of Parameter
     * @param  defaultVal  Description of Parameter
     * @return             The IntProperty value
     */
    public int getIntProperty(String key, int defaultVal) {
        try {
            return Integer.parseInt(getProperty(key));
        } catch (Exception e) {
            return defaultVal;
        }
    }


    /**
     *  Gets the StringProperty attribute of the CommandLineProperties object
     *
     * @param  key         Description of Parameter
     * @param  defaultVal  Description of Parameter
     * @return             The StringProperty value
     */
    public int getStringProperty(String key, String defaultVal) {
        return Integer.parseInt(getProperty(key, defaultVal));
    }


    /**
     *  Description of the Method
     *
     * @param  args  Description of Parameter
     */
    public void loadCommandLine(String[] args) {
        String key;
        String value;
        int i = 0;
        try {
            while (true) {
                key = args[i];
                i++;
                value = args[i];
                i++;
                setProperty(key, value);
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            // This means we've reached the end of the argument list.
        }
    }
}
