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

import java.util.Stack;

import org.apache.commons.lang3.StringUtils;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;

public class ifblank extends PostfixMathCommand {

	public ifblank() {
		numberOfParameters = 2;
	}

	public void run(Stack inStack) throws ParseException {

		checkStack(inStack);

		Object p1 = inStack.pop();
		Object p2 = inStack.pop();

		if ( p2 instanceof String ) {
			String str1 = (String) p1;

			if ( StringUtils.isBlank(str1) ){
				inStack.push(p2);
			}else {
				inStack.push(p1);
			}

		} else {
			throw new ParseException("Invalid parameter type, p2 is not string");
		}
	}
	
	public static void main(String[] args) throws ParseException {
		Stack sk = new Stack<Object>();
		
		sk.push("default");
		sk.push("asdf");
		
		new ifblank().run(sk);
		
		// System.out.println(sk.pop());
		
		
	}

}
