KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > search > MultiFilter


1 /**
2  * MultiFilter.java
3  *
4  * Copyright (c) 2000 Douglass R. Cutting.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package dlog4j.search;
22
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.BitSet JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.search.Filter;
30
31 /**
32  * A Filter that logically combines multiple other Filters. An arbitrary
33  * number of Filter objects can be added to each MultiFilter. When a Query is
34  * executed with a MultiFilter, each Document in the HitList must pass every
35  * Filter in the MultiFilter filter list.<p>
36  *
37  * For example, consider a MultiFilter that is created with a FilterX filter
38  * and FilterY filter. When a search is executed with the MultiFilter, in order
39  * for Document A to appear in the results, it must pass both the FilterX
40  * <b>and</b> FilterY filters.<p>
41  *
42  * If no Filter objects are added to a MultiFilter before it is used in a
43  * search, this will have the affect of filtering out all search results.
44  *
45  * @author Matt Tucker (matt@coolservlets.com)
46  */

47 public class MultiFilter extends org.apache.lucene.search.Filter {
48
49     /**
50      * An ArrayList to store the filters that are part of this MultiFilter. We
51      * use an ArrayList instead of a Vector for increased performance. If you
52      * require JDK1.1 support, change to a Vector.
53      */

54     private List JavaDoc filterList;
55
56     /**
57      * Creates a new MultiFilter.
58      */

59     public MultiFilter() {
60         filterList = new ArrayList JavaDoc();
61     }
62
63     /**
64      * Creates a new MultiFilter with the specified initial capacity. Providing
65      * an initial capacity equal to the size of the eventual MultiFilter size
66      * provides a slight performance advantage over letting the MultiFilter
67      * grow automatically.
68      *
69      * @param initialCapacity an initial capacity size for the MultiFilter.
70      */

71     public MultiFilter(int initialCapacity) {
72         filterList = new ArrayList JavaDoc(initialCapacity);
73     }
74
75     /**
76      * Adds a filter to the MuliFilter filter list.
77      *
78      * @param filter a Filter to add to the MultiFilter filter list.
79      */

80     public void add(Filter filter) {
81         filterList.add(filter);
82     }
83
84     public BitSet JavaDoc bits(IndexReader reader) throws IOException JavaDoc {
85         //Iterate through list of filters and apply the boolean AND operation
86
//on each bitSet. The AND operator has the affect that only documents
87
//that are allowed by every single filter in the filter list will be
88
//allowed by this MultiFilter.
89
int filterListSize = filterList.size();
90         if (filterListSize > 0) {
91             BitSet JavaDoc bits = ((Filter)filterList.get(0)).bits(reader);
92             for (int i=1; i<filterListSize; i++) {
93                 bits.and( ((Filter)filterList.get(i)).bits(reader) );
94             }
95             return bits;
96         }
97         //There are no filters defined. In this case, we return a new
98
//BitSet that will filter out all documents. This is probably the most
99
//consistent behavior with the Lucene API. It's also a lot more
100
//efficient considering the BitSet implementation.
101
else {
102             return new BitSet JavaDoc(reader.maxDoc());
103         }
104     }
105 }
Popular Tags