Return-Path: Delivered-To: apmail-jakarta-lucene-dev-archive@apache.org Received: (qmail 53162 invoked from network); 23 May 2002 00:45:03 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 23 May 2002 00:45:03 -0000 Received: (qmail 27612 invoked by uid 97); 23 May 2002 00:45:10 -0000 Delivered-To: qmlist-jakarta-archive-lucene-dev@jakarta.apache.org Received: (qmail 27596 invoked by uid 97); 23 May 2002 00:45:09 -0000 Mailing-List: contact lucene-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Lucene Developers List" Reply-To: "Lucene Developers List" Delivered-To: mailing list lucene-dev@jakarta.apache.org Received: (qmail 27585 invoked by uid 97); 23 May 2002 00:45:09 -0000 X-Antivirus: nagoya (v4198 created Apr 24 2002) Date: 23 May 2002 00:44:57 -0000 Message-ID: <20020523004457.85689.qmail@icarus.apache.org> From: carlson@apache.org To: jakarta-lucene-sandbox-cvs@apache.org Subject: cvs commit: jakarta-lucene-sandbox/contributions/searchbean/src/java/org/apache/lucene/beans HitsIterator.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N carlson 02/05/22 17:44:57 Added: contributions/searchbean/src/java/org/apache/lucene/beans HitsIterator.java Log: Initial submit of SerachBean and related classes Revision Changes Path 1.1 jakarta-lucene-sandbox/contributions/searchbean/src/java/org/apache/lucene/beans/HitsIterator.java Index: HitsIterator.java =================================================================== /* * HitsIterator.java * Provides an Iterator class around Lucene Hits * It also supports paging * Created on November 1, 2001, 8:53 PM */ package org.apache.lucene.beans; import org.apache.lucene.beans.SortedField; import org.apache.lucene.beans.CompareDocumentsByField; import org.apache.lucene.document.Document; import org.apache.lucene.search.Hits; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; //import org.apache.log4j.Logger; /** * * @author Peter Carlson * @version 1.0 */ public class HitsIterator { //static Logger logger = Logger.getLogger(HitsIterator.class.getName()); private int currentPosition = 0; private Hits hitsCollection = null; private Object[] arrayOfIndividualHits = null; private int totalHits = 0; private int pageSize = 25; // default page size private int currentPage = 1; // range from 1 to totalHits%pageSize private int totalPages = -1; // set by constructor private int endPagePosition = 0; // position currentPage ends /** Creates new HitsIterator */ private HitsIterator() { } public HitsIterator(Hits hits) throws IOException{ this(hits,null); } public HitsIterator(Hits hits, String sortFlag) throws IOException{ this.hitsCollection = hits; if (sortFlag != null){ if ((sortFlag != "") && (sortFlag !="relevance")){ //logger.debug("Sorting hits by field "+sortFlag); sortByField(sortFlag); //logger.debug("Completed sorting by field "+sortFlag); } } totalHits = getTotalHits(); setPageCount(); } /** sorts hits by the given sort flag * fills an interal array * @param sortFlag field to sort results on */ private void sortByField(String fieldName) throws IOException{ long start = System.currentTimeMillis(); Comparator c = null; if (fieldName == null){ //logger.error("sort field is null"); return; } SortedField sf = SortedField.getSortedField(fieldName); if (sf !=null){ c = (Comparator) new CompareDocumentsByField(); } else { //logger.error("Sort field not found"); arrayOfIndividualHits = null; return; } arrayOfIndividualHits = new Object[hitsCollection.length()]; long first = System.currentTimeMillis(); for (int i=0; i totalHits) { return null; } currentPosition = position; return getDoc(); } public org.apache.lucene.document.Document next() throws IOException{ currentPosition++; if (currentPosition > totalHits) { currentPosition = totalHits; return null ; } return getDoc(); } public org.apache.lucene.document.Document previous() throws IOException{ currentPosition--; if (currentPosition < 0) { return null;} return getDoc(); } public boolean hasNext() { if (currentPosition < endPagePosition) { return true; } return false; } public org.apache.lucene.document.Document getDoc() throws IOException { if (arrayOfIndividualHits == null) return hitsCollection.doc(currentPosition - 1); else { int i = ((IndividualHit)arrayOfIndividualHits[currentPosition - 1]).getIndex(); return hitsCollection.doc(i); } } public int getScore() throws Exception{ if (arrayOfIndividualHits == null) return (int) (hitsCollection.score(currentPosition - 1)*100.0f); else return (int) (((IndividualHit)arrayOfIndividualHits[currentPosition - 1]).getScore()*100.0f); } public int getTotalHits() { return hitsCollection.length(); } public int getCurrentPosition() { return currentPosition; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; setPageCount(); } public void setCurrentPage(int currentPage) throws IndexOutOfBoundsException{ if (currentPage > totalPages){ throw new IndexOutOfBoundsException("currentPage greater than total pages"); } this.currentPage = currentPage; currentPosition = ((currentPage - 1) * pageSize); endPagePosition = Math.min( ((currentPage - 1)*pageSize) + pageSize, totalHits); } public int getCurrentPage() { return currentPage; } /** * set page number to next page, unless last page, then * always return last page number *@return current page number */ public int nextPage() { setCurrentPage(currentPage++); return getCurrentPage(); } /** * set page number to previous page, unless first page, * then always return first page number *@return current page number */ public int previousPage() { setCurrentPage(currentPage--); return getCurrentPage(); } } -- To unsubscribe, e-mail: For additional commands, e-mail: