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

com.tinkerpop.gremlin.groovy.console.ResultHookClosure Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package com.tinkerpop.gremlin.groovy.console;

import com.tinkerpop.pipes.util.iterators.SingleIterator;
import groovy.lang.Closure;
import org.codehaus.groovy.tools.shell.IO;

import java.util.Iterator;
import java.util.Map;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class ResultHookClosure extends Closure {
    private final String resultPrompt;
    private final IO io;

    public ResultHookClosure(final Object owner, final IO io, final String resultPrompt) {
        super(owner);
        this.io = io;
        this.resultPrompt = resultPrompt;
    }

    public Object call(final Object[] args) {
        final Object result = args[0];
        final Iterator itty;
        if (result instanceof Iterator) {
            itty = (Iterator) result;
        } else if (result instanceof Iterable) {
            itty = ((Iterable) result).iterator();
        } else if (result instanceof Object[]) {
            itty = new ArrayIterator((Object[]) result);
        } else if (result instanceof Map) {
            itty = ((Map) result).entrySet().iterator();
        } else {
            itty = new SingleIterator(result);
        }

        while (itty.hasNext()) {
            this.io.out.println(this.resultPrompt + itty.next());
        }

        return null;
    }
}