Question

I recently bumped into the ResultConverter interface in Neo4j when examining the following method on the RestAPIFacade class...

org.neo4j.rest.graphdb.RestAPIFacade.query(String statement, Map<String, Object> params, ResultConverter resultConverter)

Thinking it might be involved in the conversion of a query result to a specified java class as the code below suggests..

public interface ResultConverter<T, R> {
R convert(T value, Class<R> type);

ResultConverter NO_OP_RESULT_CONVERTER = new ResultConverter() {
    @Override
    public Object convert(Object value, Class type) {
        return null;
    }
};

}

I started digging around for documentation on usage of the interface and what the types T and R(I'm kind of suspecting that the R might be the class to convert to) but I've come up short so far. Can anyone give me a heads up on what this is supposed to do in the context of the query method? Examples would really help.

Thanks.

Was it helpful?

Solution

If you look at the result type of the query method in RestAPIFacade, you'll see that it returns a QueryResult<T>

on QueryResult you have a bunch of methods to convert your result to other types, and these methods then use the result-converter to do the conversion.

public interface QueryResult<T> extends Iterable<T> {
    <R> ConvertedResult<R> to(Class<R> type);
    <R> ConvertedResult<R> to(Class<R> type, ResultConverter<T, R> resultConverter);
    void handle(Handler<T> handler);
}

Where the to methods return a ConvertedResult which then is either an Iterable of type R or has methods to access a single value of type R.

public interface ConvertedResult<R> extends Iterable<R> {
    R single();
    R singleOrNull();
    void handle(Handler<R> handler);
}

So in this case T is Map<String, Object> and R would be your target type. The default Converter supports conversion to node and path objects and vice versa. See the implementation here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top