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
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 
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 
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 }