add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / FilteredDocIdSet.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
22 /**
23  * Abstract decorator class for a DocIdSet implementation
24  * that provides on-demand filtering/validation
25  * mechanism on a given DocIdSet.
26  *
27  * <p/>
28  *
29  * Technically, this same functionality could be achieved
30  * with ChainedFilter (under contrib/misc), however the
31  * benefit of this class is it never materializes the full
32  * bitset for the filter.  Instead, the {@link #match}
33  * method is invoked on-demand, per docID visited during
34  * searching.  If you know few docIDs will be visited, and
35  * the logic behind {@link #match} is relatively costly,
36  * this may be a better way to filter than ChainedFilter.
37  *
38  * @see DocIdSet
39  */
40
41 public abstract class FilteredDocIdSet extends DocIdSet {
42   private final DocIdSet _innerSet;
43   
44   /**
45    * Constructor.
46    * @param innerSet Underlying DocIdSet
47    */
48   public FilteredDocIdSet(DocIdSet innerSet) {
49     _innerSet = innerSet;
50   }
51   
52   /** This DocIdSet implementation is cacheable if the inner set is cacheable. */
53   @Override
54   public boolean isCacheable() {
55     return _innerSet.isCacheable();
56   }
57
58   /**
59    * Validation method to determine whether a docid should be in the result set.
60    * @param docid docid to be tested
61    * @return true if input docid should be in the result set, false otherwise.
62    */
63   protected abstract boolean match(int docid) throws IOException;
64         
65   /**
66    * Implementation of the contract to build a DocIdSetIterator.
67    * @see DocIdSetIterator
68    * @see FilteredDocIdSetIterator
69    */
70   @Override
71   public DocIdSetIterator iterator() throws IOException {
72     return new FilteredDocIdSetIterator(_innerSet.iterator()) {
73       @Override
74       protected boolean match(int docid) throws IOException {
75         return FilteredDocIdSet.this.match(docid);
76       }
77     };
78   }
79 }