001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.impl;
007:
008: import java.util.Collections;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: import info.aduna.iteration.CloseableIteration;
013: import info.aduna.iteration.CloseableIteratorIteration;
014: import info.aduna.iteration.Iteration;
015: import info.aduna.iteration.Iterations;
016:
017: import org.openrdf.query.BindingSet;
018: import org.openrdf.query.QueryEvaluationException;
019: import org.openrdf.query.TupleQueryResult;
020:
021: /**
022: * A generic implementation of the {@link TupleQueryResult} interface.
023: */
024: public class TupleQueryResultImpl implements TupleQueryResult {
025:
026: /*-----------*
027: * Variables *
028: *-----------*/
029:
030: private List<String> bindingNames;
031:
032: private Iteration<? extends BindingSet, QueryEvaluationException> bindingSetIter;
033:
034: /*--------------*
035: * Constructors *
036: *--------------*/
037:
038: /**
039: * Creates a query result object with the supplied binding names.
040: * <em>The supplied list of binding names is assumed to be constant</em>;
041: * care should be taken that the contents of this list doesn't change after
042: * supplying it to this solution.
043: *
044: * @param bindingNames
045: * The binding names, in order of projection.
046: */
047: public TupleQueryResultImpl(List<String> bindingNames,
048: Iterable<? extends BindingSet> bindingSets) {
049: this (bindingNames, bindingSets.iterator());
050: }
051:
052: public TupleQueryResultImpl(List<String> bindingNames,
053: Iterator<? extends BindingSet> bindingSetIter) {
054: this (
055: bindingNames,
056: new CloseableIteratorIteration<BindingSet, QueryEvaluationException>(
057: bindingSetIter));
058: }
059:
060: /**
061: * Creates a query result object with the supplied binding names.
062: * <em>The supplied list of binding names is assumed to be constant</em>;
063: * care should be taken that the contents of this list doesn't change after
064: * supplying it to this solution.
065: *
066: * @param bindingNames
067: * The binding names, in order of projection.
068: */
069: public TupleQueryResultImpl(
070: List<String> bindingNames,
071: CloseableIteration<? extends BindingSet, QueryEvaluationException> bindingSetIter) {
072: // Don't allow modifications to the binding names when it is accessed
073: // through getBindingNames:
074: this .bindingNames = Collections.unmodifiableList(bindingNames);
075: this .bindingSetIter = bindingSetIter;
076: }
077:
078: /*---------*
079: * Methods *
080: *---------*/
081:
082: public List<String> getBindingNames() {
083: return bindingNames;
084: }
085:
086: public void close() throws QueryEvaluationException {
087: Iterations.closeCloseable(bindingSetIter);
088: }
089:
090: public boolean hasNext() throws QueryEvaluationException {
091: return bindingSetIter.hasNext();
092: }
093:
094: public BindingSet next() throws QueryEvaluationException {
095: return bindingSetIter.next();
096: }
097:
098: public void remove() throws QueryEvaluationException {
099: bindingSetIter.remove();
100: }
101: }
|