All Downloads are FREE. Search and download functionalities are using the official Maven repository.

toxgene.core.genes.trees.Constant Maven / Gradle / Ivy

/**
 * Wrapper for constant values in complex expressions.
 * 
 * @author Denilson Barbosa
 * @version 0.1
 */

package toxgene.core.genes.trees;

import toxgene.core.ToXgeneErrorException;
import toxgene.core.genes.lists.ToxListElement;
import toxgene.util.Date;
import toxgene.util.DateFormatException;

import java.util.Vector;

public class Constant implements Expression{
  public Vector value;
  public int type;
  
  public Constant(String constant){
	value = new Vector(1);

	//check whether this is is a STRING or DATE constant
	if ((constant.charAt(0) == '\'') && 
		(constant.charAt(constant.length()-1) == '\'')){

	   String c = constant.substring(1,constant.length()-1);

	  //check if it is a date
	  if (c.indexOf('-') == -1){
		//it is string
		type = Expression.STRING;
		value.add(c);
	  }
	  else{
		try{
		  Date temp = Date.parseDate(c);
		  type = Expression.DATE;
		  value.add(c);
		}
		catch (DateFormatException e){
		  //treat it as a string
		  type = Expression.STRING;
		  value.add(c);
		}
	  }
	}
	else{
	  //check if it is a real number
	  if (constant.indexOf('.') != -1){
		try{
		  Double temp = Double.valueOf(constant);
		}
		catch (NumberFormatException e){
		  throw new ToXgeneErrorException("invalid REAL constant: "+constant);
		}
		type = Expression.REAL;
		value.add(constant);
	  }
	  else{
		// last try, this could be an integer value
		try{
		  Long temp = Long.valueOf(constant);
		}
		catch (NumberFormatException e){
		  throw new ToXgeneErrorException("invalid constant: "+constant);
		}
		type = Expression.INTEGER;
		value.add(constant);
	  }
	}
  }

  public Vector evaluate(){
	return value;
  }

  public Vector evaluate(ToxListElement element){
	return value;
  }

  public int expressionType(){
	return type;
  }

  public int getQtty(){
	return 1;
  }

  public String expression(){
	if ((type == Expression.STRING) || (type == Expression.DATE)){
	  return ("'"+(String) value.get(0)+"'");
	}
	return ((String) value.get(0));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy