pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / tier / DistanceFilter.java
1 /** Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.apache.lucene.spatial.tier;
18
19 import java.util.Map;
20 import java.util.WeakHashMap;
21 import java.util.HashMap;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24
25 import org.apache.lucene.search.Filter;
26 import org.apache.lucene.spatial.tier.DistanceHandler.Precision;
27
28 /**
29  * <p><font color="red"><b>NOTE:</b> This API is still in
30  * flux and might change in incompatible ways in the next
31  * release.</font>
32  */
33 public abstract class DistanceFilter extends Filter {
34
35   final protected Filter startingFilter;
36   protected Precision precise;
37   protected Map<Integer,Double> distances;
38   protected double distance;
39
40   protected int nextDocBase; 
41   protected transient WeakHashMap<String,Double> distanceLookupCache;
42
43   /** Filters the startingFilter by precise distance
44    *  checking filter */
45   public DistanceFilter(Filter startingFilter, double distance) {
46     if (startingFilter == null) {
47       throw new IllegalArgumentException("please provide a non-null startingFilter; you can use QueryWrapperFilter(MatchAllDocsQuery) as a no-op filter");
48     }
49     this.startingFilter = startingFilter;
50     this.distance = distance;
51
52     // NOTE: neither of the distance filters use precision
53     // now - if we turn that on, we'll need to pass top
54     // reader into here
55     // setPrecision(reader.maxDoc());
56
57     /* store calculated distances for reuse by other components */
58     distances = new HashMap<Integer,Double>();
59
60     // create an intermediate cache to avoid recomputing
61     //   distances for the same point 
62     //   TODO: Why is this a WeakHashMap? 
63     distanceLookupCache = new WeakHashMap<String,Double>();
64   }
65
66   /** needed for deserialization, because the cache is transient */
67   private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
68     stream.defaultReadObject();
69     distanceLookupCache = new WeakHashMap<String,Double>();
70   }
71
72   public Map<Integer,Double> getDistances(){
73     return distances;
74   }
75   
76   public Double getDistance(int docid){
77     return distances.get(docid);
78   }
79   
80   public void setDistances(Map<Integer, Double> distances) {
81     this.distances = distances;
82   }
83
84   /** You must call this before re-using this DistanceFilter
85    *  across searches */
86   public void reset() {
87     nextDocBase = 0;
88   }
89
90   /** Returns true if <code>o</code> is equal to this. */
91   @Override
92   public abstract boolean equals(Object o);
93
94   /** Returns a hash code value for this object.*/
95   @Override
96   public abstract int hashCode();
97
98   /*
99   private void setPrecision(int maxDocs) {
100     precise = Precision.EXACT;
101     
102     if (maxDocs > 1000 && distance > 10) {
103       precise = Precision.TWENTYFEET;
104     }
105     
106     if (maxDocs > 10000 && distance > 10){
107       precise = Precision.TWOHUNDREDFEET;
108     }
109   }
110   */
111 }