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
TermRangeTermEnum.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 IndexReader = Lucene.Net.Index.IndexReader;
21 using Term = Lucene.Net.Index.Term;
22 using StringHelper = Lucene.Net.Util.StringHelper;
23 
24 namespace Lucene.Net.Search
25 {
26 
27  /// <summary> Subclass of FilteredTermEnum for enumerating all terms that match the
28  /// specified range parameters.
29  /// <p/>
30  /// Term enumerations are always ordered by Term.compareTo(). Each term in
31  /// the enumeration is greater than all that precede it.
32  /// </summary>
33  /// <since> 2.9
34  /// </since>
36  {
37 
38  private System.Globalization.CompareInfo collator = null;
39  private bool endEnum = false;
40  private System.String field;
41  private System.String upperTermText;
42  private System.String lowerTermText;
43  private bool includeLower;
44  private bool includeUpper;
45 
46  /// <summary> Enumerates all terms greater/equal than <c>lowerTerm</c>
47  /// but less/equal than <c>upperTerm</c>.
48  ///
49  /// If an endpoint is null, it is said to be "open". Either or both
50  /// endpoints may be open. Open endpoints may not be exclusive
51  /// (you can't select all but the first or last term without
52  /// explicitly specifying the term to exclude.)
53  ///
54  /// </summary>
55  /// <param name="reader">
56  /// </param>
57  /// <param name="field">An interned field that holds both lower and upper terms.
58  /// </param>
59  /// <param name="lowerTermText">The term text at the lower end of the range
60  /// </param>
61  /// <param name="upperTermText">The term text at the upper end of the range
62  /// </param>
63  /// <param name="includeLower">If true, the <c>lowerTerm</c> is included in the range.
64  /// </param>
65  /// <param name="includeUpper">If true, the <c>upperTerm</c> is included in the range.
66  /// </param>
67  /// <param name="collator">The collator to use to collate index Terms, to determine their
68  /// membership in the range bounded by <c>lowerTerm</c> and
69  /// <c>upperTerm</c>.
70  ///
71  /// </param>
72  /// <throws> IOException </throws>
73  public TermRangeTermEnum(IndexReader reader, System.String field, System.String lowerTermText, System.String upperTermText, bool includeLower, bool includeUpper, System.Globalization.CompareInfo collator)
74  {
75  this.collator = collator;
76  this.upperTermText = upperTermText;
77  this.lowerTermText = lowerTermText;
78  this.includeLower = includeLower;
79  this.includeUpper = includeUpper;
80  this.field = StringHelper.Intern(field);
81 
82  // do a little bit of normalization...
83  // open ended range queries should always be inclusive.
84  if (this.lowerTermText == null)
85  {
86  this.lowerTermText = "";
87  this.includeLower = true;
88  }
89 
90  if (this.upperTermText == null)
91  {
92  this.includeUpper = true;
93  }
94 
95  System.String startTermText = collator == null?this.lowerTermText:"";
96  SetEnum(reader.Terms(new Term(this.field, startTermText)));
97  }
98 
99  public override float Difference()
100  {
101  return 1.0f;
102  }
103 
104  public override bool EndEnum()
105  {
106  return endEnum;
107  }
108 
109  protected internal override bool TermCompare(Term term)
110  {
111  if (collator == null)
112  {
113  // Use Unicode code point ordering
114  bool checkLower = !includeLower;
115  if (term != null && (System.Object) term.Field == (System.Object) field)
116  {
117  // interned comparison
118  if (!checkLower || null == lowerTermText || String.CompareOrdinal(term.Text, lowerTermText) > 0)
119  {
120  checkLower = false;
121  if (upperTermText != null)
122  {
123  int compare = String.CompareOrdinal(upperTermText, term.Text);
124  /*
125  * if beyond the upper term, or is exclusive and this is equal to
126  * the upper term, break out
127  */
128  if ((compare < 0) || (!includeUpper && compare == 0))
129  {
130  endEnum = true;
131  return false;
132  }
133  }
134  return true;
135  }
136  }
137  else
138  {
139  // break
140  endEnum = true;
141  return false;
142  }
143  return false;
144  }
145  else
146  {
147  if (term != null && (System.Object) term.Field == (System.Object) field)
148  {
149  // interned comparison
150  if ((lowerTermText == null || (includeLower?collator.Compare(term.Text.ToString(), lowerTermText.ToString()) >= 0:collator.Compare(term.Text.ToString(), lowerTermText.ToString()) > 0)) && (upperTermText == null || (includeUpper?collator.Compare(term.Text.ToString(), upperTermText.ToString()) <= 0:collator.Compare(term.Text.ToString(), upperTermText.ToString()) < 0)))
151  {
152  return true;
153  }
154  return false;
155  }
156  endEnum = true;
157  return false;
158  }
159  }
160  }
161 }