comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents 0f7abd95c6e2
children efb067ab2ca4
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.model;
2
3 import net.sf.ehcache.Cache;
4 import net.sf.ehcache.Element;
5
6 import org.apache.log4j.Logger;
7
8 import de.intevation.flys.model.Annotation;
9 import de.intevation.flys.model.FastAnnotations;
10
11 import de.intevation.flys.artifacts.cache.CacheFactory;
12
13 public class LocationProvider {
14
15 private static final Logger log =
16 Logger.getLogger(LocationProvider.class);
17
18
19 public static final String CACHE_KEY = "location-provider";
20
21 public static final String PREFIX = "lp-";
22
23
24 private LocationProvider() {
25 }
26
27 public static String getLocation(String river, double km) {
28
29 FastAnnotations fas = getAnnotations(river, km);
30
31 FastAnnotations.Annotation an = fas.findByKm(km);
32
33 return an != null ? an.getPosition() : null;
34 }
35
36 public static FastAnnotations getAnnotations(String river) {
37 return getAnnotations(river, Double.MAX_VALUE);
38 }
39
40 protected static FastAnnotations getAnnotations(String river, double km) {
41
42 Cache cache = CacheFactory.getCache(CACHE_KEY);
43
44 if (cache == null) {
45 return uncachedAnnotations(river, km);
46 }
47
48 String key = PREFIX + river;
49
50 Element element = cache.get(key);
51
52 if (element != null) {
53 return (FastAnnotations)element.getValue();
54 }
55
56 FastAnnotations fas = uncachedAnnotations(river, Double.MAX_VALUE);
57
58 cache.put(new Element(key, fas));
59
60 return fas;
61 }
62
63 protected static FastAnnotations uncachedAnnotations(
64 String river,
65 double km
66 ) {
67 if (km != Double.MAX_VALUE) {
68 // XXX Fake it by using a standard Annotation.
69
70 Annotation annotation =
71 AnnotationsFactory.getAnnotation(river, km);
72
73 if (annotation != null) {
74 FastAnnotations.Annotation fa =
75 new FastAnnotations.Annotation(
76 km, Double.NaN,
77 annotation.getPosition().getValue(), null, null,
78 Double.NaN, Double.NaN);
79 return new FastAnnotations(
80 new FastAnnotations.Annotation [] { fa });
81 }
82
83 return new FastAnnotations(new FastAnnotations.Annotation[0]);
84 }
85
86 long startTime = System.currentTimeMillis();
87
88 FastAnnotations fas = new FastAnnotations(river);
89
90 long stopTime = System.currentTimeMillis();
91
92 if (log.isDebugEnabled()) {
93 log.debug("Loading locations took " +
94 (stopTime-startTime)/1000f + " secs.");
95 }
96
97 return fas;
98 }
99 }
100 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org