add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / FilteredTermEnum.java
1 package org.apache.lucene.search;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.index.TermEnum;
23
24 /** Abstract class for enumerating a subset of all terms. 
25
26   <p>Term enumerations are always ordered by Term.compareTo().  Each term in
27   the enumeration is greater than all that precede it.  */
28 public abstract class FilteredTermEnum extends TermEnum {
29     /** the current term */
30     protected Term currentTerm = null;
31     
32     /** the delegate enum - to set this member use {@link #setEnum} */
33     protected TermEnum actualEnum = null;
34     
35     public FilteredTermEnum() {}
36
37     /** Equality compare on the term */
38     protected abstract boolean termCompare(Term term);
39     
40     /** Equality measure on the term */
41     public abstract float difference();
42
43     /** Indicates the end of the enumeration has been reached */
44     protected abstract boolean endEnum();
45     
46     /**
47      * use this method to set the actual TermEnum (e.g. in ctor),
48      * it will be automatically positioned on the first matching term.
49      */
50     protected void setEnum(TermEnum actualEnum) throws IOException {
51         this.actualEnum = actualEnum;
52         // Find the first term that matches
53         Term term = actualEnum.term();
54         if (term != null && termCompare(term)) 
55             currentTerm = term;
56         else next();
57     }
58     
59     /** 
60      * Returns the docFreq of the current Term in the enumeration.
61      * Returns -1 if no Term matches or all terms have been enumerated.
62      */
63     @Override
64     public int docFreq() {
65         if (currentTerm == null) return -1;
66         assert actualEnum != null;
67         return actualEnum.docFreq();
68     }
69     
70     /** Increments the enumeration to the next element.  True if one exists. */
71     @Override
72     public boolean next() throws IOException {
73         if (actualEnum == null) return false; // the actual enumerator is not initialized!
74         currentTerm = null;
75         while (currentTerm == null) {
76             if (endEnum()) return false;
77             if (actualEnum.next()) {
78                 Term term = actualEnum.term();
79                 if (termCompare(term)) {
80                     currentTerm = term;
81                     return true;
82                 }
83             }
84             else return false;
85         }
86         currentTerm = null;
87         return false;
88     }
89     
90     /** Returns the current Term in the enumeration.
91      * Returns null if no Term matches or all terms have been enumerated. */
92     @Override
93     public Term term() {
94         return currentTerm;
95     }
96     
97     /** Closes the enumeration to further activity, freeing resources.  */
98     @Override
99     public void close() throws IOException {
100         if (actualEnum != null) actualEnum.close();
101         currentTerm = null;
102         actualEnum = null;
103     }
104 }