View Javadoc

1   package org.paneris.bibliomania.fti;
2   
3   import java.util.Enumeration;
4   import java.util.Vector;
5   
6   import org.melati.util.PagedEnumeration;
7   
8   import com.sleepycat.db.DatabaseException;
9   
10  public class ScoredHitTextsEnumeration implements PagedEnumeration {
11    private ContextSearchResults results;
12  
13    private int pageStart, pageSize, requestedPageSize;
14  
15    // private HitText hitText = null;
16    private boolean totalCountIsMinimum;
17  
18    private Enumeration us;
19  
20    public ScoredHitTextsEnumeration(ContextSearchResults resultsP,
21        Score.Hit[] scores, int pageStart, int requestedPageSize, int hitsPerText)
22        throws FTIException {
23      try {
24        this.results = resultsP;
25        this.pageStart = pageStart = Math.max(pageStart, 1);
26        this.requestedPageSize = requestedPageSize;
27  
28        int h = 0;
29        int areaCount = 0, areasInThisText = 0;
30        while (areaCount + 1 < pageStart) {
31          if (results.nextArea() == null || areasInThisText >= hitsPerText) {
32            if (h >= scores.length)
33              break;
34            results.gotoText(scores[h++].textID);
35            areasInThisText = 0;
36          } else {
37            ++areaCount;
38            ++areasInThisText;
39          }
40        }
41  
42        Vector texts = new Vector();
43        int a;
44        for (a = 0; a < requestedPageSize && h < scores.length; ++h) {
45          results.gotoText(scores[h].textID);
46  
47          // Just make sure the text exists---we let old indexings hang around
48          // rather than expend effort deleting them
49  
50          if (results.currentText() != null) {
51            HitText text = new HitText(scores[h].score, results, Math.min(
52                requestedPageSize - a, hitsPerText - areasInThisText));
53            areasInThisText = 0;
54            texts.addElement(text);
55            a += text.areas;
56          }
57        }
58  
59        totalCountIsMinimum = a == requestedPageSize;
60        pageSize = a;
61  
62        us = texts.elements();
63      } catch (DatabaseException e) {
64        throw new FTIException(e);
65      }
66    }
67  
68    // 
69    // -------------
70    // Enumeration
71    // -------------
72    // 
73  
74    public boolean hasMoreElements() {
75      return us.hasMoreElements();
76    }
77  
78    public Object nextElement() {
79      return us.nextElement();
80    }
81  
82    // 
83    // -----------------
84    // PageEnumeration
85    // -----------------
86    // 
87  
88    public int getPageStart() {
89      return pageStart;
90    }
91  
92    public int getPageSize() {
93      return pageSize;
94    }
95  
96    public int getPageEnd() {
97      return pageStart + pageSize - 1;
98    }
99  
100   public int getTotalCount() {
101     return getPageEnd();
102   }
103 
104   public boolean getTotalCountIsMinimum() {
105     return totalCountIsMinimum;
106   }
107 
108   public Integer getPrevPageStart() {
109     int it = pageStart - requestedPageSize;
110     return it < 0 ? null : new Integer(it);
111   }
112 
113   public Integer getNextPageStart() {
114     return totalCountIsMinimum ? new Integer(pageStart + requestedPageSize)
115         : null;
116   }
117 
118   public int getCurrentPosition() {
119     throw new RuntimeException("No one else has ever called this method.");
120   }
121 
122   public int getNextPosition() {
123     throw new RuntimeException("No one else has ever called this method.");
124   }
125 
126   public Vector getPages() {
127     throw new RuntimeException("No one else has ever called this method.");
128   }
129 
130   public boolean nextElementOnThisPage() {
131     throw new RuntimeException("No one else has ever called this method.");
132   }
133 }