01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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: private Term currentTerm = null;
30: private TermEnum actualEnum = null;
31:
32: public FilteredTermEnum() {
33: }
34:
35: /** Equality compare on the term */
36: protected abstract boolean termCompare(Term term);
37:
38: /** Equality measure on the term */
39: public abstract float difference();
40:
41: /** Indicates the end of the enumeration has been reached */
42: protected abstract boolean endEnum();
43:
44: protected void setEnum(TermEnum actualEnum) throws IOException {
45: this .actualEnum = actualEnum;
46: // Find the first term that matches
47: Term term = actualEnum.term();
48: if (term != null && termCompare(term))
49: currentTerm = term;
50: else
51: next();
52: }
53:
54: /**
55: * Returns the docFreq of the current Term in the enumeration.
56: * Returns -1 if no Term matches or all terms have been enumerated.
57: */
58: public int docFreq() {
59: if (actualEnum == null)
60: return -1;
61: return actualEnum.docFreq();
62: }
63:
64: /** Increments the enumeration to the next element. True if one exists. */
65: public boolean next() throws IOException {
66: if (actualEnum == null)
67: return false; // the actual enumerator is not initialized!
68: currentTerm = null;
69: while (currentTerm == null) {
70: if (endEnum())
71: return false;
72: if (actualEnum.next()) {
73: Term term = actualEnum.term();
74: if (termCompare(term)) {
75: currentTerm = term;
76: return true;
77: }
78: } else
79: return false;
80: }
81: currentTerm = null;
82: return false;
83: }
84:
85: /** Returns the current Term in the enumeration.
86: * Returns null if no Term matches or all terms have been enumerated. */
87: public Term term() {
88: return currentTerm;
89: }
90:
91: /** Closes the enumeration to further activity, freeing resources. */
92: public void close() throws IOException {
93: actualEnum.close();
94: currentTerm = null;
95: actualEnum = null;
96: }
97: }
|