add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / tier / DistanceFieldComparatorSource.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
18 package org.apache.lucene.spatial.tier;
19
20 import java.io.IOException;
21
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.search.Filter;
24 import org.apache.lucene.search.FieldComparator;
25 import org.apache.lucene.search.FieldComparatorSource;
26
27 /**
28  * <p><font color="red"><b>NOTE:</b> This API is still in
29  * flux and might change in incompatible ways in the next
30  * release.</font>
31  */
32 public class DistanceFieldComparatorSource extends FieldComparatorSource {
33
34   private static final long serialVersionUID = 1L;
35
36   private DistanceFilter distanceFilter;
37   private DistanceScoreDocLookupComparator dsdlc;
38
39   public DistanceFieldComparatorSource(Filter distanceFilter) {
40     this.distanceFilter = (DistanceFilter) distanceFilter;
41   }
42
43   public void cleanUp() {
44     distanceFilter = null;
45
46     if (dsdlc != null) {
47       dsdlc.cleanUp();
48     }
49
50     dsdlc = null;
51   }
52
53   @Override
54   public FieldComparator newComparator(String fieldname, int numHits,
55                                          int sortPos, boolean reversed) throws IOException {
56     dsdlc = new DistanceScoreDocLookupComparator(numHits);
57     return dsdlc;
58   }
59
60   private class DistanceScoreDocLookupComparator extends FieldComparator<Double> {
61
62     private double[] values;
63     private double bottom;
64     private int offset =0;
65                 
66     public DistanceScoreDocLookupComparator(int numHits) {
67       values = new double[numHits];
68       return;
69     }
70
71     @Override
72     public int compare(int slot1, int slot2) {
73       double a = values[slot1];
74       double b = values[slot2];
75       if (a > b)
76         return 1;
77       if (a < b)
78         return -1;
79
80       return 0;
81     }
82
83     public void cleanUp() {
84       distanceFilter = null;
85     }
86
87     @Override
88     public int compareBottom(int doc) {
89       double v2 = distanceFilter.getDistance(doc+ offset);
90                         
91       if (bottom > v2) {
92         return 1;
93       } else if (bottom < v2) {
94         return -1;
95       }
96       return 0;
97     }
98
99     @Override
100     public void copy(int slot, int doc) {
101       values[slot] = distanceFilter.getDistance(doc + offset);
102     }
103
104     @Override
105     public void setBottom(int slot) {
106       this.bottom = values[slot];
107     }
108
109     @Override
110     public void setNextReader(IndexReader reader, int docBase)
111       throws IOException {
112                         
113       // each reader in a segmented base
114       // has an offset based on the maxDocs of previous readers
115       offset = docBase;
116     }
117
118     @Override
119     public Double value(int slot) {
120       return values[slot];
121     }
122   }
123 }