01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
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.CloseableIterationBase;
10:
11: import java.util.ArrayList;
12: import java.util.Collections;
13: import java.util.Comparator;
14: import java.util.Iterator;
15: import java.util.List;
16:
17: import org.openrdf.query.BindingSet;
18: import org.openrdf.query.QueryEvaluationException;
19:
20: /**
21: *
22: * @author james
23: *
24: */
25: public class OrderIterator extends
26: CloseableIterationBase<BindingSet, QueryEvaluationException> {
27:
28: /*-----------*
29: * Variables *
30: *-----------*/
31:
32: private CloseableIteration<BindingSet, QueryEvaluationException> iter;
33:
34: private Comparator<BindingSet> comparator;
35:
36: private Iterator<BindingSet> ordered;
37:
38: /*--------------*
39: * Constructors *
40: *--------------*/
41:
42: public OrderIterator(
43: CloseableIteration<BindingSet, QueryEvaluationException> iter,
44: Comparator<BindingSet> comparator) {
45: this .iter = iter;
46: this .comparator = comparator;
47: }
48:
49: /*---------*
50: * Methods *
51: *---------*/
52:
53: private Iterator<BindingSet> getOrderedIterator()
54: throws QueryEvaluationException {
55: if (ordered == null) {
56: List<BindingSet> list = new ArrayList<BindingSet>(1024);
57: while (iter.hasNext()) {
58: list.add(iter.next());
59: }
60: Collections.sort(list, comparator);
61: ordered = list.iterator();
62: }
63: return ordered;
64: }
65:
66: public boolean hasNext() throws QueryEvaluationException {
67: return getOrderedIterator().hasNext();
68: }
69:
70: public BindingSet next() throws QueryEvaluationException {
71: return getOrderedIterator().next();
72: }
73:
74: public void remove() throws QueryEvaluationException {
75: throw new UnsupportedOperationException();
76: }
77:
78: }
|