View Javadoc

1   package org.paneris.bibliomania.fti;
2   
3   class OrTextSearchResults extends TextSearchResultsBase {
4     private TextSearchResults[] streams;
5     private TextSearchResults currentStream = null;
6     private int hitWordsCount;
7   
8     public OrTextSearchResults(TextSearchResults[] streams) {
9       this.streams = streams;
10      hitWordsCount = 0;
11      for (int i = 0; i < streams.length; ++i)
12        hitWordsCount = Math.max(hitWordsCount, streams[i].hitWordsCount());
13    }
14  
15    public int hitWordsCount() {
16      return hitWordsCount;
17    }
18  
19    public void init() {
20      // FIXME sort streams by local count, but into a copied array
21      for (int w = 0; w < streams.length; ++w)
22        streams[w].init();
23      findHit();
24    }
25  
26    private void findHit() {
27      int firstIndex = Integer.MAX_VALUE;
28      currentStream = null;
29      for (int w = 0; w < streams.length; ++w) {
30        TextSearchResults stream = streams[w];
31        int streamIndex = stream.currentWordIndex();
32        if (streamIndex != -1 && streamIndex < firstIndex) {
33          firstIndex = streamIndex;
34          currentStream = stream;
35        }
36      }
37    }
38  
39    public void skipToNextHit() {
40      if (currentStream != null) {
41        currentStream.skipToNextHit();
42        findHit();
43      }
44    }
45  
46    public int currentWordIndex() {
47      return currentStream == null ? -1 : currentStream.currentWordIndex();
48    }
49  
50    public int currentOffset() {
51      return currentStream == null ? -1 : currentStream.currentOffset();
52    }
53  
54    public void close() {
55      for (int i = 0; i < streams.length; ++i)
56        streams[i].close();
57    }
58  }