KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > search > LuceneCocoonPager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.search;
17
18 import org.apache.lucene.document.Document;
19 import org.apache.lucene.search.Hits;
20
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.ListIterator JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25
26 /**
27  * This class should help you to manage paging of hits.
28  *
29  * @author <a HREF="mailto:berni_huber@a1.net">Bernhard Huber</a>
30  * @version CVS $Id: LuceneCocoonPager.java 171276 2005-05-22 03:20:17Z antonio $
31  */

32 public class LuceneCocoonPager implements ListIterator JavaDoc
33 {
34     /**
35      * Default count of hits per page.
36      */

37     public final static int COUNT_OF_HITS_PER_PAGE_DEFAULT = 5;
38
39     /**
40      * Default starting index
41      */

42     public final static int HITS_INDEX_START_DEFAULT = 0;
43
44     /**
45      * Current index of hit to return by next()
46      */

47     int hitsIndex = HITS_INDEX_START_DEFAULT;
48
49     /**
50      * Maximum count of hits to return by next(), and previous()
51      */

52     int countOfHitsPerPage = COUNT_OF_HITS_PER_PAGE_DEFAULT;
53
54     /**
55      * Hits to iterate upon
56      */

57     private Hits hits;
58
59
60     /**
61      * @param hits Description of Parameter
62      */

63     public LuceneCocoonPager(Hits hits) {
64         setHits(hits);
65     }
66
67
68     /**
69      * Constructor for the LuceneCocoonPager object
70      */

71     public LuceneCocoonPager() {
72     }
73
74
75     /**
76      * Sets the hits attribute of the LuceneCocoonPager object
77      *
78      * @param hits The new hits value
79      */

80     public void setHits(Hits hits) {
81         this.hits = hits;
82         this.hitsIndex = HITS_INDEX_START_DEFAULT;
83     }
84
85
86     /**
87      * Set count of hits displayed per single page
88      *
89      * @param countOfHitsPerPage The new countOfHitsPerPage value
90      */

91     public void setCountOfHitsPerPage(int countOfHitsPerPage) {
92         this.countOfHitsPerPage = countOfHitsPerPage;
93         if (this.countOfHitsPerPage <= 0) {
94             this.countOfHitsPerPage = 1;
95         }
96     }
97
98
99     /**
100      * Get starting index for retrieving hits
101      *
102      * @param start_index The new startIndex value
103      */

104     public void setStartIndex(int start_index) {
105         this.hitsIndex = start_index;
106     }
107
108
109     /**
110      * Replaces the last element returned by next or previous with the
111      * specified element (optional operation).
112      *
113      * @param o Description of Parameter
114      */

115     public void set(Object JavaDoc o) {
116         throw new UnsupportedOperationException JavaDoc();
117     }
118
119
120     /**
121      * Get count of hits
122      *
123      * @return The count of hits
124      */

125     public int getCountOfHits() {
126         return hits.length();
127     }
128
129     /**
130      * Get count of hits displayed per single page
131      *
132      * @return The countOfHitsPerPage value
133      */

134     public int getCountOfHitsPerPage() {
135         return this.countOfHitsPerPage;
136     }
137
138     /**
139      * Caluclate count of pages for displaying all hits
140      *
141      * @return The countOfPages value
142      */

143     public int getCountOfPages() {
144         int count_of_pages = hits.length() / this.countOfHitsPerPage;
145         int remainder = hits.length() % this.countOfHitsPerPage;
146         if (remainder != 0) {
147             count_of_pages += 1;
148         }
149         return count_of_pages;
150     }
151
152
153     /**
154      * Set starting index for retrieving hits
155      *
156      * @return The startIndex value
157      */

158     public int getStartIndex() {
159         return this.hitsIndex;
160     }
161
162     /**
163      * Inserts the specified element into the list (optional operation).
164      *
165      * @param o Description of Parameter
166      * @exception UnsupportedOperationException Description of Exception
167      */

168     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
169         throw new UnsupportedOperationException JavaDoc();
170     }
171
172     /**
173      * Returns true if this list iterator has more elements when traversing
174      * the list in the forward direction.
175      *
176      * @return Description of the Returned Value
177      */

