All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.basking2.sdsai.itrex.functions.StringJoinFunction Maven / Gradle / Ivy

There is a newer version: 1.1.23
Show newest version
package com.github.basking2.sdsai.itrex.functions;

import com.github.basking2.sdsai.itrex.EvaluationContext;

import java.util.Iterator;

public class StringJoinFunction implements FunctionInterface {
    @Override
    public String apply(final Iterator iterator, final EvaluationContext evaluationContext) {
        if (!iterator.hasNext()) {
            return "";
        }

        final String s = iterator.next().toString();

        final StringBuilder sb = new StringBuilder();

        while (iterator.hasNext()) {
            final Object o = iterator.next();
            if (o instanceof Iterator) {
                @SuppressWarnings("unchecked")
                final Iterator i = (Iterator)o;
                while (i.hasNext()) {
                    sb.append(i.next().toString()).append(s);
                }
            }
            else {
                sb.append(o.toString()).append(s);
            }
        }

        return sb.delete(sb.length() - s.length(), sb.length()).toString();
    }
}