01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.algebra.evaluation.iterator;
07:
08: import info.aduna.iteration.CloseableIteration;
09: import info.aduna.iteration.LookAheadIteration;
10:
11: import org.openrdf.query.BindingSet;
12: import org.openrdf.query.QueryEvaluationException;
13: import org.openrdf.query.algebra.Join;
14: import org.openrdf.query.algebra.evaluation.EvaluationStrategy;
15:
16: public class JoinIterator extends
17: LookAheadIteration<BindingSet, QueryEvaluationException> {
18:
19: /*-----------*
20: * Constants *
21: *-----------*/
22:
23: private final EvaluationStrategy strategy;
24:
25: private final Join join;
26:
27: /*-----------*
28: * Variables *
29: *-----------*/
30:
31: private CloseableIteration<BindingSet, QueryEvaluationException> leftIter;
32:
33: private CloseableIteration<BindingSet, QueryEvaluationException> rightIter;
34:
35: /*--------------*
36: * Constructors *
37: *--------------*/
38:
39: public JoinIterator(EvaluationStrategy strategy, Join join,
40: BindingSet bindings) throws QueryEvaluationException {
41: this .strategy = strategy;
42: this .join = join;
43:
44: leftIter = strategy.evaluate(join.getLeftArg(), bindings);
45: }
46:
47: /*---------*
48: * Methods *
49: *---------*/
50:
51: @Override
52: protected BindingSet getNextElement()
53: throws QueryEvaluationException {
54: while (rightIter != null || leftIter.hasNext()) {
55: if (rightIter == null) {
56: rightIter = strategy.evaluate(join.getRightArg(),
57: leftIter.next());
58: }
59:
60: if (rightIter.hasNext()) {
61: return rightIter.next();
62: } else {
63: rightIter.close();
64: rightIter = null;
65: }
66: }
67:
68: return null;
69: }
70:
71: @Override
72: protected void handleClose() throws QueryEvaluationException {
73: if (rightIter != null) {
74: rightIter.close();
75: rightIter = null;
76: }
77:
78: leftIter.close();
79:
80: super.handleClose();
81: }
82: }
|