2006-09-10 Francisco Javier F. Serrador <serrador@openshine.com>
[beagle.git] / beagled / Lucene.Net / Search / FilteredTermEnum.cs
blob386dfc7dca4f8b8373ab38d59ba287aba203e83c
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 Term = Lucene.Net.Index.Term;
18 using TermEnum = Lucene.Net.Index.TermEnum;
19 namespace Lucene.Net.Search
22 /// <summary>Abstract class for enumerating a subset of all terms.
23 /// <p>Term enumerations are always ordered by Term.compareTo(). Each term in
24 /// the enumeration is greater than all that precede it.
25 /// </summary>
26 public abstract class FilteredTermEnum:TermEnum
28 private Term currentTerm = null;
29 private TermEnum actualEnum = null;
31 public FilteredTermEnum()
35 /// <summary>Equality compare on the term </summary>
36 protected internal abstract bool TermCompare(Term term);
38 /// <summary>Equality measure on the term </summary>
39 public abstract float Difference();
41 /// <summary>Indiciates the end of the enumeration has been reached </summary>
42 public abstract bool EndEnum();
44 protected internal virtual void SetEnum(TermEnum actualEnum)
46 this.actualEnum = actualEnum;
47 // Find the first term that matches
48 Term term = actualEnum.Term();
49 if (term != null && TermCompare(term))
50 currentTerm = term;
51 else
52 Next();
55 /// <summary> Returns the docFreq of the current Term in the enumeration.
56 /// Returns -1 if no Term matches or all terms have been enumerated.
57 /// </summary>
58 public override int DocFreq()
60 if (actualEnum == null)
61 return - 1;
62 return actualEnum.DocFreq();
65 /// <summary>Increments the enumeration to the next element. True if one exists. </summary>
66 public override bool Next()
68 if (actualEnum == null)
69 return false; // the actual enumerator is not initialized!
70 currentTerm = null;
71 while (currentTerm == null)
73 if (EndEnum())
74 return false;
75 if (actualEnum.Next())
77 Term term = actualEnum.Term();
78 if (TermCompare(term))
80 currentTerm = term;
81 return true;
84 else
85 return false;
87 currentTerm = null;
88 return false;
91 /// <summary>Returns the current Term in the enumeration.
92 /// Returns null if no Term matches or all terms have been enumerated.
93 /// </summary>
94 public override Term Term()
96 return currentTerm;
99 /// <summary>Closes the enumeration to further activity, freeing resources. </summary>
100 public override void Close()
102 actualEnum.Close();
103 currentTerm = null;
104 actualEnum = null;