View Javadoc

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