Skip to content

Java Handler

Bryn Cooke edited this page Oct 7, 2013 · 13 revisions

You can use Java to handle your frames methods. This is useful you want to embed some logic in your model.

Usage

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

FramedGraphframedGraph = newFramedGraphFactory(newJavaHandlerModule()).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.

publicinterfacePerson {
@Property("name")
publicStringgetName();
@Property("age")
publicIntegergetAge();
@GremlinGroovy("it.as('x').out('created').in('created').except('x')")
publicIterable<Person> getCoCreators();
@JavaHandlerpublicIterable<Person> getCoCreatorsJava();
@JavaHandlerpublicStringgetNameAndAge();
}

Create a nested abstract class in your frame called ‘Impl’.
Note that this class must implement the original interface and may optionally implement JavaHandlerContext with either Vertex or Edge as a parameter.
publicinterfacePerson {
//Interface methods excluded for brevity.publicabstractclassImplimplementsJavaHandlerContext<Vertex>, Person {
publicIterable<Person> getCoCreatorsJava() {
returnframeVertices(gremlin().as("x").out("created").in("created").except("x"));
}
publicStringgetNameAndAge() {
returngetName() + " (" + getAge() + ")"; //Call other methods that are handled by other annotations.
} }
}

Interacting with the underlying element.

By implementing JavaHandlerContext your implementation has access to the following:

FramedGraph<?> g(); //The graphCit(); //The element being framedGremlinPipeline<C, E> gremlin(); //Start a gremlin pipeline from the framed elementGremlinPipeline<C, E> gremlin(Objectstarts); //Start a gremlin pipeline from an element//... Also all the framing methods available on FramedGraph.

Frame initialization.

To perform initialization on the frame annotate a no-args method with @Initializer. It will be called when a frame is created via the FramedGraph.addVertex or FramedGraph.addEdge.

publicinterfacePerson {
//Interface methods excluded for brevity.publicabstractclassImplimplementsJavaHandlerContext<Vertex>, Person {
@Initializerpublicvoidinit() {
//This will be called when a new framed element is added to the graph.setAge(23);//Set the default age
}
}
}

Specifying an alternative implementation class

You can specify an alternative class for your JavaHandler implementation using the @JavaHandlerClass annotation.

@JavaHandlerClass(MyPersonImpl.class)
public interface Person {
}

In this case MyPersonImpl will be used in preference to the nested Impl class.

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.

JavaHandlerFactoryhandlerFactory = newJavaHandlerFactory() {...};
FramedGraphFactoryfactory = newFramedGraphFactory(newJavaHandlerModule().withFactory(handlerFactory))
FramedGraphframedGraph = factory.create(graph);

Clone this wiki locally