3 messages in org.apache.lucene.java-devRe: Should Document.getFieldables rea...
FromSent OnAttachments
Stefan TrcekMar 13, 2008 10:00 am 
Michael McCandlessMar 14, 2008 3:46 am 
Stefan TrcekMar 14, 2008 8:50 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: Should Document.getFieldables really return nullActions...
From:Michael McCandless (luc@mikemccandless.com)
Date:Mar 14, 2008 3:46:19 am
List:org.apache.lucene.java-dev

I agree, this makes sense. I'll commit it. Thanks Stefan!

Except, the last one you list (getBinaryValue) I think should still return null if no field by that name exists?

Mike

Stefan Trcek wrote:

Hello

The 'Document.getFieldables(String name)' is documented to return 'null' in some cases (and really does, see the code below). However this makes a penalty to the client, as code like this

Document doc = hits.doc(i); for (Fieldable f: doc.getFieldables("somefield")) { System.out.println(f.stringValue()); }

is wrong (no check on 'null'). For the client code it would be better, if 'Document.getFieldables(String)' would return 'new Fieldable[0]' instead (no NullPointerException).

If you needn't distinguish between null-ed arrays and arrays of zero lenght (do you?), I suggest to never return 'null', but return an array of size zero. If you don't trust the just-in-time compiler (concerning performance), you may even define

private final static Fieldable[] EMPTY = new Fieldable[0];

and return 'EMPTY' at the (*) line. Same with

public final Field[] getFields(String name) { public final String[] getValues(String name) { public final byte[][] getBinaryValues(String name) { public final byte[] getBinaryValue(String name) {

and maybe others.

Stefan

--- org.apache.lucene.document.Document.java --------------------- public Fieldable[] getFieldables(String name) { List result = new ArrayList(); for (int i = 0; i < fields.size(); i++) { Fieldable field = (Fieldable)fields.get(i); if (field.name().equals(name)) { result.add(field); } }

if (result.size() == 0) (*) return null;

return (Fieldable[])result.toArray(new Fieldable[result.size()]); } ------------------------