add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / tier / CartesianShapeFilter.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.lucene.spatial.tier;
18
19 import java.io.IOException;
20 import java.util.List;
21
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.index.TermDocs;
25 import org.apache.lucene.search.Filter;
26 import org.apache.lucene.search.DocIdSet;
27 import org.apache.lucene.util.NumericUtils;
28 import org.apache.lucene.util.FixedBitSet;
29
30 /**
31  * <p><font color="red"><b>NOTE:</b> This API is still in
32  * flux and might change in incompatible ways in the next
33  * release.</font>
34  */
35 public class CartesianShapeFilter extends Filter {
36  
37   private final Shape shape;
38   private final String fieldName;
39   
40   CartesianShapeFilter(final Shape shape, final String fieldName){
41     this.shape = shape;
42     this.fieldName = fieldName;
43   }
44   
45   @Override
46   public DocIdSet getDocIdSet(final IndexReader reader) throws IOException {
47     final FixedBitSet bits = new FixedBitSet(reader.maxDoc());
48     final TermDocs termDocs = reader.termDocs();
49     final List<Double> area = shape.getArea();
50     int sz = area.size();
51     
52     final Term term = new Term(fieldName);
53     // iterate through each boxid
54     for (int i =0; i< sz; i++) {
55       double boxId = area.get(i).doubleValue();
56       termDocs.seek(term.createTerm(NumericUtils.doubleToPrefixCoded(boxId)));
57       // iterate through all documents
58       // which have this boxId
59       while (termDocs.next()) {
60         bits.set(termDocs.doc());
61       }
62     }
63     return bits;
64   }
65 }