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

import java.util.Stack;

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


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

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

        Object p1 = inStack.pop();

        if (p1 instanceof byte[]) {
        	inStack.push(p1);
        } else if (p1 instanceof String) { 	
            inStack.push(((String) p1).getBytes());
        } else if (p1 instanceof Double) {
        	if (((Double) p1).doubleValue() == Math.round(((Double) p1).doubleValue())) {
        		inStack.push(new Long(Math.round(((Double) p1).doubleValue())).toString().getBytes());
        	} else {
                inStack.push(((Number)p1).toString().getBytes());
        	}
        } else if (p1 instanceof Number) {
            inStack.push(((Number)p1 ).toString().getBytes());
        } else {
            throw new ParseException("Invalid parameter type ["+p1+"]");
        }
    }
}
