comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java @ 3818:dc18457b1cef

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

http://dive4elements.wald.intevation.org