add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queries / src / java / org / apache / lucene / search / TermsFilter.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 import java.util.Iterator;
22 import java.util.Set;
23 import java.util.TreeSet;
24
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.index.TermDocs;
28 import org.apache.lucene.util.FixedBitSet;
29
30 /**
31  * Constructs a filter for docs matching any of the terms added to this class. 
32  * Unlike a RangeFilter this can be used for filtering on multiple terms that are not necessarily in 
33  * a sequence. An example might be a collection of primary keys from a database query result or perhaps 
34  * a choice of "category" labels picked by the end user. As a filter, this is much faster than the 
35  * equivalent query (a BooleanQuery with many "should" TermQueries)
36  *
37  */
38 public class TermsFilter extends Filter
39 {
40         Set<Term> terms=new TreeSet<Term>();
41         
42         /**
43          * Adds a term to the list of acceptable terms   
44          * @param term
45          */
46         public void addTerm(Term term)
47         {
48                 terms.add(term);
49         }
50         
51 /* (non-Javadoc)
52    * @see org.apache.lucene.search.Filter#getDocIdSet(org.apache.lucene.index.IndexReader)
53          */
54   @Override
55   public DocIdSet getDocIdSet(IndexReader reader) throws IOException
56         {
57     FixedBitSet result=new FixedBitSet(reader.maxDoc());
58         TermDocs td = reader.termDocs();
59         try
60         {
61             for (Iterator<Term> iter = terms.iterator(); iter.hasNext();)
62             {
63                 Term term = iter.next();
64                 td.seek(term);
65                 while (td.next())
66                 {
67                     result.set(td.doc());
68                 }
69             }
70         }
71         finally
72         {
73             td.close();
74         }
75         return result;
76         }
77         
78         @Override
79         public boolean equals(Object obj)
80         {
81                 if(this == obj)
82                         return true;
83                 if((obj == null) || (obj.getClass() != this.getClass()))
84                                 return false;
85                 TermsFilter test = (TermsFilter)obj;
86                 return (terms == test.terms ||
87                                          (terms != null && terms.equals(test.terms)));
88         }
89
90         @Override
91         public int hashCode()
92         {
93                 int hash=9;
94                 for (Iterator<Term> iter = terms.iterator(); iter.hasNext();)
95                 {
96                         Term term = iter.next();
97                         hash = 31 * hash + term.hashCode();                     
98                 }
99                 return hash;
100         }
101         
102 }