Skip to content

Instantly share code, notes, and snippets.

@okram
Created December 17, 2013 19:45
Show Gist options
  • Save okram/8011350 to your computer and use it in GitHub Desktop.
Save okram/8011350 to your computer and use it in GitHub Desktop.
package com.tinkerpop.blueprints.extension;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.util.ElementHelper;
import java.util.Arrays;
import java.util.Optional;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class IdExtension implements Extension {
private final Extension.VertexExtension vertexExtension;
private final Extension.GraphQueryExtension graphQueryExtension;
private final String idKey;
public IdExtension(final String idKey) {
this.vertexExtension = new VertexExtension();
this.graphQueryExtension = new GraphQueryExtension();
this.idKey = idKey;
}
public Optional<Extension.VertexExtension> vertex() {
return Optional.of(this.vertexExtension);
}
public Optional<Extension.GraphQueryExtension> graphQuery() {
return Optional.of(this.graphQueryExtension);
}
public class GraphExtension implements Extension.GraphExtension {
public Object[] beforeAddVertex(Graph graph, Object... keyValues) {
Object[] newKeyValues = Arrays.copyOf(keyValues, keyValues.length + 2);
newKeyValues[keyValues.length] = idKey;
newKeyValues[keyValues.length + 1] = ElementHelper.getIdValue(keyValues);
return newKeyValues;
}
}
public class GraphQueryExtension implements Extension.GraphQueryExtension {
}
public class VertexExtension implements Extension.VertexExtension {
public Object getId(Vertex vertex) {
return vertex.getProperty(idKey);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment