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.model.Statement;
8   import org.openrdf.model.URI;
9   import org.openrdf.model.impl.URIImpl;
10  import org.openrdf.query.QueryEvaluationException;
11  
12  import com.ontotext.ordi.tripleset.ORDIConst;
13  import com.ontotext.ordi.tripleset.TStatement;
14  
15  /**
16   * This class is used to filter implicit statements from the result set.
17   * 
18   * @author vassil
19   */
20  public class ExplicitStatementIteratorImpl implements
21          CloseableIteration<Statement, QueryEvaluationException> {
22  
23      private boolean isClosed;
24      private CloseableIteration<? extends Statement, QueryEvaluationException> iterator;
25      private Statement next;
26      private static URI EXPLICIT_URI;
27  
28      public ExplicitStatementIteratorImpl(
29              CloseableIteration<? extends Statement, QueryEvaluationException> iterator) {
30          if (iterator == null) {
31              throw new IllegalArgumentException();
32          }
33          if (EXPLICIT_URI == null) {
34              EXPLICIT_URI = new URIImpl(ORDIConst.EXPLICIT_STATEMENT);
35          }
36          this.iterator = iterator;
37      }
38  
39      public boolean hasNext() throws QueryEvaluationException {
40          if (isClosed)
41              throw new NoSuchElementException("Iterator is closed!");
42          if (next != null)
43              return true;
44          
45          while (iterator.hasNext()) {
46              next = iterator.next();
47  
48              // Filter works only for TStatement types
49              if (next instanceof TStatement == false)
50                  return true;
51  
52              // Filter any statement which is not EXPLICIT_URI
53              if (((TStatement) next).isMemberOf(EXPLICIT_URI))
54                  return true;
55          }
56          return false;
57      }
58  
59      public void remove() throws QueryEvaluationException {
60          if (isClosed)
61              throw new NoSuchElementException("Iterator is closed!");
62          iterator.remove();
63      }
64  
65      public void close() throws QueryEvaluationException {
66          isClosed = true;
67          iterator.close();
68      }
69  
70      @SuppressWarnings("unchecked")
71      public Statement next() throws QueryEvaluationException {
72          if (isClosed || hasNext() == false)
73              throw new NoSuchElementException();
74  
75          try {
76              Statement nextResult = next;
77              next = null;
78              return nextResult;
79          } catch (ClassCastException e) {
80              throw new QueryEvaluationException(e);
81          }
82      }
83  }