1 package eu.ehri.project.core.impl.neo4j; 2 3 4 import com.tinkerpop.blueprints.CloseableIterable; 5 import org.neo4j.graphdb.Node; 6 import org.neo4j.graphdb.ResourceIterable; 7 import org.neo4j.graphdb.index.IndexHits; 8 9 import java.util.Iterator; 10 11 12 public class Neo4j2VertexIterable<T extends Neo4j2Vertex> implements CloseableIterable<Neo4j2Vertex> { 13 14 private final ResourceIterable<Node> nodes; 15 private final Neo4j2Graph graph; 16 17 public Neo4j2VertexIterable(ResourceIterable<Node> nodes, Neo4j2Graph graph) { 18 this.nodes = nodes; 19 this.graph = graph; 20 } 21 22 public Iterator<Neo4j2Vertex> iterator() { 23 graph.autoStartTransaction(false); 24 return new Iterator<Neo4j2Vertex>() { 25 private final Iterator<Node> itty = nodes.iterator(); 26 27 public void remove() { 28 this.itty.remove(); 29 } 30 31 public Neo4j2Vertex next() { 32 graph.autoStartTransaction(false); 33 return new Neo4j2Vertex(this.itty.next(), graph); 34 } 35 36 public boolean hasNext() { 37 graph.autoStartTransaction(false); 38 return this.itty.hasNext(); 39 } 40 }; 41 } 42 43 public void close() { 44 if (this.nodes instanceof IndexHits) { 45 ((IndexHits) this.nodes).close(); 46 } 47 } 48 49 }