ImmutableTupleQueryResult.java

  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. import javax.annotation.concurrent.NotThreadSafe;
  29. import java.util.ArrayList;
  30. import java.util.Collection;
  31. import java.util.LinkedHashSet;
  32. import java.util.List;
  33. import java.util.NoSuchElementException;
  34. import java.util.Set;
  35. import org.eclipse.rdf4j.common.iteration.Iteration;
  36. import org.eclipse.rdf4j.common.iteration.Iterations;
  37. import org.eclipse.rdf4j.query.BindingSet;
  38. import org.eclipse.rdf4j.query.QueryEvaluationException;
  39. import org.eclipse.rdf4j.query.TupleQueryResult;

  40. /***********************************************************************************************************************
  41.  *
  42.  * @author Fabrizio Giudici
  43.  *
  44.  **********************************************************************************************************************/
  45. @NotThreadSafe
  46. public class ImmutableTupleQueryResult implements TupleQueryResult
  47.   {
  48.     private final Set<String> bindingNames = new LinkedHashSet<>();

  49.     private final List<BindingSet> bindingSets = new ArrayList<>();

  50.     private int currentIndex = 0;

  51.     public ImmutableTupleQueryResult (final TupleQueryResult tqr)
  52.       throws QueryEvaluationException
  53.       {
  54.         this(tqr.getBindingNames(), tqr);
  55.       }

  56.     public ImmutableTupleQueryResult (final ImmutableTupleQueryResult tqr)
  57.       throws QueryEvaluationException
  58.       {
  59.         this.bindingNames.addAll(tqr.bindingNames);
  60.         this.bindingSets.addAll(tqr.bindingSets);
  61.       }

  62.     private <E extends Exception> ImmutableTupleQueryResult (final Collection<String> bindingNames,
  63.                                                              final Iteration<? extends BindingSet, E> iteration)
  64.       throws E
  65.       {
  66.         this.bindingNames.addAll(bindingNames);
  67.         Iterations.addAll(iteration, this.bindingSets); // this also closes iteration
  68.       }

  69.     @Override
  70.     public List<String> getBindingNames()
  71.       {
  72.         return new ArrayList<>(bindingNames);
  73.       }

  74.     @Override
  75.     public boolean hasNext()
  76.       {
  77.         return currentIndex < bindingSets.size();
  78.       }

  79.     @Override
  80.     public BindingSet next()
  81.       {
  82.         if (hasNext())
  83.           {
  84.             final BindingSet result = bindingSets.get(currentIndex);
  85.             currentIndex++;
  86.             return result;
  87.           }

  88.         throw new NoSuchElementException();
  89.       }

  90.     @Override
  91.     public void close()
  92.       {
  93.         // no-op
  94.       }

  95.     @Override
  96.     public void remove()
  97.       throws QueryEvaluationException
  98.       {
  99.         throw new UnsupportedOperationException("Immutable");
  100.       }
  101.   }