001: /**
002: * MultiFilter.java
003: *
004: * Copyright (c) 2000 Douglass R. Cutting.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: */package org.nemesis.forum.search;
020:
021: import java.util.BitSet;
022: import java.util.ArrayList;
023: import java.io.IOException;
024:
025: import org.apache.lucene.index.IndexReader;
026: import org.apache.lucene.search.Filter;
027:
028: /**
029: * A Filter that logically combines multiple other Filters. An arbitrary
030: * number of Filter objects can be added to each MultiFilter. When a Query is
031: * executed with a MultiFilter, each Document in the HitList must pass every
032: * Filter in the MultiFilter filter list.<p>
033: *
034: * For example, consider a MultiFilter that is created with a FilterX filter
035: * and FilterY filter. When a search is executed with the MultiFilter, in order
036: * for Document A to appear in the results, it must pass both the FilterX
037: * <b>and</b> FilterY filters.<p>
038: *
039: * If no Filter objects are added to a MultiFilter before it is used in a
040: * search, this will have the affect of filtering out all search results.
041: *
042: * @author Matt Tucker (matt@Yasna.com)
043: */
044: public class MultiFilter extends Filter {
045:
046: /**
047: * An ArrayList to store the filters that are part of this MultiFilter. We
048: * use an ArrayList instead of a Vector for increased performance. If you
049: * require JDK1.1 support, change to a Vector.
050: */
051: private ArrayList filterList;
052:
053: /**
054: * Creates a new MultiFilter.
055: */
056: public MultiFilter() {
057: filterList = new ArrayList();
058: }
059:
060: /**
061: * Creates a new MultiFilter with the specified initial capacity. Providing
062: * an initial capacity equal to the size of the eventual MultiFilter size
063: * provides a slight performance advantage over letting the MultiFilter
064: * grow automatically.
065: *
066: * @param initialCapacity an initial capacity size for the MultiFilter.
067: */
068: public MultiFilter(int initialCapacity) {
069: filterList = new ArrayList(initialCapacity);
070: }
071:
072: /**
073: * Adds a filter to the MuliFilter filter list.
074: *
075: * @param filter a Filter to add to the MultiFilter filter list.
076: */
077: public void add(Filter filter) {
078: filterList.add(filter);
079: }
080:
081: public BitSet bits(IndexReader reader) throws IOException {
082: //Iterate through list of filters and apply the boolean AND operation
083: //on each bitSet. The AND operator has the affect that only documents
084: //that are allowed by every single filter in the filter list will be
085: //allowed by this MultiFilter.
086: int filterListSize = filterList.size();
087: if (filterListSize > 0) {
088: BitSet bits = ((Filter) filterList.get(0)).bits(reader);
089: for (int i = 1; i < filterListSize; i++) {
090: bits.and(((Filter) filterList.get(i)).bits(reader));
091: }
092: return bits;
093: }
094: //There are no filters defined. In this case, we return a new
095: //BitSet that will filter out all documents. This is probably the most
096: //consistent behavior with the Lucene API. It's also a lot more
097: //efficient considering the BitSet implementation.
098: else {
099: return new BitSet(reader.maxDoc());
100: }
101: }
102: }
|