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 HitTextsEnumeration implements PagedEnumeration {
11    private ContextSearchResults results;
12    private int pageStart, pageSize, requestedPageSize;
13    private boolean totalCountIsMinimum;
14    private Enumeration us;
15  
16    public HitTextsEnumeration(
17      ContextSearchResults resultsP,
18      long startTextID,
19      long limitTextID,
20      int pageStart,
21      int requestedPageSize,
22      int hitsPerText)
23      throws FTIException {
24      try {
25        this.results = resultsP;
26        this.pageStart = pageStart = Math.max(pageStart, 1);
27        this.requestedPageSize = requestedPageSize;
28  
29        results.gotoText(startTextID);
30  
31        int areaCount = 0, areasInThisText = 0;
32        while (areaCount + 1 < pageStart
33          && results.currentTextID() != -1
34          && results.currentTextID() < limitTextID) {
35          if (results.nextArea() == null || areasInThisText >= hitsPerText) {
36            if (results.currentTextID() != -1)
37              results.gotoText(results.currentTextID() + 1);
38            areasInThisText = 0;
39          } else {
40            ++areaCount;
41            ++areasInThisText;
42          }
43        }
44  
45        Vector texts = new Vector();
46        int a;
47        for (a = 0;
48          a < requestedPageSize
49            && results.currentTextID() != -1
50            && results.currentTextID() < limitTextID;
51          ) {
52  
53          // Just make sure the text exists---we let old indexings hang around
54          // rather than expend effort deleting them
55  
56          if (results.currentText() != null) {
57            HitText text =
58              new HitText(
59                results,
60                Math.min(requestedPageSize - a, hitsPerText - areasInThisText));
61            areasInThisText = 0;
62            texts.addElement(text);
63            a += text.areas;
64            if (results.currentTextID() != -1)
65              results.gotoText(results.currentTextID() + 1);
66          }
67        }
68  
69        totalCountIsMinimum = a == requestedPageSize;
70        pageSize = a;
71  
72        us = texts.elements();
73      } catch (DatabaseException e) {
74        throw new FTIException(e);
75      }
76    }
77  
78    // 
79    // -------------
80    //  Enumeration
81    // -------------
82    // 
83  
84    public boolean hasMoreElements() {
85      return us.hasMoreElements();
86    }
87  
88    public Object nextElement() {
89      return us.nextElement();
90    }
91  
92    // 
93    // -----------------
94    //  PageEnumeration
95    // -----------------
96    // 
97  
98    public int getPageStart() {
99      return pageStart;
100   }
101 
102   public int getPageSize() {
103     return pageSize;
104   }
105 
106   public int getPageEnd() {
107     return pageStart + pageSize - 1;
108   }
109 
110   public int getTotalCount() {
111     return getPageEnd();
112   }
113 
114   public boolean getTotalCountIsMinimum() {
115     return totalCountIsMinimum;
116   }
117 
118   public Integer getPrevPageStart() {
119     int it = pageStart - requestedPageSize;
120     return it < 0 ? null : new Integer(it);
121   }
122 
123   public Integer getNextPageStart() {
124     return totalCountIsMinimum
125       ? new Integer(pageStart + requestedPageSize)
126       : null;
127   }
128 
129   public int getCurrentPosition() {
130     throw new RuntimeException("TODO No one else has ever called this method." +
131                                " Do you really want to start now?");
132     
133   }
134 
135   public int getNextPosition() {
136     throw new RuntimeException("TODO No one else has ever called this method." +
137                                " Do you really want to start now?");
138     
139   }
140 
141   public Vector getPages() {
142     throw new RuntimeException("TODO No one else has ever called this method." +
143                                " Do you really want to start now?");
144     
145   }
146 
147   public boolean nextElementOnThisPage() {
148     throw new RuntimeException("TODO No one else has ever called this method." +
149                                " Do you really want to start now?");
150     
151   }
152 }