Skip to content

Method: getBindingNames()

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.util;
28:
29: import javax.annotation.concurrent.NotThreadSafe;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.LinkedHashSet;
33: import java.util.List;
34: import java.util.NoSuchElementException;
35: import java.util.Set;
36: import org.eclipse.rdf4j.common.iteration.Iteration;
37: import org.eclipse.rdf4j.common.iteration.Iterations;
38: import org.eclipse.rdf4j.query.BindingSet;
39: import org.eclipse.rdf4j.query.QueryEvaluationException;
40: import org.eclipse.rdf4j.query.TupleQueryResult;
41:
42: /***********************************************************************************************************************
43: *
44: * @author Fabrizio Giudici
45: *
46: **********************************************************************************************************************/
47: @NotThreadSafe
48: public class ImmutableTupleQueryResult implements TupleQueryResult
49: {
50: private final Set<String> bindingNames = new LinkedHashSet<>();
51:
52: private final List<BindingSet> bindingSets = new ArrayList<>();
53:
54: private int currentIndex = 0;
55:
56: public ImmutableTupleQueryResult (final TupleQueryResult tqr)
57: throws QueryEvaluationException
58: {
59: this(tqr.getBindingNames(), tqr);
60: }
61:
62: public ImmutableTupleQueryResult (final ImmutableTupleQueryResult tqr)
63: throws QueryEvaluationException
64: {
65: this.bindingNames.addAll(tqr.bindingNames);
66: this.bindingSets.addAll(tqr.bindingSets);
67: }
68:
69: private <E extends Exception> ImmutableTupleQueryResult (final Collection<String> bindingNames,
70: final Iteration<? extends BindingSet, E> iteration)
71: throws E
72: {
73: this.bindingNames.addAll(bindingNames);
74: Iterations.addAll(iteration, this.bindingSets); // this also closes iteration
75: }
76:
77: @Override
78: public List<String> getBindingNames()
79: {
80: return new ArrayList<>(bindingNames);
81: }
82:
83: @Override
84: public boolean hasNext()
85: {
86: return currentIndex < bindingSets.size();
87: }
88:
89: @Override
90: public BindingSet next()
91: {
92: if (hasNext())
93: {
94: final BindingSet result = bindingSets.get(currentIndex);
95: currentIndex++;
96: return result;
97: }
98:
99: throw new NoSuchElementException();
100: }
101:
102: @Override
103: public void close()
104: {
105: // no-op
106: }
107:
108: @Override
109: public void remove()
110: throws QueryEvaluationException
111: {
112: throw new UnsupportedOperationException("Immutable");
113: }
114: }