lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chuck Williams" <ch...@manawiz.com>
Subject RE: new Document class using collection framework
Date Wed, 24 Nov 2004 22:15:20 GMT
As another user of java5, I'd love to see this work with generics
properly.  However, I don't quite see how the below works.  Have you
tested it with for (Field f : doc)?  Since you don't extend
ArrayList<Field>, how would the jvm know that the values are Field's?
The code below does the explicit casting for its get's, etc., which
seems unfortunately necessary.  And you can't extend ArrayList<Field>
since this would require java5 to work.

Chuck

  > -----Original Message-----
  > From: Yonik Seeley [mailto:yseeley@yahoo.com]
  > Sent: Wednesday, November 24, 2004 1:41 PM
  > To: lucene-dev@jakarta.apache.org
  > Subject: 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


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


Mime
View raw message