import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Filter;

import java.io.IOException;
import java.util.BitSet;

/**
 * <p>
 * A ChainableFilter allows multiple filters to be chained
 * such that the result is the intersection of all the
 * filters.
 * </p>
 * <p>
 * Order in which filters are called depends on
 * the position of the filter in the chain. It's probably
 * more efficient to place the most restrictive filters
 * /least computationally-intensive filters first.
 * </p>
 *
 * @author <a href="mailto:kelvin@relevanz.com">Kelvin Tan</a>
 */
public class ChainableFilter extends Filter
{
    /** The filter chain */
    private Filter[] chain = null;

    /**
     * Creates a new ChainableFilter.
     *
     * @param chain The chain of filters.
     */
    public ChainableFilter(Filter[] chain)
    {
        this.chain = chain;
    }

    public BitSet bits(IndexReader reader) throws IOException
    {
        BitSet result = null;
        for (int i = 0; i < chain.length; i++)
        {
            result = chain[i].bits(reader);
        }
        return result;
    }
}