178     public boolean hasNext() {
179         return hitsIndex + countOfHitsPerPage < hits.length();
180     }
181
182     /**
183      * Returns true if this list iterator has more elements when traversing
184      * the list in the reverse direction.
185      *
186      * @return Description of the Returned Value
187      */

188     public boolean hasPrevious() {
189         return hitsIndex >= countOfHitsPerPage;
190     }
191
192     /**
193      * Returns the next element in the list.
194      *
195      * @return Description of the Returned Value
196      */

197     public Object JavaDoc next() {
198         ArrayList JavaDoc hitsPerPageList = new ArrayList JavaDoc();
199         int endIndex = Math.min(hits.length(), hitsIndex + countOfHitsPerPage);
200         //do not increment the actual hitsindex
201
int hits_copy = hitsIndex;
202         while (hits_copy < endIndex) {
203             try {
204                 HitWrapper hit = new HitWrapper(hits.score(hits_copy), hits.doc(hits_copy));
205                 hitsPerPageList.add(hit);
206             } catch (IOException JavaDoc ioe) {
207                 throw new NoSuchElementException JavaDoc("no more hits: " + ioe.getMessage());
208             }
209             hits_copy++;
210         }
211         return hitsPerPageList;
212     }
213
214     /**
215      * Returns the index of the element that would be returned by a
216      * subsequent call to next.
217      *
218      * @return Description of the Returned Value
219      */

220     public int nextIndex() {
221         return Math.min(hitsIndex + countOfHitsPerPage, hits.length() - 1);
222     }
223
224     /**
225      * Returns the previous element in the list.
226      *
227      * @return Description of the Returned Value
228      */

229     public Object JavaDoc previous() {
230         ArrayList JavaDoc hitsPerPageList = new ArrayList JavaDoc();
231
232         int startIndex = Math.max(0, hitsIndex - countOfHitsPerPage);
233         int endIndex = Math.min(hits.length() - 1, hitsIndex - countOfHitsPerPage);
234
235         if (startIndex < endIndex) {
236             while (startIndex < endIndex) {
237                 try {
238                     HitWrapper hit = new HitWrapper(hits.score(startIndex),
239                                                     hits.doc(startIndex));
240                     hitsPerPageList.add(hit);
241                 } catch (IOException JavaDoc ioe) {
242                     throw new NoSuchElementException JavaDoc("no more hits: " + ioe.getMessage());
243                 }
244                 startIndex++;
245             }
246             hitsIndex = endIndex;
247         } else {
248             throw new NoSuchElementException JavaDoc();
249         }
250         return hitsPerPageList;
251     }
252
253     /**
254      * Returns the index of the element that would be returned by a
255      * subsequent call to previous.
256      *
257      * @return Description of the Returned Value
258      */

259     public int previousIndex() {
260         return Math.max(0, hitsIndex - countOfHitsPerPage);
261     }
262
263     /**
264      * Removes from the list the last element that was returned by next or
265      * previous (optional operation).
266      */

267     public void remove() {
268         throw new UnsupportedOperationException JavaDoc();
269     }
270
271     /**
272      * A helper class encapsulating found document, and its score
273      *
274      * @author <a HREF="mailto:berni_huber@a1.net">Bernhard Huber</a>
275      * @version CVS $Id: LuceneCocoonPager.java 171276 2005-05-22 03:20:17Z antonio $
276      */

277     public static class HitWrapper {
278         float score;
279         Document document;
280
281         /**
282          * Constructor for the HitWrapper object
283          *
284          * @param score Description of Parameter
285          * @param document Description of Parameter
286          */

287         public HitWrapper(float score, Document document) {
288             this.document = document;
289             this.score = score;
290         }
291
292         /**
293          * Gets the document attribute of the HitWrapper object
294          *
295          * @return The document value
296          */

297         public Document getDocument() {
298             return document;
299         }
300
301         /**
302          * Gets the score attribute of the HitWrapper object
303          *
304          * @return The score value
305          */

306         public float getScore() {
307             return score;
308         }
309
310         /**
311          * Gets the field attribute of the HitWrapper object
312          *
313          * @param field Description of Parameter
314          * @return The field value
315          */

316         public String JavaDoc getField(String JavaDoc field) {
317             return document.get(field);
318         }
319     }
320 }
321
Popular Tags