package com.eactive.eai.transformer.function.generic;

import java.util.Stack;

import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;


public class ToInteger extends PostfixMathCommand {
    public ToInteger() {
        numberOfParameters = 1;
    }

    public void run(Stack inStack) throws ParseException {
        checkStack(inStack);

        Object p1 = inStack.pop();

        if (p1 instanceof byte[]) {
            inStack.push(Integer.valueOf(new String((byte[]) p1)));
        } else if (p1 instanceof String) {
            inStack.push(Integer.valueOf((String) p1));
        } else if (p1 instanceof Number) {
            inStack.push(((Number) p1).intValue());
        } else {
            throw new ParseException("Invalid parameter type, p1 is not string/bytes/number");
        }
    }
}
