1 package org.apache.lucene.document; 2 3 18 19 import java.util.Enumeration ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.ArrayList ; 23 import java.util.Vector ; 24 import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.Searcher; import org.apache.lucene.search.Hits; 28 41 42 public final class Document implements java.io.Serializable { 43 List fields = new Vector (); 44 private float boost = 1.0f; 45 46 47 public Document() {} 48 49 50 59 public void setBoost(float boost) { 60 this.boost = boost; 61 } 62 63 74 public float getBoost() { 75 return boost; 76 } 77 78 88 public final void add(Field field) { 89 fields.add(field); 90 } 91 92 102 public final void removeField(String name) { 103 Iterator it = fields.iterator(); 104 while (it.hasNext()) { 105 Field field = (Field)it.next(); 106 if (field.name().equals(name)) { 107 it.remove(); 108 return; 109 } 110 } 111 } 112 113 122 public final void removeFields(String name) { 123 Iterator it = fields.iterator(); 124 while (it.hasNext()) { 125 Field field = (Field)it.next(); 126 if (field.name().equals(name)) { 127 it.remove(); 128 } 129 } 130 } 131 132 136 public final Field getField(String name) { 137 for (int i = 0; i < fields.size(); i++) { 138 Field field = (Field)fields.get(i); 139 if (field.name().equals(name)) 140 return field; 141 } 142 return null; 143 } 144 145 150 public final String get(String name) { 151 for (int i = 0; i < fields.size(); i++) { 152 Field field = (Field)fields.get(i); 153 if (field.name().equals(name) && (!field.isBinary())) 154 return field.stringValue(); 155 } 156 return null; 157 } 158 159 160 public final Enumeration fields() { 161 return ((Vector )fields).elements(); 162 } 163 164 171 public final Field[] getFields(String name) { 172 List result = new ArrayList (); 173 for (int i = 0; i < fields.size(); i++) { 174 Field field = (Field)fields.get(i); 175 if (field.name().equals(name)) { 176 result.add(field); 177 } 178 } 179 180 if (result.size() == 0) 181 return null; 182 183 return (Field[])result.toArray(new Field[result.size()]); 184 } 185 186 193 public final String [] getValues(String name) { 194 List result = new ArrayList (); 195 for (int i = 0; i < fields.size(); i++) { 196 Field field = (Field)fields.get(i); 197 if (field.name().equals(name) && (!field.isBinary())) 198 result.add(field.stringValue()); 199 } 200 201 if (result.size() == 0) 202 return null; 203 204 return (String [])result.toArray(new String [result.size()]); 205 } 206 207 215 public final byte[][] getBinaryValues(String name) { 216 List result = new ArrayList (); 217 for (int i = 0; i < fields.size(); i++) { 218 Field field = (Field)fields.get(i); 219 if (field.name().equals(name) && (field.isBinary())) 220 result.add(field.binaryValue()); 221 } 222 223 if (result.size() == 0) 224 return null; 225 226 return (byte[][])result.toArray(new byte[result.size()][]); 227 } 228 229 238 public final byte[] getBinaryValue(String name) { 239 for (int i=0; i < fields.size(); i++) { 240 Field field = (Field)fields.get(i); 241 if (field.name().equals(name) && (field.isBinary())) 242 return field.binaryValue(); 243 } 244 return null; 245 } 246 247 248 public final String toString() { 249 StringBuffer buffer = new StringBuffer (); 250 buffer.append("Document<"); 251 for (int i = 0; i < fields.size(); i++) { 252 Field field = (Field)fields.get(i); 253 buffer.append(field.toString()); 254 if (i != fields.size()-1) 255 buffer.append(" "); 256 } 257 buffer.append(">"); 258 return buffer.toString(); 259 } 260 } 261 | Popular Tags |