Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Search / TermRangeQuery.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 IndexReader = Mono.Lucene.Net.Index.IndexReader;
21 using ToStringUtils = Mono.Lucene.Net.Util.ToStringUtils;
22
23 namespace Mono.Lucene.Net.Search
24 {
25         
26         /// <summary> A Query that matches documents within an exclusive range of terms.
27         /// 
28         /// <p/>This query matches the documents looking for terms that fall into the
29         /// supplied range according to {@link String#compareTo(String)}. It is not intended
30         /// for numerical ranges, use {@link NumericRangeQuery} instead.
31         /// 
32         /// <p/>This query uses the {@link
33         /// MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT}
34         /// rewrite method.
35         /// </summary>
36         /// <since> 2.9
37         /// </since>
38         
39         [Serializable]
40         public class TermRangeQuery:MultiTermQuery
41         {
42                 private System.String lowerTerm;
43                 private System.String upperTerm;
44                 private System.Globalization.CompareInfo collator;
45                 private System.String field;
46                 private bool includeLower;
47                 private bool includeUpper;
48                 
49                 
50                 /// <summary> Constructs a query selecting all terms greater/equal than <code>lowerTerm</code>
51                 /// but less/equal than <code>upperTerm</code>. 
52                 /// 
53                 /// <p/>
54                 /// If an endpoint is null, it is said 
55                 /// to be "open". Either or both endpoints may be open.  Open endpoints may not 
56                 /// be exclusive (you can't select all but the first or last term without 
57                 /// explicitly specifying the term to exclude.)
58                 /// 
59                 /// </summary>
60                 /// <param name="field">The field that holds both lower and upper terms.
61                 /// </param>
62                 /// <param name="lowerTerm">The term text at the lower end of the range
63                 /// </param>
64                 /// <param name="upperTerm">The term text at the upper end of the range
65                 /// </param>
66                 /// <param name="includeLower">If true, the <code>lowerTerm</code> is
67                 /// included in the range.
68                 /// </param>
69                 /// <param name="includeUpper">If true, the <code>upperTerm</code> is
70                 /// included in the range.
71                 /// </param>
72                 public TermRangeQuery(System.String field, System.String lowerTerm, System.String upperTerm, bool includeLower, bool includeUpper):this(field, lowerTerm, upperTerm, includeLower, includeUpper, null)
73                 {
74                 }
75                 
76                 /// <summary>Constructs a query selecting all terms greater/equal than
77                 /// <code>lowerTerm</code> but less/equal than <code>upperTerm</code>.
78                 /// <p/>
79                 /// If an endpoint is null, it is said 
80                 /// to be "open". Either or both endpoints may be open.  Open endpoints may not 
81                 /// be exclusive (you can't select all but the first or last term without 
82                 /// explicitly specifying the term to exclude.)
83                 /// <p/>
84                 /// If <code>collator</code> is not null, it will be used to decide whether
85                 /// index terms are within the given range, rather than using the Unicode code
86                 /// point order in which index terms are stored.
87                 /// <p/>
88                 /// <strong>WARNING:</strong> Using this constructor and supplying a non-null
89                 /// value in the <code>collator</code> parameter will cause every single 
90                 /// index Term in the Field referenced by lowerTerm and/or upperTerm to be
91                 /// examined.  Depending on the number of index Terms in this Field, the 
92                 /// operation could be very slow.
93                 /// 
94                 /// </summary>
95                 /// <param name="lowerTerm">The Term text at the lower end of the range
96                 /// </param>
97                 /// <param name="upperTerm">The Term text at the upper end of the range
98                 /// </param>
99                 /// <param name="includeLower">If true, the <code>lowerTerm</code> is
100                 /// included in the range.
101                 /// </param>
102                 /// <param name="includeUpper">If true, the <code>upperTerm</code> is
103                 /// included in the range.
104                 /// </param>
105                 /// <param name="collator">The collator to use to collate index Terms, to determine
106                 /// their membership in the range bounded by <code>lowerTerm</code> and
107                 /// <code>upperTerm</code>.
108                 /// </param>
109                 public TermRangeQuery(System.String field, System.String lowerTerm, System.String upperTerm, bool includeLower, bool includeUpper, System.Globalization.CompareInfo collator)
110                 {
111                         this.field = field;
112                         this.lowerTerm = lowerTerm;
113                         this.upperTerm = upperTerm;
114                         this.includeLower = includeLower;
115                         this.includeUpper = includeUpper;
116                         this.collator = collator;
117                 }
118                 
119                 /// <summary>Returns the field name for this query </summary>
120                 public virtual System.String GetField()
121                 {
122                         return field;
123                 }
124                 
125                 /// <summary>Returns the lower value of this range query </summary>
126                 public virtual System.String GetLowerTerm()
127                 {
128                         return lowerTerm;
129                 }
130                 
131                 /// <summary>Returns the upper value of this range query </summary>
132                 public virtual System.String GetUpperTerm()
133                 {
134                         return upperTerm;
135                 }
136                 
137                 /// <summary>Returns <code>true</code> if the lower endpoint is inclusive </summary>
138                 public virtual bool IncludesLower()
139                 {
140                         return includeLower;
141                 }
142                 
143                 /// <summary>Returns <code>true</code> if the upper endpoint is inclusive </summary>
144                 public virtual bool IncludesUpper()
145                 {
146                         return includeUpper;
147                 }
148                 
149                 /// <summary>Returns the collator used to determine range inclusion, if any. </summary>
150                 public virtual System.Globalization.CompareInfo GetCollator()
151                 {
152                         return collator;
153                 }
154                 
155                 public /*protected internal*/ override FilteredTermEnum GetEnum(IndexReader reader)
156                 {
157                         return new TermRangeTermEnum(reader, field, lowerTerm, upperTerm, includeLower, includeUpper, collator);
158                 }
159                 
160                 /// <summary>Prints a user-readable version of this query. </summary>
161                 public override System.String ToString(System.String field)
162                 {
163                         System.Text.StringBuilder buffer = new System.Text.StringBuilder();
164                         if (!GetField().Equals(field))
165                         {
166                                 buffer.Append(GetField());
167                                 buffer.Append(":");
168                         }
169                         buffer.Append(includeLower?'[':'{');
170                         buffer.Append(lowerTerm != null?lowerTerm:"*");
171                         buffer.Append(" TO ");
172                         buffer.Append(upperTerm != null?upperTerm:"*");
173                         buffer.Append(includeUpper?']':'}');
174                         buffer.Append(ToStringUtils.Boost(GetBoost()));
175                         return buffer.ToString();
176                 }
177                 
178                 //@Override
179                 public override int GetHashCode()
180                 {
181                         int prime = 31;
182                         int result = base.GetHashCode();
183                         result = prime * result + ((collator == null)?0:collator.GetHashCode());
184                         result = prime * result + ((field == null)?0:field.GetHashCode());
185                         result = prime * result + (includeLower?1231:1237);
186                         result = prime * result + (includeUpper?1231:1237);
187                         result = prime * result + ((lowerTerm == null)?0:lowerTerm.GetHashCode());
188                         result = prime * result + ((upperTerm == null)?0:upperTerm.GetHashCode());
189                         return result;
190                 }
191                 
192                 //@Override
193                 public  override bool Equals(System.Object obj)
194                 {
195                         if (this == obj)
196                                 return true;
197                         if (!base.Equals(obj))
198                                 return false;
199                         if (GetType() != obj.GetType())
200                                 return false;
201                         TermRangeQuery other = (TermRangeQuery) obj;
202                         if (collator == null)
203                         {
204                                 if (other.collator != null)
205                                         return false;
206                         }
207                         else if (!collator.Equals(other.collator))
208                                 return false;
209                         if (field == null)
210                         {
211                                 if (other.field != null)
212                                         return false;
213                         }
214                         else if (!field.Equals(other.field))
215                                 return false;
216                         if (includeLower != other.includeLower)
217                                 return false;
218                         if (includeUpper != other.includeUpper)
219                                 return false;
220                         if (lowerTerm == null)
221                         {
222                                 if (other.lowerTerm != null)
223                                         return false;
224                         }
225                         else if (!lowerTerm.Equals(other.lowerTerm))
226                                 return false;
227                         if (upperTerm == null)
228                         {
229                                 if (other.upperTerm != null)
230                                         return false;
231                         }
232                         else if (!upperTerm.Equals(other.upperTerm))
233                                 return false;
234                         return true;
235                 }
236         }
237 }