// ============================================================================
// Attribute                                                                 //
//                                                                           //
// Luis Francisco-Revilla                                                    //
// Sept 25, 2000                                                             //
//                                                                           //
// ============================================================================
import java.util.*;
import java.io.*;

// ============================================================================
// class Attribute                                                    Attribute
// ============================================================================
public class Attribute {

    // Constants                                                      Constants
    // ========================================================================

    //Type
    public final static String TYPE_REAL   = "REAL";
    public final static String TYPE_INT    = "INT";
    public final static String TYPE_SYMBOL = "SYM";

    //Interpretation
    public final static String CONTINUOUS = "CONT";
    public final static String DISCRETE   = "DISCR";

    // Members                                                          Members
    // ========================================================================
    protected String name;
    protected String type;
    protected String interpretation;
    protected Vector values;

    protected int[]  counters;
    protected int    maxCountIndex;
    protected int    maxCount;

    // Constructor                                                  Constructor
    // ========================================================================
    public Attribute(String parameters) {

        StringTokenizer commaTokenizer = new StringTokenizer(parameters,",",false);
        name           = commaTokenizer.nextToken();
        type           = commaTokenizer.nextToken();
        interpretation = commaTokenizer.nextToken();

        values = new Vector();
        for(int i=0;commaTokenizer.hasMoreTokens();i++){
            String tmpToken = commaTokenizer.nextToken();
            values.addElement(tmpToken);
        }

        resetCounters();

    }//Constructor

    // Constructor                                                  Constructor
    // ========================================================================
    public Attribute(String newName, String newType, String newInterpretation, Vector newValues) {
        name=newName;
        type=newType;
        interpretation=newInterpretation;
        values=newValues;
        resetCounters();

    }//Constructor

    // Constructor                                                  Constructor
    // ========================================================================
    public Attribute(Attribute otherAttribute) {
        name=otherAttribute.getName();
        type=otherAttribute.getType();
        interpretation=otherAttribute.getInterpretation();
        values=otherAttribute.getAllValuesVector();
        resetCounters();
        
    }//Constructor

    // Constructor                                                  Constructor
    // ========================================================================
    public Attribute() {
        name="";
        type="";
        interpretation="";
        values=new Vector();
    }//Constructor


    // Methods                                                          Methods
    // ========================================================================


    // resetCounters                                              resetCounters
    // ------------------------------------------------------------------------
    public void resetCounters() {
        //initialize counters array
        counters = new int[values.size()];
        maxCountIndex = 0;
        maxCount = 0;
        for(int i=0; i<counters.length;i++){
            counters[i]=0;
        }
    }//resetCounters


    // count                                                              count
    // ------------------------------------------------------------------------
    public void count(String tmpValue) {
        //count occurence of tmpoValue
        for(int i=0;i<values.size();i++) {
            if(values.elementAt(i).equals(tmpValue)){
                counters[i]++;
                //store maxCount
                if(counters[i]>maxCount){
                    maxCountIndex = i;
                    maxCount=counters[i];
                }
            }
        }
    }//count


    // getMajorityValue                                        getMajorityValue
    // ------------------------------------------------------------------------
    public String getMajorityValue() {
        return (String)values.elementAt(maxCountIndex);
    }//getMajorityValue


    // getMaxCount                                                  getMaxCount
    // ------------------------------------------------------------------------
    public int getMaxCount() {
        return maxCount;
    }//getMaxCount


    // getMaxCountIndex                                        getMaxCountIndex
    // ------------------------------------------------------------------------
    public int getMaxCountIndex() {
        return maxCountIndex;
    }//getMaxCountIndex


    // getCounters                                                  getCounters
    // ------------------------------------------------------------------------
    public int[] getCounters() {
        return counters;
    }//getCounters


    // setName                                                          setName
    // ------------------------------------------------------------------------
    public void setName(String tmpValue) {
        name = tmpValue;
    }//setName


    // getName                                                          getName
    // ------------------------------------------------------------------------
    public String getName() {
        return name;
    }//getName


    // setType                                                          setType
    // ------------------------------------------------------------------------
    public void setType(String tmpValue) {
        type = tmpValue;
    }//setType


    // getType                                                          getType
    // ------------------------------------------------------------------------
    public String getType() {
        return type;
    }//getType


    // setInterpretation                                      setInterpretation
    // ------------------------------------------------------------------------
    public void setInterpretation(String tmpValue) {
        interpretation = tmpValue;
    }//setInterpretation


    // getInterpretation                                      getInterpretation
    // ------------------------------------------------------------------------
    public String getInterpretation() {
        return interpretation;
    }//getInterpretation


    // setValue                                                        setValue
    // ------------------------------------------------------------------------
    public void setValue(Vector tmpValue) {
        values = (Vector)tmpValue.clone();
    }//setValue


    // addValue                                                        addValue
    // ------------------------------------------------------------------------
    public void addValue(String tmpValue) {
        values.addElement(tmpValue);
    }//addValue


    // getValuesString                                          getValuesString
    // ------------------------------------------------------------------------
    public String getValuesString() {
        String tmpValuesString = (String)values.elementAt(0);
        for(int i=1;i<values.size();i++){
            tmpValuesString += ","+(String)values.elementAt(i);
        }
        return tmpValuesString;
    }//getValuesString


    // getAllValuesVector                                    getAllValuesVector
    // ------------------------------------------------------------------------
    public Vector getAllValuesVector() {
        values.trimToSize();
        return values;
    }//getAllValuesVector


    // getValue                                                        getValue
    // ------------------------------------------------------------------------
    public String getValue(int index) {
        return (String)values.elementAt(index);
    }//getValue


    
    // toString                                                        toString
    // ------------------------------------------------------------------------
    public String toString() {
        String tmpOutput = "";
        tmpOutput += name + "," + type + "," + interpretation;
        for(int i=0;i<values.size();i++) {
            tmpOutput += ","+values.elementAt(i);
        }
        return tmpOutput;
    }//getValue


}//class Attribute


