1 package eu.ehri.project.acl.wrapper;
2
3 import com.tinkerpop.blueprints.CloseableIterable;
4 import com.tinkerpop.blueprints.Vertex;
5
6 import java.util.Iterator;
7 import java.util.NoSuchElementException;
8
9
10 public class AclVertexIterable implements CloseableIterable<Vertex> {
11 private final Iterable<Vertex> iterable;
12 private final AclGraph<?> aclGraph;
13
14 public AclVertexIterable(Iterable<Vertex> iterable, AclGraph<?> aclGraph) {
15 this.iterable = iterable;
16 this.aclGraph = aclGraph;
17 }
18
19 @Override
20 public void close() {
21 if (this.iterable instanceof CloseableIterable) {
22 ((CloseableIterable) iterable).close();
23 }
24 }
25
26 @Override
27 public Iterator<Vertex> iterator() {
28 return new Iterator<Vertex>() {
29 private final Iterator<Vertex> itty = iterable.iterator();
30 private AclVertex nextVertex;
31
32 @Override
33 public boolean hasNext() {
34 if (null != this.nextVertex) {
35 return true;
36 }
37 while (this.itty.hasNext()) {
38 Vertex vertex = this.itty.next();
39 if (aclGraph.evaluateVertex(vertex)) {
40 this.nextVertex = new AclVertex(vertex, aclGraph);
41 return true;
42 }
43 }
44 return false;
45
46 }
47
48 @Override
49 public Vertex next() {
50 if (null != this.nextVertex) {
51 AclVertex temp = this.nextVertex;
52 this.nextVertex = null;
53 return temp;
54 } else {
55 while (this.itty.hasNext()) {
56 Vertex vertex = this.itty.next();
57 if (aclGraph.evaluateVertex(vertex)) {
58 return new AclVertex(vertex, aclGraph);
59 }
60 }
61 throw new NoSuchElementException();
62 }
63 }
64
65 @Override
66 public void remove() {
67 throw new UnsupportedOperationException();
68 }
69 };
70 }
71 }