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

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Stack;

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

public class BytesToDouble extends PostfixMathCommand {
    public BytesToDouble() {
        numberOfParameters = 1;
    }
    
//  Byte 문자를 Double 형 Binary(8Bytes) 로
    public void run(Stack inStack) throws ParseException {
    	
    	checkStack(inStack);

        Object p1 = inStack.pop();
        
        double d = 0;

        if (p1 instanceof byte[]) {
            d = Double.parseDouble(new String((byte[]) p1));
        } else if (p1 instanceof String) {
            d = Double.parseDouble((String) p1);
        } else if (p1 instanceof Number) {
            d = ((Number) p1).doubleValue();
        } else {
            throw new ParseException("Invalid parameter type");
        }
        
        ByteBuffer buff = ByteBuffer.allocate(Double.SIZE / 8);
	    buff.putDouble(d);
		buff.order(ByteOrder.BIG_ENDIAN);
        inStack.push(buff.array());    	
    }
}
