Skip to content

Instantly share code, notes, and snippets.

@okram
Created May 14, 2012 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okram/2697481 to your computer and use it in GitHub Desktop.
Save okram/2697481 to your computer and use it in GitHub Desktop.
package com.tinkerpop.blueprints.util;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import java.util.Iterator;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class VerticesFromEdgesIterable implements Iterable<Vertex> {
private final Iterable<Edge> iterable;
private final Direction direction;
private final Vertex vertex;
public VerticesFromEdgesIterable(final Vertex vertex, final Direction direction, final String... labels) {
this.direction = direction;
this.vertex = vertex;
this.iterable = vertex.getEdges(direction, labels);
}
public Iterator<Vertex> iterator() {
return new Iterator<Vertex>() {
Iterator<Edge> itty = iterable.iterator();
public void remove() {
itty.remove();
}
public boolean hasNext() {
return itty.hasNext();
}
public Vertex next() {
if (direction.equals(Direction.OUT)) {
return itty.next().getInVertex();
} else if (direction.equals(Direction.IN)) {
return itty.next().getOutVertex();
} else {
final Edge edge = itty.next();
if (edge.getInVertex().equals(vertex))
return edge.getOutVertex();
else
return edge.getInVertex();
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment