Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Index / Term.cs
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 = Mono.Lucene.Net.Util.StringHelper;
21
22 namespace Mono.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         
32         [Serializable]
33     public sealed class Term : System.IComparable
34         {
35                 internal System.String field;
36                 internal System.String text;
37                 
38                 /// <summary>Constructs a Term with the given field and text.
39                 /// <p/>Note that a null field or null text value results in undefined
40                 /// behavior for most Lucene APIs that accept a Term parameter. 
41                 /// </summary>
42                 public Term(System.String fld, System.String txt)
43                 {
44                         field = StringHelper.Intern(fld);
45                         text = txt;
46                 }
47                 
48                 /// <summary>Constructs a Term with the given field and empty text.
49                 /// This serves two purposes: 1) reuse of a Term with the same field.
50                 /// 2) pattern for a query.
51                 /// 
52                 /// </summary>
53                 /// <param name="fld">
54                 /// </param>
55                 public Term(System.String fld):this(fld, "", true)
56                 {
57                 }
58                 
59                 internal Term(System.String fld, System.String txt, bool intern)
60                 {
61                         field = intern?StringHelper.Intern(fld):fld; // field names are interned
62                         text = txt; // unless already known to be
63                 }
64                 
65                 /// <summary>Returns the field of this term, an interned string.   The field indicates
66                 /// the part of a document which this term came from. 
67                 /// </summary>
68                 public System.String Field()
69                 {
70                         return field;
71                 }
72                 
73                 /// <summary>Returns the text of this term.  In the case of words, this is simply the
74                 /// text of the word.  In the case of dates and other types, this is an
75                 /// encoding of the object as a string.  
76                 /// </summary>
77                 public System.String Text()
78                 {
79                         return text;
80                 }
81                 
82                 /// <summary> Optimized construction of new Terms by reusing same field as this Term
83                 /// - avoids field.intern() overhead 
84                 /// </summary>
85                 /// <param name="text">The text of the new term (field is implicitly same as this Term instance)
86                 /// </param>
87                 /// <returns> A new Term
88                 /// </returns>
89                 public Term CreateTerm(System.String text)
90                 {
91                         return new Term(field, text, false);
92                 }
93                 
94                 //@Override
95                 public  override bool Equals(System.Object obj)
96                 {
97                         if (this == obj)
98                                 return true;
99                         if (obj == null)
100                                 return false;
101                         if (GetType() != obj.GetType())
102                                 return false;
103                         Term other = (Term) obj;
104                         if (field == null)
105                         {
106                                 if (other.field != null)
107                                         return false;
108                         }
109                         else if (!field.Equals(other.field))
110                                 return false;
111                         if (text == null)
112                         {
113                                 if (other.text != null)
114                                         return false;
115                         }
116                         else if (!text.Equals(other.text))
117                                 return false;
118                         return true;
119                 }
120                 
121                 //@Override
122                 public override int GetHashCode()
123                 {
124                         int prime = 31;
125                         int result = 1;
126                         result = prime * result + ((field == null)?0:field.GetHashCode());
127                         result = prime * result + ((text == null)?0:text.GetHashCode());
128                         return result;
129                 }
130                 
131                 public int CompareTo(System.Object other)
132                 {
133                         return CompareTo((Term) other);
134                 }
135                 
136                 /// <summary>Compares two terms, returning a negative integer if this
137                 /// term belongs before the argument, zero if this term is equal to the
138                 /// argument, and a positive integer if this term belongs after the argument.
139                 /// The ordering of terms is first by field, then by text.
140                 /// </summary>
141                 public int CompareTo(Term other)
142                 {
143                         if ((System.Object) field == (System.Object) other.field)
144                         // fields are interned
145                                 return String.CompareOrdinal(text, other.text);
146                         else
147                                 return String.CompareOrdinal(field, other.field);
148                 }
149                 
150                 /// <summary>Resets the field and text of a Term. </summary>
151                 internal void  Set(System.String fld, System.String txt)
152                 {
153                         field = fld;
154                         text = txt;
155                 }
156                 
157                 public override System.String ToString()
158                 {
159                         return field + ":" + text;
160                 }
161                 
162 //              private void  ReadObject(System.IO.BinaryReader in_Renamed)
163 //              {
164 //                      in_Renamed.defaultReadObject();
165 //                      field = StringHelper.Intern(field);
166 //              }
167
168         [System.Runtime.Serialization.OnDeserialized]
169         internal void OnDeserialized(System.Runtime.Serialization.StreamingContext context)
170         {
171             field = StringHelper.Intern(field);
172         }
173
174         public System.String text_ForNUnit
175         {
176             get { return text; }
177         }
178         }
179 }