1 package org.apache.lucene.search;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.index.TermEnum;
24 /** Abstract class for enumerating a subset of all terms.
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;
32 /** the delegate enum - to set this member use {@link #setEnum} */
33 protected TermEnum actualEnum = null;
35 public FilteredTermEnum() {}
37 /** Equality compare on the term */
38 protected abstract boolean termCompare(Term term);
40 /** Equality measure on the term */
41 public abstract float difference();
43 /** Indicates the end of the enumeration has been reached */
44 protected abstract boolean endEnum();
47 * use this method to set the actual TermEnum (e.g. in ctor),
48 * it will be automatically positioned on the first matching term.
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))
60 * Returns the docFreq of the current Term in the enumeration.
61 * Returns -1 if no Term matches or all terms have been enumerated.
64 public int docFreq() {
65 if (currentTerm == null) return -1;
66 assert actualEnum != null;
67 return actualEnum.docFreq();
70 /** Increments the enumeration to the next element. True if one exists. */
72 public boolean next() throws IOException {
73 if (actualEnum == null) return false; // the actual enumerator is not initialized!
75 while (currentTerm == null) {
76 if (endEnum()) return false;
77 if (actualEnum.next()) {
78 Term term = actualEnum.term();
79 if (termCompare(term)) {
90 /** Returns the current Term in the enumeration.
91 * Returns null if no Term matches or all terms have been enumerated. */
97 /** Closes the enumeration to further activity, freeing resources. */
99 public void close() throws IOException {
100 if (actualEnum != null) actualEnum.close();