KickJava   Java API By Example, From Geeks To Geeks.

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

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

55     private ArrayList JavaDoc filterList;
56
57     /**
58      * Creates a new MultiFilter.
59      */

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

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

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