Index: java/org/apache/lucene/search/Hits.java =================================================================== RCS file: /home/cvspublic/jakarta-lucene/src/java/org/apache/lucene/search/Hits.java,v retrieving revision 1.8 diff -u -r1.8 Hits.java --- java/org/apache/lucene/search/Hits.java 13 Sep 2003 23:40:29 -0000 1.8 +++ java/org/apache/lucene/search/Hits.java 7 Oct 2003 17:28:38 -0000 @@ -55,12 +55,13 @@ */ import java.io.IOException; +import java.util.AbstractList; import java.util.Vector; import org.apache.lucene.document.Document; /** A ranked list of documents, used to hold search results. */ -public final class Hits { +public final class Hits extends AbstractList { private Query query; private Searcher searcher; private Filter filter = null; @@ -189,6 +190,28 @@ numDocs--; } + + /* (non-Javadoc) + * @see java.util.List#get(int) + */ + public final Object get(int index) { + Document doc; + try { + doc = doc(index); + } catch (IOException e) { + throw new RuntimeException(e); + // TODO can we do something better here or is this the right thing to do? + } + return doc; + } + + /* (non-Javadoc) + * @see java.util.Collection#size() + */ + public final int size() { + return length(); + } + } final class HitDoc { Index: src/test/org/apache/lucene/search/TestHits.java =================================================================== RCS file: src/test/org/apache/lucene/search/TestHits.java diff -N src/test/org/apache/lucene/search/TestHits.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/test/org/apache/lucene/search/TestHits.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,133 @@ +package org.apache.lucene.search; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache Lucene" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache Lucene", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.util.List; + +import junit.framework.TestCase; +import org.apache.lucene.analysis.WhitespaceAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.Term; +import org.apache.lucene.store.RAMDirectory; + +/** + * Tests {@link Hits}. + * + * @author Eric Isakson + */ +public class TestHits extends TestCase { + + private static int LIST_SIZE = 10; // must be > 1 + private static int SUBLIST_FROM_INDEX = 3; // should be > 0 && < LIST_SIZE - 1 + private static int SUBLIST_TO_INDEX = 7; // must be > SUBLIST_FROM_INDEX && <= LIST_SIZE + + private IndexSearcher searcher; + private Hits listAll; + private Hits listOne; + private Hits listZero; + + public void setUp() throws Exception { + RAMDirectory directory = new RAMDirectory(); + IndexWriter writer = + new IndexWriter(directory, new WhitespaceAnalyzer(), true); + for (int i = 0; i < LIST_SIZE; i++) { + Document doc = new Document(); + doc.add(Field.Keyword("field", "all")); + doc.add(Field.Keyword("index", Integer.toString(i))); + writer.addDocument(doc); + } + writer.close(); + + searcher = new IndexSearcher(directory); + + // setup the all list + Term term = new Term("field", "all"); + TermQuery query = new TermQuery(term); + listAll = searcher.search(query); + + // setup the one list to contain the document with index field 1 + term = new Term("index", Integer.toString(1)); + query = new TermQuery(term); + listOne = searcher.search(query); + + // setup the zero list + term = new Term("field", "none"); + query = new TermQuery(term); + listZero = searcher.search(query); + } + + public void tearDown() throws Exception { + searcher.close(); + } + + public void testSize() throws Exception { + assertEquals(LIST_SIZE, listAll.size()); + assertEquals(1, listOne.size()); + assertEquals(0, listZero.size()); + } + + public void testSubList() throws Exception { + List sublist = listAll.subList(SUBLIST_FROM_INDEX,SUBLIST_TO_INDEX); + int size = SUBLIST_TO_INDEX - SUBLIST_FROM_INDEX; + assertEquals(size, sublist.size()); + } + + public void testGet() throws Exception { + Document docOne = (Document)listOne.get(0); + assertEquals(Integer.toString(1),docOne.get("index")); + } +}