View Javadoc

1   package com.ontotext.ordi.iterator;
2   
3   import info.aduna.iteration.CloseableIteration;
4   
5   import java.util.NoSuchElementException;
6   
7   import org.openrdf.query.QueryEvaluationException;
8   
9   import com.ontotext.ordi.tripleset.TStatement;
10  
11  /**
12   * This class converts the ORDI's result iterator to Sesame's CloseableIteration<X,
13   * Exception>.
14   * 
15   * @author vassil
16   * 
17   * @param <X>
18   */
19  public class ConvertingIteratorImpl<X> implements
20          CloseableIteration<X, QueryEvaluationException> {
21  
22      private CloseableIterator<? extends TStatement> iterator;
23      private boolean isClosed = false;
24  
25      public ConvertingIteratorImpl(CloseableIterator<? extends TStatement> iterator) {
26          if (iterator == null) {
27              throw new IllegalArgumentException();
28          }
29          this.iterator = iterator;
30      }
31  
32      public boolean hasNext() {
33          if (isClosed)
34              throw new NoSuchElementException("Iterator is closed!");
35          return iterator.hasNext();
36      }
37  
38      public void remove() {
39          if (isClosed)
40              throw new NoSuchElementException("Iterator is closed!");
41          iterator.remove();
42      }
43  
44      public void close() {
45          isClosed = true;
46          iterator.close();
47      }
48  
49      @SuppressWarnings("unchecked")
50      public X next() throws QueryEvaluationException {
51          if (isClosed || hasNext() == false)
52              throw new NoSuchElementException();
53  
54          try {
55              return (X) iterator.next();
56          } catch (ClassCastException e) {
57              throw new QueryEvaluationException(e);
58          }
59      }
60  }