Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / FilteredTermEnum.cs
blobd3583308f683a479ecadfbac8dd60224f9f74073
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using IndexReader = Lucene.Net.Index.IndexReader;
18 using Term = Lucene.Net.Index.Term;
19 using TermEnum = Lucene.Net.Index.TermEnum;
20 namespace Lucene.Net.Search
23 /// <summary>Abstract class for enumerating a subset of all terms.
24 /// <p>Term enumerations are always ordered by Term.compareTo(). Each term in
25 /// the enumeration is greater than all that precede it.
26 /// </summary>
27 public abstract class FilteredTermEnum:TermEnum
29 private Term currentTerm = null;
30 private TermEnum actualEnum = null;
32 public FilteredTermEnum()
36 /// <summary>Equality compare on the term </summary>
37 protected internal abstract bool TermCompare(Term term);
39 /// <summary>Equality measure on the term </summary>
40 public abstract float Difference();
42 /// <summary>Indiciates the end of the enumeration has been reached </summary>
43 public abstract bool EndEnum();
45 protected internal virtual void SetEnum(TermEnum actualEnum)
47 this.actualEnum = actualEnum;
48 // Find the first term that matches
49 Term term = actualEnum.Term();
50 if (term != null && TermCompare(term))
51 currentTerm = term;
52 else
53 Next();
56 /// <summary> Returns the docFreq of the current Term in the enumeration.
57 /// Initially invalid, valid after next() called for the first time.
58 /// </summary>
59 public override int DocFreq()
61 if (actualEnum == null)
62 return - 1;
63 return actualEnum.DocFreq();
66 /// <summary>Increments the enumeration to the next element. True if one exists. </summary>
67 public override bool Next()
69 if (actualEnum == null)
70 return false; // the actual enumerator is not initialized!
71 currentTerm = null;
72 while (currentTerm == null)
74 if (EndEnum())
75 return false;
76 if (actualEnum.Next())
78 Term term = actualEnum.Term();
79 if (TermCompare(term))
81 currentTerm = term;
82 return true;
85 else
86 return false;
88 currentTerm = null;
89 return false;
92 /// <summary>Returns the current Term in the enumeration.
93 /// Initially invalid, valid after next() called for the first time.
94 /// </summary>
95 public override Term Term()
97 return currentTerm;
100 /// <summary>Closes the enumeration to further activity, freeing resources. </summary>
101 public override void Close()
103 actualEnum.Close();
104 currentTerm = null;
105 actualEnum = null;