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
FilteredTermEnum.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 Term = Lucene.Net.Index.Term;
21 using TermEnum = Lucene.Net.Index.TermEnum;
22 
23 namespace Lucene.Net.Search
24 {
25 
26  /// <summary>Abstract class for enumerating a subset of all terms.
27  /// <p/>Term enumerations are always ordered by Term.compareTo(). Each term in
28  /// the enumeration is greater than all that precede it.
29  /// </summary>
30  public abstract class FilteredTermEnum:TermEnum
31  {
32  /// <summary>the current term </summary>
33  protected internal Term currentTerm = null;
34 
35  /// <summary>the delegate enum - to set this member use <see cref="SetEnum" /> </summary>
36  protected internal TermEnum actualEnum = null;
37 
38  protected FilteredTermEnum()
39  {
40  }
41 
42  /// <summary>Equality compare on the term </summary>
43  protected internal abstract bool TermCompare(Term term);
44 
45  /// <summary>Equality measure on the term </summary>
46  public abstract float Difference();
47 
48  /// <summary>Indicates the end of the enumeration has been reached </summary>
49  public abstract bool EndEnum();
50 
51  private bool isDisposed;
52 
53  /// <summary> use this method to set the actual TermEnum (e.g. in ctor),
54  /// it will be automatically positioned on the first matching term.
55  /// </summary>
56  protected internal virtual void SetEnum(TermEnum actualEnum)
57  {
58  this.actualEnum = actualEnum;
59  // Find the first term that matches
60  Term term = actualEnum.Term;
61  if (term != null && TermCompare(term))
62  currentTerm = term;
63  else
64  Next();
65  }
66 
67  /// <summary> Returns the docFreq of the current Term in the enumeration.
68  /// Returns -1 if no Term matches or all terms have been enumerated.
69  /// </summary>
70  public override int DocFreq()
71  {
72  if (currentTerm == null)
73  return - 1;
74  System.Diagnostics.Debug.Assert(actualEnum != null);
75  return actualEnum.DocFreq();
76  }
77 
78  /// <summary>Increments the enumeration to the next element. True if one exists. </summary>
79  public override bool Next()
80  {
81  if (actualEnum == null)
82  return false; // the actual enumerator is not initialized!
83  currentTerm = null;
84  while (currentTerm == null)
85  {
86  if (EndEnum())
87  return false;
88  if (actualEnum.Next())
89  {
90  Term term = actualEnum.Term;
91  if (TermCompare(term))
92  {
93  currentTerm = term;
94  return true;
95  }
96  }
97  else
98  return false;
99  }
100  currentTerm = null;
101  return false;
102  }
103 
104  /// <summary>Returns the current Term in the enumeration.
105  /// Returns null if no Term matches or all terms have been enumerated.
106  /// </summary>
107  public override Term Term
108  {
109  get { return currentTerm; }
110  }
111 
112  protected override void Dispose(bool disposing)
113  {
114  if (isDisposed) return;
115 
116  if (disposing)
117  {
118  if (actualEnum != null)
119  actualEnum.Close();
120  currentTerm = null;
121  actualEnum = null;
122  }
123 
124  isDisposed = true;
125  }
126  }
127 }