KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > Term


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 /**
20   A Term represents a word from text. This is the unit of search. It is
21   composed of two elements, the text of the word, as a string, and the name of
22   the field that the text occured in, an interned string.
23
24   Note that terms may represent more than words from text fields, but also
25   things like dates, email addresses, urls, etc. */

26
27 public final class Term implements Comparable JavaDoc, java.io.Serializable JavaDoc {
28   String JavaDoc field;
29   String JavaDoc text;
30
31   /** Constructs a Term with the given field and text. */
32   public Term(String JavaDoc fld, String JavaDoc txt) {
33     this(fld, txt, true);
34   }
35   Term(String JavaDoc fld, String JavaDoc txt, boolean intern) {
36     field = intern ? fld.intern() : fld; // field names are interned
37
text = txt; // unless already known to be
38
}
39
40   /** Returns the field of this term, an interned string. The field indicates
41     the part of a document which this term came from. */

42   public final String JavaDoc field() { return field; }
43
44   /** Returns the text of this term. In the case of words, this is simply the
45     text of the word. In the case of dates and other types, this is an
46     encoding of the object as a string. */

47   public final String JavaDoc text() { return text; }
48   
49   /**
50    * Optimized construction of new Terms by reusing same field as this Term
51    * - avoids field.intern() overhead
52    * @param text The text of the new term (field is implicitly same as this Term instance)
53    * @return A new Term
54    */

55   public Term createTerm(String JavaDoc text)
56   {
57       return new Term(field,text,false);
58   }
59
60   /** Compares two terms, returning true iff they have the same
61       field and text. */

62   public final boolean equals(Object JavaDoc o) {
63     if (o == null)
64       return false;
65     Term other = (Term)o;
66     return field == other.field && text.equals(other.text);
67   }
68
69   /** Combines the hashCode() of the field and the text. */
70   public final int hashCode() {
71     return field.hashCode() + text.hashCode();
72   }
73
74   public int compareTo(Object JavaDoc other) {
75     return compareTo((Term)other);
76   }
77
78   /** Compares two terms, returning a negative integer if this
79     term belongs before the argument, zero if this term is equal to the
80     argument, and a positive integer if this term belongs after the argument.
81
82     The ordering of terms is first by field, then by text.*/

83   public final int compareTo(Term other) {
84     if (field == other.field) // fields are interned
85
return text.compareTo(other.text);
86     else
87       return field.compareTo(other.field);
88   }
89
90   /** Resets the field and text of a Term. */
91   final void set(String JavaDoc fld, String JavaDoc txt) {
92     field = fld;
93     text = txt;
94   }
95
96   public final String JavaDoc toString() { return field + ":" + text; }
97
98   private void readObject(java.io.ObjectInputStream JavaDoc in)
99     throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
100   {
101       in.defaultReadObject();
102       field = field.intern();
103   }
104 }
105
Popular Tags