import java.lang.reflect.Constructor;
/**
 * A triple that contains an option, its type,and its value.
 * To be used by Options class to store available options and their values.
 * @author Wanlin Liu
 * @Version 1.1 at November, 1999
 */
public class OptionField {
    //+--------+------------------------------------------------------
    //| Fields |
    //+--------+
    
    // Name of the option.
    String optionName;
    // Type of option.
    String optionType;
    // value of the option. 
    Object optionValue;
    
    //+--------------+---------------------------------------------------------
    //| Constructors |
    //+--------------+
    
    /**
     * This constuctors takes the option name, option type, and the value
     * and builds a new optionField.
     */
    public OptionField (String name, String type, Object value) {
        this.optionName = name;
        this.optionType = type;
        this.optionValue = value;
    }
           

    /**
     * Takes a string produced by the toString method in this class and
     * rebuild an object.
     */
    public OptionField(String fromToString) 
        throws OptionException{
        // Step 1, get the index of the first comma.
        int i = fromToString.indexOf(",");
        // Step 2, the substring from the beginning to the first comma is the name.
        this.optionName = fromToString.substring(0, i);
        // Step 3, Cut the first part off.
        String str = fromToString.substring(i+1);
        // Step 4, get the index of the comma.
        i = str.indexOf(",");
        // Step 5, the substring before the  commas is the option type.  
        this.optionType = str.substring(0, i);       
        // Step 6, the substring from the last comma to the end is the option value.
        str = str.substring(i+1);
        try {
            this.optionValue = Classist.buildOne(this.optionType, str);        
        }// try
        catch(Exception e) {
        }// catch
    }//OptiionField(String)
    
    
    
    //+-----------+--------------------------------------------------
    //| Modifiers |
    //+-----------+
    /**
     * Change the value of the optionField.
     */
    public void setOptionValue(Object newVal) {
        this.optionValue = newVal;
    }
    
    /**
     * Change the value of the option type.
     */
    public void setOptionType(String newType) {
        this.optionType = newType;
    }
    //+------------+----------------------------------------------------
    //| Extractors |
    //+------------+
    /** get the value of the optionField. */
    public Object getOptionValue() {
        return optionValue;
    }
    
    /** get the type of the option.*/
    public String getOptionType() {
        return optionType;
    }

    /** get the name of the optionField. */
    public String getOptionName() {
        return optionName;
    }

    /** PUt the information into a string. */
    public String toString() {
        return optionName + "," + optionType + "," + optionValue.toString() + "\n";
    }// toString()

    // Testing
    public static void main(String[] args) {
        OptionField opt1 = new OptionField("quote", "java.lang.Boolean", "true");
        OptionField opt2 = new OptionField("lineWidth", "java.lang.Integer", "64");
        OptionField opt3 = new OptionField("fullName", "Name", new Name("Liu","Wanlin"));
        System.out.println("opt1 is: " + opt1.toString());
        System.out.println("opt2 is: " + opt2.toString());
        System.out.println("opt3 is: " + opt3.toString());
        System.out.println("opt4, opt5, and opt6 should match opt1, opt2, and opt3");
        try{
            OptionField opt4 = new OptionField(opt1.toString());
            OptionField opt5 = new OptionField(opt2.toString());
            OptionField opt6 = new OptionField(opt3.toString());

            System.out.println("opt4 is: " + opt4.toString());
            System.out.println("opt5 is: ");
            System.out.println(opt5.getOptionValue().toString());
            System.out.println("opt6 is: " + opt6.toString());
        }// try
        catch(Exception e) {
            System.out.println(e.toString());
        }
        
    }// main(String[])
}//class OptionField

