KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > FilteredTermEnum


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import org.apache.lucene.index.Term;
21 import org.apache.lucene.index.TermEnum;
22
23 /** Abstract class for enumerating a subset of all terms.
24
25   <p>Term enumerations are always ordered by Term.compareTo(). Each term in
26   the enumeration is greater than all that precede it. */

27 public abstract class FilteredTermEnum extends TermEnum {
28     private Term currentTerm = null;
29     private TermEnum actualEnum = null;
30     
31     public FilteredTermEnum() {}
32
33     /** Equality compare on the term */
34     protected abstract boolean termCompare(Term term);
35     
36     /** Equality measure on the term */
37     public abstract float difference();
38
39     /** Indiciates the end of the enumeration has been reached */
40     protected abstract boolean endEnum();
41     
42     protected void setEnum(TermEnum actualEnum) throws IOException JavaDoc {
43         this.actualEnum = actualEnum;
44         // Find the first term that matches
45
Term term = actualEnum.term();
46         if (term != null && termCompare(term))
47             currentTerm = term;
48         else next();
49     }
50     
51     /**
52      * Returns the docFreq of the current Term in the enumeration.
53      * Returns -1 if no Term matches or all terms have been enumerated.
54      */

55     public int docFreq() {
56         if (actualEnum == null) return -1;
57         return actualEnum.docFreq();
58     }
59     
60     /** Increments the enumeration to the next element. True if one exists. */
61     public boolean next() throws IOException JavaDoc {
62         if (actualEnum == null) return false; // the actual enumerator is not initialized!
63
currentTerm = null;
64         while (currentTerm == null) {
65             if (endEnum()) return false;
66             if (actualEnum.next()) {
67                 Term term = actualEnum.term();
68                 if (termCompare(term)) {
69                     currentTerm = term;
70                     return true;
71                 }
72             }
73             else return false;
74         }
75         currentTerm = null;
76         return false;
77     }
78     
79     /** Returns the current Term in the enumeration.
80      * Returns null if no Term matches or all terms have been enumerated. */

81     public Term term() {
82         return currentTerm;
83     }
84     
85     /** Closes the enumeration to further activity, freeing resources. */
86     public void close() throws IOException JavaDoc {
87         actualEnum.close();
88         currentTerm = null;
89         actualEnum = null;
90     }
91 }
92
Popular Tags