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

import java.text.MessageFormat;

import com.tibbo.aggregate.common.Cres;
import com.tibbo.aggregate.common.data.Data;
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 ByteAtFunction extends AbstractFunction
{
  
  public ByteAtFunction()
  {
    super("byteAt", Function.GROUP_DATA_BLOCK_PROCESSING, "DataBlock data,  Integer index", "Integer", Cres.get().getString("fDescByteAt"));
  }
  
  @Override
  public Object execute(Evaluator evaluator, EvaluationEnvironment environment, Object... parameters) throws EvaluationException
  {
    checkParameters(2, false, parameters);
    Data dataBlock = (Data) parameters[0];
    int index = ((Number) parameters[1]).intValue();
    if (index >= dataBlock.getData().length || index < 0)
    {
      throw new EvaluationException(MessageFormat.format(Cres.get().getString("fErrOutOfBound"), index));
    }
    return (int) dataBlock.getData()[index];
  }
}
