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

import java.util.Stack;

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

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

	@SuppressWarnings("unchecked")
	public void run(Stack inStack) throws ParseException {
		checkStack(inStack);

		int n = curNumberOfParameters;
		if (n < 1) {
			throw new ParseException("concatN() requires at least 1 parameter");
		}

		String[] params = new String[n];
		for (int i = n - 1; i >= 0; i--) {
			Object p = inStack.pop();
			if (!(p instanceof String)) {
				throw new ParseException("Invalid parameter type: parameter " + (i + 1) + " is not a string");
			}
			params[i] = (String) p;
		}

		StringBuilder sb = new StringBuilder();
		for (String s : params) {
			sb.append(s);
		}

		inStack.push(sb.toString());
	}
}
