Index: src/test/org/apache/lucene/document/TestDocument.java =================================================================== --- src/test/org/apache/lucene/document/TestDocument.java (revision 962932) +++ src/test/org/apache/lucene/document/TestDocument.java (working copy) @@ -1,5 +1,8 @@ package org.apache.lucene.document; +import java.util.ArrayList; +import java.util.LinkedList; + import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; @@ -36,7 +39,43 @@ String binaryVal = "this text will be stored as a byte array in the index"; String binaryVal2 = "this text will be also stored as a byte array in the index"; - + + /** test the 3 constructor variants */ + public void testDocumentConstructors() { + Document d; + d = new Document(); + assertNotNull(d.fields); + + // unfortunately ArrayList doesn't allow us to validate initialCapacity + d = new Document(128); + assertNotNull(d.fields); + + ArrayList fields = new ArrayList(512); + d = new Document(fields); + assertSame(fields, d.fields); + + // any kind of list is ok, though perhaps rarely wise + LinkedList linked = new LinkedList(); + d = new Document(linked); + assertSame(linked, d.fields); + } + + /** test the constructor variants exceptions */ + public void testDocumentConstructorExceptions() { + try { + new Document(-1); + fail("negative capcity should throw an exception"); + } catch (IllegalArgumentException e) { + // expected exception + } + try { + new Document(null); + fail("null list should throw an exception"); + } catch (IllegalArgumentException e) { + // expected exception + } + } + public void testBinaryField() throws Exception { Index: src/java/org/apache/lucene/index/FieldsReader.java =================================================================== --- src/java/org/apache/lucene/index/FieldsReader.java (revision 962932) +++ src/java/org/apache/lucene/index/FieldsReader.java (working copy) @@ -218,8 +218,8 @@ long position = indexStream.readLong(); fieldsStream.seek(position); - Document doc = new Document(); - int numFields = fieldsStream.readVInt(); + final int numFields = fieldsStream.readVInt(); + final Document doc = new Document((fieldSelector == null && numFields > 10) ? numFields : 10); for (int i = 0; i < numFields; i++) { int fieldNumber = fieldsStream.readVInt(); FieldInfo fi = fieldInfos.fieldInfo(fieldNumber); Index: src/java/org/apache/lucene/document/Document.java =================================================================== --- src/java/org/apache/lucene/document/Document.java (revision 962932) +++ src/java/org/apache/lucene/document/Document.java (working copy) @@ -37,13 +37,29 @@ */ public final class Document implements java.io.Serializable { - List fields = new ArrayList(); + final List fields; private float boost = 1.0f; /** Constructs a new document with no fields. */ - public Document() {} + public Document() { + this(new ArrayList()); + } + /** Construct a Document with an initial capacity for fields + * @param initialCapacity initial number of fields in the internal List */ + public Document(final int initialCapacity) { + this(new ArrayList(initialCapacity)); + } + /** Construct a Document with a List of Fieldables + * @param fields a List of Fielables to use. NOT COPIED, so be careful */ + public Document(final List fields) { + this.fields = fields; + if (fields == null) { + throw new IllegalArgumentException("fields must be non-null"); + } + } + /** Sets a boost factor for hits on any field of this document. This value * will be multiplied into the score of all hits on this document. *