Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Search / FilteredTermEnum.cs
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 = Mono.Lucene.Net.Index.Term;
21 using TermEnum = Mono.Lucene.Net.Index.TermEnum;
22
23 namespace Mono.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 {@link #setEnum} </summary>
36                 protected internal TermEnum actualEnum = null;
37                 
38                 public FilteredTermEnum()
39                 {
40                 }
41                 
42                 /// <summary>Equality compare on the term </summary>
43                 public /*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                 /// <summary> use this method to set the actual TermEnum (e.g. in ctor),
52                 /// it will be automatically positioned on the first matching term.
53                 /// </summary>
54                 protected internal virtual void  SetEnum(TermEnum actualEnum)
55                 {
56                         this.actualEnum = actualEnum;
57                         // Find the first term that matches
58                         Term term = actualEnum.Term();
59                         if (term != null && TermCompare(term))
60                                 currentTerm = term;
61                         else
62                                 Next();
63                 }
64                 
65                 /// <summary> Returns the docFreq of the current Term in the enumeration.
66                 /// Returns -1 if no Term matches or all terms have been enumerated.
67                 /// </summary>
68                 public override int DocFreq()
69                 {
70                         if (currentTerm == null)
71                                 return - 1;
72                         System.Diagnostics.Debug.Assert(actualEnum != null);
73                         return actualEnum.DocFreq();
74                 }
75                 
76                 /// <summary>Increments the enumeration to the next element.  True if one exists. </summary>
77                 public override bool Next()
78                 {
79                         if (actualEnum == null)
80                                 return false; // the actual enumerator is not initialized!
81                         currentTerm = null;
82                         while (currentTerm == null)
83                         {
84                                 if (EndEnum())
85                                         return false;
86                                 if (actualEnum.Next())
87                                 {
88                                         Term term = actualEnum.Term();
89                                         if (TermCompare(term))
90                                         {
91                                                 currentTerm = term;
92                                                 return true;
93                                         }
94                                 }
95                                 else
96                                         return false;
97                         }
98                         currentTerm = null;
99                         return false;
100                 }
101                 
102                 /// <summary>Returns the current Term in the enumeration.
103                 /// Returns null if no Term matches or all terms have been enumerated. 
104                 /// </summary>
105                 public override Term Term()
106                 {
107                         return currentTerm;
108                 }
109                 
110                 /// <summary>Closes the enumeration to further activity, freeing resources.  </summary>
111                 public override void  Close()
112                 {
113                         if (actualEnum != null)
114                                 actualEnum.Close();
115                         currentTerm = null;
116                         actualEnum = null;
117                 }
118         }
119 }