/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.lucene.spatial.tier;

import java.util.HashMap;
import java.util.Map;

/**
 * Provide a high level access point to distances
 * Used by DistanceSortSource and DistanceQuery
 *  
 * <p><font color="red"><b>NOTE:</b> This API is still in
 * flux and might change in incompatible ways in the next
 * release.</font>
 *
 */
public class DistanceHandler {
  
  private Map<Integer,Double> distances;
  public enum Precision {EXACT, TWOFEET, TWENTYFEET, TWOHUNDREDFEET};
  private Precision precise;
  
  public DistanceHandler (Map<Integer,Double> distances, Precision precise){
    this.distances = distances;
    this.precise = precise; 
    
  }
  
  
  public static double getPrecision(double x, Precision thisPrecise){
    
    if(thisPrecise != null){
      double dif = 0;
      switch(thisPrecise) {
        case EXACT: return x;
        case TWOFEET:        dif = x % 0.0001; break;
        case TWENTYFEET:     dif = x % 0.001;  break;
        case TWOHUNDREDFEET: dif = x % 0.01; break;
      }
      return x - dif;
    }
    return x;
  }
  
  public Precision getPrecision() {
    return precise;
  }
  
  public double getDistance(int docid, double centerLat, double centerLng, double lat, double lng){
  
    // check to see if we have distances
    // if not calculate the distance
    if(distances == null){
      return DistanceUtils.getInstance().getDistanceMi(centerLat, centerLng, lat, lng);
    }
    
    // check to see if the doc id has a cached distance
    Double docd = distances.get( docid );
    if (docd != null){
      return docd.doubleValue();
    }
    
    //check to see if we have a precision code
    // and if another lat/long has been calculated at
    // that rounded location
    if (precise != null) {
      double xLat = getPrecision(lat, precise);
      double xLng = getPrecision(lng, precise);
      
      String k = new Double(xLat).toString() +","+ new Double(xLng).toString();
    
      Double d = (distances.get(k));
      if (d != null){
        return d.doubleValue();
      }
    }
    
    //all else fails calculate the distances    
    return DistanceUtils.getInstance().getDistanceMi(centerLat, centerLng, lat, lng);
  }
  
  
  public static void main(String args[]){ 
    DistanceHandler db = new DistanceHandler(new HashMap(), Precision.TWOHUNDREDFEET);
    System.out.println(DistanceHandler.getPrecision(-1234.123456789, db.getPrecision()));
  }
}

@@ -1171,51 +1171,8 @@
 r %7B%0A
-  %0A  private Map%3CInteger,Double%3E distances;
 %0A  p
@@ -1238,16 +1238,111 @@
 DFEET%7D;%0A
+  %0A  private Map%3CInteger,Double%3E distances;%0A  private Map%3CString, Double%3E distanceLookupCache;%0A
   privat
@@ -1421,16 +1421,57 @@
 stances,
+ Map%3CString, Double%3E distanceLookupCache,
  Precisi
@@ -1515,16 +1515,68 @@
 tances;%0A
+    this.distanceLookupCache = distanceLookupCache;%0A
     this
@@ -1596,21 +1596,16 @@
 ecise; %0A
-    %0A
   %7D%0A  %0A 
@@ -2909,25 +2909,35 @@
  = (distance
-s
+LookupCache
 .get(k));%0A  
@@ -3254,16 +3254,31 @@
 shMap(),
+ new HashMap(),
  Precisi

fix using the wrong cache