Index: src/java/org/apache/lucene/search/HitList.java
===================================================================
RCS file: src/java/org/apache/lucene/search/HitList.java
diff -N src/java/org/apache/lucene/search/HitList.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/java/org/apache/lucene/search/HitList.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,94 @@
+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.io.IOException;
+import java.util.AbstractList;
+
+import org.apache.lucene.document.Document;
+
+/**
+ * A simple List implementation wrapping a Hits object.
+ */
+public class HitList extends AbstractList {
+
+ private Hits hits;
+
+ public HitList(Hits hits) {
+ this.hits = hits;
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.List#get(int)
+ */
+ public Object get(int index) {
+ Document doc;
+ try {
+ doc = hits.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 int size() {
+ return hits.length();
+ }
+
+}
Index: src/test/org/apache/lucene/search/TestHitList.java
===================================================================
RCS file: src/test/org/apache/lucene/search/TestHitList.java
diff -N src/test/org/apache/lucene/search/TestHitList.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/test/org/apache/lucene/search/TestHitList.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,136 @@
+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 HitList}.
+ *
+ * @author Eric Isakson
+ */
+public class TestHitList 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 HitList listAll;
+ private HitList listOne;
+ private HitList 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);
+ Hits hits = searcher.search(query);
+ listAll = new HitList(hits);
+
+ // setup the one list to contain the document with index field 1
+ term = new Term("index", Integer.toString(1));
+ query = new TermQuery(term);
+ hits = searcher.search(query);
+ listOne = new HitList(hits);
+
+ // setup the zero list
+ term = new Term("field", "none");
+ query = new TermQuery(term);
+ hits = searcher.search(query);
+ listZero = new HitList(hits);
+ }
+
+ 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"));
+ }
+}