You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Yonik Seeley <ys...@yahoo.com> on 2004/11/24 22:40:40 UTC

new Document class using collection framework

The current document class is not that friendly if you
are trying to do things efficiently...
 - uses Vector, which is synchronized
 - can't use java5 "for (Field f : doc)"
 - can't get size()
 - can't get all fields into a Field[]
 - can't use with any generic code that works on
Collections or Lists

Proposal: Document should implement the List or
Collection interface

Example: here is a quick example that inherits from
ArrayList to quickly implement the needed List
functionality.  It should be completely backward
compatible.  If there is interest, I can flesh it out
a bit more...

-Yonik


import org.apache.lucene.document.Field;
import java.util.*;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public final class Document extends ArrayList
implements List {
    private float boost = 1.0f;

    /** Constructs a new document with no fields. */
    public Document() {}


    // non List methods
    public void setBoost(float boost) {
      this.boost = boost;
    }

    public float getBoost() {
      return boost;
    }

    //
    // all methods after this are provided for
    // backward compatability with the old Document
    //
    public final void removeField(String name) {
      Iterator it = iterator();
      while (it.hasNext()) {
        Field field = (Field)it.next();
        if (field.name().equals(name)) {
          it.remove();
          return;
        }
      }
    }

    public final void removeFields(String name) {
      Iterator it = iterator();
      while (it.hasNext()) {
        Field field = (Field)it.next();
        if (field.name().equals(name)) {
          it.remove();
        }
      }
    }

    public final Field getField(String name) {
      for (int i = 0; i < size(); i++) {
        Field field = (Field)get(i);
        if (field.name().equals(name))
    return field;
      }
      return null;
    }


    public final String get(String name) {
      Field field = getField(name);
      if (field != null)
        return field.stringValue();
      else
        return null;
    }

    public final Enumeration fields() {
      return Collections.enumeration(this);
    }

     public final Field[] getFields(String name) {
       List result = new ArrayList();
       for (int i = 0; i < size(); i++) {
         Field field = (Field)get(i);
         if (field.name().equals(name)) {
           result.add(field);
         }
       }

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

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

    public final String[] getValues(String name) {
      Field[] namedFields = getFields(name);
      if (namedFields == null)
        return null;
      String[] values = new
String[namedFields.length];
      for (int i = 0; i < namedFields.length; i++) {
        values[i] = namedFields[i].stringValue();
      }
      return values;
    }

    public final String toString() {
      StringBuffer buffer = new StringBuffer();
      buffer.append("Document<");
      for (int i = 0; i < size(); i++) {
        Field field = (Field)get(i);
        buffer.append(field.toString());
        if (i != size()-1)
          buffer.append(" ");
      }
      buffer.append(">");
      return buffer.toString();
    }

}



		
__________________________________ 
Do you Yahoo!? 
The all-new My Yahoo! - Get yours free! 
http://my.yahoo.com 
 


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