package com.tibbo.aggregate.common.expression.function.string;

import java.text.MessageFormat;

import com.tibbo.aggregate.common.Cres;
import com.tibbo.aggregate.common.expression.EvaluationEnvironment;
import com.tibbo.aggregate.common.expression.EvaluationException;
import com.tibbo.aggregate.common.expression.Evaluator;
import com.tibbo.aggregate.common.expression.Function;
import com.tibbo.aggregate.common.expression.function.AbstractFunction;

public class CharAtFunction extends AbstractFunction
{
  
  public CharAtFunction()
  {
    super("charAt", Function.GROUP_STRING_PROCESSING, "String string, Integer index", "integer", Cres.get().getString("fDescCharAt"));
  }
  
  @Override
  public Object execute(Evaluator evaluator, EvaluationEnvironment environment, Object... parameters) throws EvaluationException
  {
    checkParameters(2, false, parameters);
    String string = parameters[0].toString();
    int index = ((Number) parameters[1]).intValue();
    if (index >= string.length())
    {
      throw new EvaluationException(MessageFormat.format(Cres.get().getString("fErrOutOfBound"), index));
    }
    return (int) string.charAt(index);
  }
}
