Created
May 14, 2012 21:44
-
-
Save okram/2697481 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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