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 java.util.List;
09:
10: import info.aduna.iteration.CloseableIteration;
11: import info.aduna.iteration.CloseableIterationBase;
12: import info.aduna.iteration.Iteration;
13:
14: import org.openrdf.query.BindingSet;
15: import org.openrdf.query.QueryEvaluationException;
16: import org.openrdf.query.algebra.MultiProjection;
17: import org.openrdf.query.algebra.ProjectionElemList;
18:
19: public class MultiProjectionIterator extends
20: CloseableIterationBase<BindingSet, QueryEvaluationException> {
21:
22: /*-----------*
23: * Constants *
24: *-----------*/
25:
26: private final List<ProjectionElemList> projections;
27:
28: private final Iteration<BindingSet, QueryEvaluationException> iter;
29:
30: private final BindingSet parentBindings;
31:
32: /*-----------*
33: * Variables *
34: *-----------*/
35:
36: private BindingSet currentBindings;
37:
38: private int nextProjectionIdx;
39:
40: /*--------------*
41: * Constructors *
42: *--------------*/
43:
44: public MultiProjectionIterator(
45: MultiProjection multiProjection,
46: CloseableIteration<BindingSet, QueryEvaluationException> iter,
47: BindingSet bindings) {
48: this .projections = multiProjection.getProjections();
49: this .iter = iter;
50: this .parentBindings = bindings;
51: }
52:
53: /*---------*
54: * Methods *
55: *---------*/
56:
57: public boolean hasNext() throws QueryEvaluationException {
58: return currentBindings != null
59: && nextProjectionIdx < projections.size()
60: || iter.hasNext();
61: }
62:
63: public BindingSet next() throws QueryEvaluationException {
64: if (currentBindings == null
65: || nextProjectionIdx >= projections.size()) {
66: currentBindings = iter.next();
67: nextProjectionIdx = 0;
68: }
69:
70: ProjectionElemList nextProjection = projections
71: .get(nextProjectionIdx++);
72: return ProjectionIterator.project(nextProjection,
73: currentBindings, parentBindings);
74: }
75:
76: public void remove() {
77: throw new UnsupportedOperationException();
78: }
79:
80: @Override
81: protected void handleClose() throws QueryEvaluationException {
82: nextProjectionIdx = projections.size();
83: currentBindings = null;
84: super.handleClose();
85: }
86: }
|