Skip to content

Java Handler

Bryn Cooke edited this page Jun 26, 2013 · 13 revisions

You can use Java to handle your frames methods. This is useful if you have some logic that naturally belongs on your model.

Usage

To use @JavaHandler you must include the module when creating the graph factory:

FramedGraph framedGraph = new FramedGraphFactory(new JavaHandlerModule()).create(graph);

Here we demonstrate how the getCoCreators using the @GremlinGroovy annotation can be emulated using @JavaHandler and also a generic method that uses other methods on the frame.

interface Person {

    @GremlinGroovy("it.as('x').out('created').in('created').except('x')")
    public Iterable<Person> getCoCreators();

    @JavaHandler
    public Iterable<Person> getCoCreatorsJava();

    @JavaHandler
    public String getNameAndAge();

}

Create a class in the same package that is the same name as the frame interface with a suffix of ‘Impl’.
Note that this class must implement the original interface and may optionally implement JavaHandlerImpl with either Vertex or Edge as a parameter.
public abstract class PersonImpl implements JavaHandlerImpl<Vertex>, Person {

	public Iterable<Person> getCoCreatorsJava() {
		return frameVertices(gremlin().as("x").out("created").in("created").except("x"));
	}

	public String getNameAndAge() {
		return getName() + " (" + getAge() + ")"; //Call other frame methods that are handled by other annotations.
	}
	
}

By implementing JavaHandlerImpl your implementation has access to the following:

      FramedGraph<?> g();
C it();
GremlinPipeline<C, E> gremlin();
GremlinPipeline<C, E> gremlin(Object starts);
//... Also all the framing methods available on FramedGraph.

Custom handler factory

If you would rather be in control of the creating the handlers for your methods then you can specify a factory on the module.

JavaHandlerFactory handlerFactory = new JavaHandlerFactory() {...};
FramedGraphFactory factory = new FramedGraphFactory(new JavaHandlerModule().withFactory(handlerFactory))
FramedGraph framedGraph = factory.create(graph);
Clone this wiki locally