View Javadoc

1   package org.paneris.bibliomania.fti;
2   
3   class GroupTextSearchResults extends TextSearchResultsBase {
4     private TextSearchResults[] streams;
5     private int[] positions;
6     private TextSearchResults leadStream;
7     private int currentWordIndex;
8   
9     public GroupTextSearchResults(TextSearchResults[] streams) {
10      this.streams = streams;
11      positions = new int[streams.length];
12      for (int i = 0; i < positions.length; ++i)
13        positions[i] = i;
14  
15      // FIXME sort them, actually
16  
17      if (streams.length > 0)
18        leadStream = streams[0];
19    }
20  
21    /**
22     * FIXME won't work if doing goups of not just single words, but then
23     * positions can't be static either
24     */
25  
26    public int hitWordsCount() {
27      return streams.length;
28    }
29  
30    private void findHit() {
31      for (int w = 0; w < streams.length;) {
32        TextSearchResults stream = streams[w];
33        int wantedIndex = currentWordIndex + positions[w];
34        stream.skipToWordIndex(wantedIndex);
35        int streamIndex = stream.currentWordIndex();
36        if (streamIndex == -1) {
37          currentWordIndex = -1;
38          return;
39        }
40  
41        if (streamIndex == wantedIndex)
42          ++w;
43        else {
44          currentWordIndex = streamIndex - positions[w];
45          w = 0;
46        }
47      }
48    }
49  
50    public void init() {
51      for (int w = 0; w < streams.length; ++w)
52        streams[w].init();
53      currentWordIndex = 0;
54      findHit();
55    }
56  
57    public void skipToNextHit() {
58      if (currentWordIndex != -1) {
59        ++currentWordIndex;
60        findHit();
61      }
62    }
63  
64    public int currentOffset() {
65      return currentWordIndex == -1 ? -1 : leadStream.currentOffset();
66    }
67  
68    public int currentWordIndex() {
69      return currentWordIndex;
70    }
71  
72    public void close() {
73      for (int i = 0; i < streams.length; ++i)
74        streams[i].close();
75    }
76  }