Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Term.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 using StringHelper = Lucene.Net.Util.StringHelper;
21 
22 namespace Lucene.Net.Index
23 {
24 
25  /// <summary>A Term represents a word from text. This is the unit of search. It is
26  /// composed of two elements, the text of the word, as a string, and the name of
27  /// the field that the text occured in, an interned string.
28  /// Note that terms may represent more than words from text fields, but also
29  /// things like dates, email addresses, urls, etc.
30  /// </summary>
31  [Serializable]
32  public sealed class Term : System.IComparable<Term>
33  {
34  internal System.String field;
35  internal System.String text;
36 
37  /// <summary>Constructs a Term with the given field and text.
38  /// <p/>Note that a null field or null text value results in undefined
39  /// behavior for most Lucene APIs that accept a Term parameter.
40  /// </summary>
41  public Term(System.String fld, System.String txt)
42  {
43  field = StringHelper.Intern(fld);
44  text = txt;
45  }
46 
47  /// <summary>Constructs a Term with the given field and empty text.
48  /// This serves two purposes: 1) reuse of a Term with the same field.
49  /// 2) pattern for a query.
50  ///
51  /// </summary>
52  /// <param name="fld">
53  /// </param>
54  public Term(System.String fld):this(fld, "", true)
55  {
56  }
57 
58  internal Term(System.String fld, System.String txt, bool intern)
59  {
60  field = intern?StringHelper.Intern(fld):fld; // field names are interned
61  text = txt; // unless already known to be
62  }
63 
64  /// <summary>Returns the field of this term, an interned string. The field indicates
65  /// the part of a document which this term came from.
66  /// </summary>
67  public string Field
68  {
69  get { return field; }
70  }
71 
72  /// <summary>Returns the text of this term. In the case of words, this is simply the
73  /// text of the word. In the case of dates and other types, this is an
74  /// encoding of the object as a string.
75  /// </summary>
76  public string Text
77  {
78  get { return text; }
79  }
80 
81  /// <summary> Optimized construction of new Terms by reusing same field as this Term
82  /// - avoids field.intern() overhead
83  /// </summary>
84  /// <param name="text">The text of the new term (field is implicitly same as this Term instance)
85  /// </param>
86  /// <returns> A new Term
87  /// </returns>
88  public Term CreateTerm(System.String text)
89  {
90  return new Term(field, text, false);
91  }
92 
93  //@Override
94  public override bool Equals(System.Object obj)
95  {
96  if (this == obj)
97  return true;
98  if (obj == null)
99  return false;
100  if (GetType() != obj.GetType())
101  return false;
102  Term other = (Term) obj;
103  if (field == null)
104  {
105  if (other.field != null)
106  return false;
107  }
108  else if (!field.Equals(other.field))
109  return false;
110  if (text == null)
111  {
112  if (other.text != null)
113  return false;
114  }
115  else if (!text.Equals(other.text))
116  return false;
117  return true;
118  }
119 
120  //@Override
121  public override int GetHashCode()
122  {
123  int prime = 31;
124  int result = 1;
125  result = prime*result + ((field == null) ? 0 : field.GetHashCode());
126  result = prime*result + ((text == null) ? 0 : text.GetHashCode());
127  return result;
128  }
129 
130  /// <summary>Compares two terms, returning a negative integer if this
131  /// term belongs before the argument, zero if this term is equal to the
132  /// argument, and a positive integer if this term belongs after the argument.
133  /// The ordering of terms is first by field, then by text.
134  /// </summary>
135  public int CompareTo(Term other)
136  {
137  if ((System.Object) field == (System.Object) other.field)
138  // fields are interned
139  return String.CompareOrdinal(text, other.text);
140  else
141  return String.CompareOrdinal(field, other.field);
142  }
143 
144  ///// <summary>Resets the field and text of a Term. </summary>
145  //internal void Set(System.String fld, System.String txt)
146  //{
147  // field = fld;
148  // text = txt;
149  //}
150 
151  public override System.String ToString()
152  {
153  return field + ":" + text;
154  }
155 
156 // private void ReadObject(System.IO.BinaryReader in_Renamed)
157 // {
158 // in_Renamed.defaultReadObject();
159 // field = StringHelper.Intern(field);
160 // }
161 
162  [System.Runtime.Serialization.OnDeserialized]
163  internal void OnDeserialized(System.Runtime.Serialization.StreamingContext context)
164  {
165  field = StringHelper.Intern(field);
166  }
167  }
168 }