comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java @ 2144:a4bdf7d8527e

Use FastAnnotations for LocationProvider. flys-artifacts/trunk@3723 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 19 Jan 2012 13:43:58 +0000
parents 0c7847b8e85e
children fe3ea0f16098
comparison
equal deleted inserted replaced
2143:c5d24e0587ce 2144:a4bdf7d8527e
4 import net.sf.ehcache.Element; 4 import net.sf.ehcache.Element;
5 5
6 import org.apache.log4j.Logger; 6 import org.apache.log4j.Logger;
7 7
8 import de.intevation.flys.model.Annotation; 8 import de.intevation.flys.model.Annotation;
9 import de.intevation.flys.model.Position; 9 import de.intevation.flys.model.FastAnnotations;
10 import de.intevation.flys.model.Range;
11 10
12 import de.intevation.flys.artifacts.cache.CacheFactory; 11 import de.intevation.flys.artifacts.cache.CacheFactory;
12
13 import de.intevation.flys.artifacts.model.AnnotationsFactory; 13 import de.intevation.flys.artifacts.model.AnnotationsFactory;
14
15 import java.util.Map;
16 import java.util.TreeMap;
17 import java.util.Comparator;
18 import java.util.Iterator;
19
20 import java.io.Serializable;
21
22 14
23 public class LocationProvider { 15 public class LocationProvider {
24 16
25 public static final double EPSILON = 1e-5; 17 private static final Logger log =
18 Logger.getLogger(LocationProvider.class);
26 19
27 public static class KmComparator
28 implements Serializable, Comparator<Double>
29 {
30 @Override
31 public int compare(Double a, Double b) {
32 double diff = a - b;
33 if (diff < -EPSILON) return -1;
34 if (diff > +EPSILON) return +1;
35 return 0;
36 }
37 } // class KmComparator
38 20
39 public static final String CACHE_KEY = "location-provider"; 21 public static final String CACHE_KEY = "location-provider";
40 22
41 public static final String PREFIX = "lp-"; 23 public static final String PREFIX = "lp-";
42
43
44 private static final Logger logger =
45 Logger.getLogger(LocationProvider.class);
46 24
47 25
48 private LocationProvider() { 26 private LocationProvider() {
49 } 27 }
50 28
51 public static String getLocation(String river, double km) { 29 public static String getLocation(String river, double km) {
52 30
53 Double KM = Double.valueOf(km); 31 FastAnnotations fas = getAnnotations(river, km);
54 32
55 return getKmMap(river, KM).get(KM); 33 FastAnnotations.Annotation an = fas.findByKm(km);
34
35 return an != null ? an.getPosition() : null;
56 } 36 }
57 37
58 protected static Map<Double, String> getKmMap(String river, double km) { 38 public static FastAnnotations getAnnotations(String river) {
39 return getAnnotations(river, Double.NaN);
40 }
41
42 protected static FastAnnotations getAnnotations(String river, double km) {
59 43
60 Cache cache = CacheFactory.getCache(CACHE_KEY); 44 Cache cache = CacheFactory.getCache(CACHE_KEY);
61 45
62 if (cache == null) { 46 if (cache == null) {
63 return uncachedKmMap(river, km); 47 return uncachedAnnotations(river, km);
64 } 48 }
65 49
66 String key = PREFIX + river; 50 String key = PREFIX + river;
67 51
68 Element element = cache.get(key); 52 Element element = cache.get(key);
69 53
70 if (element != null) { 54 if (element != null) {
71 return (Map<Double, String>)element.getValue(); 55 return (FastAnnotations)element.getValue();
72 } 56 }
73 57
74 Map<Double, String> map = uncachedKmMap(river, null); 58 FastAnnotations fas = uncachedAnnotations(river, Double.MAX_VALUE);
75 59
76 cache.put(new Element(key, map)); 60 cache.put(new Element(key, fas));
77 61
78 return map; 62 return fas;
79 } 63 }
80 64
81 protected static Map<Double, String> uncachedKmMap( 65 protected static FastAnnotations uncachedAnnotations(
82 String river, 66 String river,
83 Double queryKm 67 double km
84 ) { 68 ) {
85 Map<Double, String> map = 69 if (km != Double.MAX_VALUE) {
86 new TreeMap<Double, String>(new KmComparator()); 70 // XXX Fake it by using a standard Annotation.
87 71
88 if (queryKm != null) {
89 Annotation annotation = 72 Annotation annotation =
90 AnnotationsFactory.getAnnotation(river, queryKm); 73 AnnotationsFactory.getAnnotation(river, km);
74
91 if (annotation != null) { 75 if (annotation != null) {
92 map.put(queryKm, annotation.getPosition().getValue()); 76 FastAnnotations.Annotation fa =
77 new FastAnnotations.Annotation(
78 km, Double.NaN,
79 annotation.getPosition().getValue(), null, null,
80 Double.NaN, Double.NaN);
81 return new FastAnnotations(
82 new FastAnnotations.Annotation [] { fa });
93 } 83 }
94 return map; 84
85 return new FastAnnotations(new FastAnnotations.Annotation[0]);
95 } 86 }
96 87
97 for (Iterator<Annotation> iter = 88 long startTime = System.currentTimeMillis();
98 AnnotationsFactory.getAnnotationsIterator(river); 89
99 iter.hasNext(); 90 FastAnnotations fas = new FastAnnotations(river);
100 ) { 91
101 Annotation annotation = iter.next(); 92 long stopTime = System.currentTimeMillis();
102 Position pos = annotation.getPosition(); 93
103 Range range = annotation.getRange(); 94 if (log.isDebugEnabled()) {
104 Double km = range.getA().doubleValue(); 95 log.debug("Loading locations took " +
105 map.put(km, pos.getValue()); 96 (stopTime-startTime)/1000f + " secs.");
106 } 97 }
107 98
108 return map; 99 return fas;
109 } 100 }
110 } 101 }
111 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 102 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org