/************************************************
* Columbia University CS Department
* Network Security Lab
*
* Open and Survivable Embedded Systems (OASES)
*
* Structured Policy Command Language (SPCL)
*
*
* This software is provided as is without any
* warranty or guarantee of merchantibility
* or fitness for a particular purpose. This
* software is released under the terms of the
* GNU General Public License (GPL).
*
* Last updated: March 27, 2003
*
***********************************************/

package spcl.data;

/** 
 * An object representing optional variables
 * associated with a principal or systemobject.
 *
 * @author locasto@cs.columbia.edu
 *
*/
public class Variable{

	public static final int STRING = 100;
	public static final int BOOLEAN = 102;
	public static final int NUMBER = 104;

	private String name = "null";
	private int type = Variable.STRING;
	private String svalue="";
	private int nvalue=0;
	private boolean bvalue=false;

	public Variable(){}

	public Variable( String n, int t, String v ){
		this.name = n;
		setType( t );
		setValue( v );
	}

	public String getName(){
		return this.name;
	}

	public int getNumberValue(){ return nvalue; }
	public String getStringValue(){ return svalue; }
	public boolean getBooleanValue(){ return bvalue; }

	public int getType(){ return type; }

	public void setType( int t ){
		switch( t ){
			case Variable.STRING: this.type = t; break;
			case Variable.BOOLEAN: this.type = t;  break;
			case Variable.NUMBER: this.type = t; break;
			default: this.type = Variable.STRING;
		}
	}

	public void setValue( String v ){
		if( v==null ){
			bvalue=false;
			nvalue=0;
			svalue="";
		}

		switch( this.type ){
			case Variable.STRING: svalue = v; break;
			case Variable.BOOLEAN:
				if(v.equals( "true" )){
					bvalue=true;
				}else{
					bvalue=false;
				}
				break;
			case Variable.NUMBER:
				try{
					nvalue=Integer.parseInt( v );
				}catch(Exception e){
					svalue=v;
				}
				break;
			default:
				svalue=v;
		}
	}

	public String toString(){
		switch( type ){
			case Variable.STRING: return svalue;
			case Variable.NUMBER: return ""+nvalue;
			case Variable.BOOLEAN: return ""+bvalue;
			default: return "";
		}
	}
}
