1 package org.apache.lucene.index;
3 import org.apache.lucene.util.StringHelper;
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 A Term represents a word from text. This is the unit of search. It is
24 composed of two elements, the text of the word, as a string, and the name of
25 the field that the text occurred in, an interned string.
27 Note that terms may represent more than words from text fields, but also
28 things like dates, email addresses, urls, etc. */
30 public final class Term implements Comparable<Term>, java.io.Serializable {
34 /** Constructs a Term with the given field and text.
35 * <p>Note that a null field or null text value results in undefined
36 * behavior for most Lucene APIs that accept a Term parameter. */
37 public Term(String fld, String txt) {
38 field = StringHelper.intern(fld);
42 /** Constructs a Term with the given field and empty text.
43 * This serves two purposes: 1) reuse of a Term with the same field.
44 * 2) pattern for a query.
48 public Term(String fld) {
52 Term(String fld, String txt, boolean intern) {
53 field = intern ? StringHelper.intern(fld) : fld; // field names are interned
54 text = txt; // unless already known to be
57 /** Returns the field of this term, an interned string. The field indicates
58 the part of a document which this term came from. */
59 public final String field() { return field; }
61 /** Returns the text of this term. In the case of words, this is simply the
62 text of the word. In the case of dates and other types, this is an
63 encoding of the object as a string. */
64 public final String text() { return text; }
67 * Optimized construction of new Terms by reusing same field as this Term
68 * - avoids field.intern() overhead
69 * @param text The text of the new term (field is implicitly same as this Term instance)
72 public Term createTerm(String text)
74 return new Term(field,text,false);
78 public boolean equals(Object obj) {
83 if (getClass() != obj.getClass())
85 Term other = (Term) obj;
87 if (other.field != null)
89 } else if (field != other.field)
92 if (other.text != null)
94 } else if (!text.equals(other.text))
100 public int hashCode() {
101 final int prime = 31;
103 result = prime * result + ((field == null) ? 0 : field.hashCode());
104 result = prime * result + ((text == null) ? 0 : text.hashCode());
108 /** Compares two terms, returning a negative integer if this
109 term belongs before the argument, zero if this term is equal to the
110 argument, and a positive integer if this term belongs after the argument.
112 The ordering of terms is first by field, then by text.*/
113 public final int compareTo(Term other) {
114 if (field == other.field) // fields are interned
115 return text.compareTo(other.text);
117 return field.compareTo(other.field);
120 /** Resets the field and text of a Term. */
121 final void set(String fld, String txt) {
127 public final String toString() { return field + ":" + text; }
129 private void readObject(java.io.ObjectInputStream in)
130 throws java.io.IOException, ClassNotFoundException
132 in.defaultReadObject();
133 field = StringHelper.intern(field);