lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael McCandless <luc...@mikemccandless.com>
Subject Re: Should Document.getFieldables really return null
Date Fri, 14 Mar 2008 10:46:42 GMT

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()]);
>    }
> ------------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-dev-help@lucene.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


Mime
View raw message