01: package org.ontoware.rdf2go.model.impl;
02:
03: import java.util.Iterator;
04:
05: import org.ontoware.aifbcommons.collection.ClosableIterator;
06: import org.ontoware.rdf2go.model.Model;
07: import org.ontoware.rdf2go.model.Statement;
08: import org.ontoware.rdf2go.model.TriplePattern;
09:
10: /**
11: * Returns all statements matching the pattern. Looks into each model.
12: * TODO test me
13: * @author voelkel
14: */
15: public class LazyUnionModelIterator implements
16: ClosableIterator<Statement> {
17:
18: private TriplePattern pattern;
19:
20: private Iterator<? extends Model> modelit;
21:
22: private ClosableIterator<? extends Statement> stmtit;
23:
24: public LazyUnionModelIterator(AbstractModelSetImpl impl,
25: TriplePattern pattern) {
26: this .pattern = pattern;
27: // create iterator over models
28: this .modelit = impl.getModels();
29: }
30:
31: public void close() {
32: this .stmtit.close();
33: }
34:
35: public boolean hasNext() {
36: getNonEmptyStmtIterator();
37: return this .stmtit.hasNext();
38: }
39:
40: public Statement next() {
41: getNonEmptyStmtIterator();
42: return this .stmtit.next();
43: }
44:
45: private void getNonEmptyStmtIterator() {
46: // make sure we have a statement iterator
47: if (this .stmtit == null) {
48: Model m = this .modelit.next();
49: m.open();
50: this .stmtit = m.findStatements(this .pattern);
51: }
52: // make sure we have a statement iterator, which contains statements
53: while (this .modelit.hasNext() && !this .stmtit.hasNext()) {
54: this .stmtit.close();
55: Model m = this .modelit.next();
56: m.open();
57: this .stmtit = m.findStatements(this .pattern);
58: }
59: }
60:
61: public void remove() {
62: getNonEmptyStmtIterator();
63: this.stmtit.remove();
64: }
65:
66: }
|