View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   
3   package org.galagosearch.tupleflow;
4   
5   import java.io.EOFException;
6   import java.io.IOException;
7   
8   public abstract class OrderedReader<T> implements TypeReader<T>, ExNihiloSource<T> {
9       protected Processor<T> processor;
10      protected ArrayInput input;
11      protected boolean needsInit;
12      protected T last = null;
13      protected int batchCount = 0;
14      
15      public OrderedReader(ArrayInput input, Processor<T> processor) {
16          this.input = input;
17          this.processor = processor;
18          needsInit = true;
19          last = clone(null);
20      }
21      
22      public OrderedReader(ArrayInput input) {
23          this(input, null);
24      }
25      
26      public void setProcessor(Processor<T> processor) {
27          this.processor = processor;
28      }
29  
30      public void run() throws IOException {
31          T object = null;
32          while ((object = read()) != null) {
33              processor.process(object);
34          }
35          processor.close();
36      }
37      
38      public T read() throws IOException {
39          try {
40              readTuple();
41          } catch(EOFException e) {
42              return null;
43          }
44          
45          return clone(last);
46      }
47      
48      public void readTuple() throws IOException {}
49      public abstract T clone(T object);
50  }