Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 
31  [Serializable]
32  public sealed class Term : System.IComparable<Term>
33  {
34  internal System.String field;
35  internal System.String text;
36 
41  public Term(System.String fld, System.String txt)
42  {
43  field = StringHelper.Intern(fld);
44  text = txt;
45  }
46 
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 
67  public string Field
68  {
69  get { return field; }
70  }
71 
76  public string Text
77  {
78  get { return text; }
79  }
80 
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 
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 
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 }