Mercurial > dive4elements > river
diff 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 |
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java Thu Jan 19 13:09:00 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java Thu Jan 19 13:43:58 2012 +0000 @@ -6,61 +6,45 @@ import org.apache.log4j.Logger; import de.intevation.flys.model.Annotation; -import de.intevation.flys.model.Position; -import de.intevation.flys.model.Range; +import de.intevation.flys.model.FastAnnotations; import de.intevation.flys.artifacts.cache.CacheFactory; -import de.intevation.flys.artifacts.model.AnnotationsFactory; -import java.util.Map; -import java.util.TreeMap; -import java.util.Comparator; -import java.util.Iterator; - -import java.io.Serializable; - +import de.intevation.flys.artifacts.model.AnnotationsFactory; public class LocationProvider { - public static final double EPSILON = 1e-5; + private static final Logger log = + Logger.getLogger(LocationProvider.class); - public static class KmComparator - implements Serializable, Comparator<Double> - { - @Override - public int compare(Double a, Double b) { - double diff = a - b; - if (diff < -EPSILON) return -1; - if (diff > +EPSILON) return +1; - return 0; - } - } // class KmComparator public static final String CACHE_KEY = "location-provider"; public static final String PREFIX = "lp-"; - private static final Logger logger = - Logger.getLogger(LocationProvider.class); - - private LocationProvider() { } public static String getLocation(String river, double km) { - Double KM = Double.valueOf(km); + FastAnnotations fas = getAnnotations(river, km); - return getKmMap(river, KM).get(KM); + FastAnnotations.Annotation an = fas.findByKm(km); + + return an != null ? an.getPosition() : null; } - protected static Map<Double, String> getKmMap(String river, double km) { + public static FastAnnotations getAnnotations(String river) { + return getAnnotations(river, Double.NaN); + } + + protected static FastAnnotations getAnnotations(String river, double km) { Cache cache = CacheFactory.getCache(CACHE_KEY); if (cache == null) { - return uncachedKmMap(river, km); + return uncachedAnnotations(river, km); } String key = PREFIX + river; @@ -68,44 +52,51 @@ Element element = cache.get(key); if (element != null) { - return (Map<Double, String>)element.getValue(); + return (FastAnnotations)element.getValue(); } - Map<Double, String> map = uncachedKmMap(river, null); + FastAnnotations fas = uncachedAnnotations(river, Double.MAX_VALUE); - cache.put(new Element(key, map)); + cache.put(new Element(key, fas)); - return map; + return fas; } - protected static Map<Double, String> uncachedKmMap( + protected static FastAnnotations uncachedAnnotations( String river, - Double queryKm + double km ) { - Map<Double, String> map = - new TreeMap<Double, String>(new KmComparator()); - - if (queryKm != null) { + if (km != Double.MAX_VALUE) { + // XXX Fake it by using a standard Annotation. + Annotation annotation = - AnnotationsFactory.getAnnotation(river, queryKm); + AnnotationsFactory.getAnnotation(river, km); + if (annotation != null) { - map.put(queryKm, annotation.getPosition().getValue()); + FastAnnotations.Annotation fa = + new FastAnnotations.Annotation( + km, Double.NaN, + annotation.getPosition().getValue(), null, null, + Double.NaN, Double.NaN); + return new FastAnnotations( + new FastAnnotations.Annotation [] { fa }); } - return map; + + return new FastAnnotations(new FastAnnotations.Annotation[0]); } - for (Iterator<Annotation> iter = - AnnotationsFactory.getAnnotationsIterator(river); - iter.hasNext(); - ) { - Annotation annotation = iter.next(); - Position pos = annotation.getPosition(); - Range range = annotation.getRange(); - Double km = range.getA().doubleValue(); - map.put(km, pos.getValue()); + long startTime = System.currentTimeMillis(); + + FastAnnotations fas = new FastAnnotations(river); + + long stopTime = System.currentTimeMillis(); + + if (log.isDebugEnabled()) { + log.debug("Loading locations took " + + (stopTime-startTime)/1000f + " secs."); } - return map; + return fas; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :