package com.airhacks.enhydrator.transform;

import com.airhacks.enhydrator.in.Entry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 *
 * @author airhacks.com
 */
public class Expression {

    private final ScriptEngineManager manager;
    private final ScriptEngine engine;

    public Expression() {
        this.manager = new ScriptEngineManager();
        this.engine = manager.getEngineByName("nashorn");
    }

    public List<Entry> execute(List<Entry> columns, Entry entry, String expression) {
        List<Entry> results = new ArrayList<>();
        Bindings bindings = this.engine.createBindings();
        bindings.put("columns", columns);
        bindings.put("current", entry);
        try {
            Object result = this.engine.eval(expression, bindings);
            if (!(result instanceof List)) {
                return Collections.EMPTY_LIST;
            }
        } catch (ScriptException ex) {
            throw new IllegalStateException(ex.getMessage(), ex);
        }
        return results;
    }
}

@@ -155,24 +155,60 @@
 .util.List;%0A
+import java.util.function.Consumer;%0A
 import javax
@@ -490,34 +490,237 @@
 ne;%0A
-%0A    public Expression() %7B
+    private Consumer%3CString%3E expressionListener;%0A%0A    public Expression() %7B%0A        this(l -%3E %7B%0A        %7D);%0A    %7D%0A%0A    public Expression(Consumer%3CString%3E expressionListener) %7B%0A        this.expressionListener = expressionListener;
 %0A   
@@ -921,57 +921,8 @@
 ) %7B%0A
-        List%3CEntry%3E results = new ArrayList%3C%3E();%0A
     
@@ -1087,61 +1087,202 @@
     
-Object result = this.engine.eval(expression, bindings
+this.expressionListener.accept(%22Executing: %22 + expression);%0A            Object result = this.engine.eval(expression, bindings);%0A            this.expressionListener.accept(%22Got result: %22 + result
 );%0A 
@@ -1372,16 +1372,82 @@
 Y_LIST;%0A
+            %7D else %7B%0A                return (List%3CEntry%3E) result;%0A
         
@@ -1571,32 +1571,8 @@
   %7D%0A
-        return results;%0A
     

Return fixed