Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java @ 2141:79a94c4171cb
Cosmetics.
flys-artifacts/trunk@3719 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Thu, 19 Jan 2012 13:01:33 +0000 |
parents | 0c7847b8e85e |
children | a4bdf7d8527e |
line wrap: on
line source
package de.intevation.flys.artifacts.model; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; 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.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; public class LocationProvider { public static final double EPSILON = 1e-5; 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); return getKmMap(river, KM).get(KM); } protected static Map<Double, String> getKmMap(String river, double km) { Cache cache = CacheFactory.getCache(CACHE_KEY); if (cache == null) { return uncachedKmMap(river, km); } String key = PREFIX + river; Element element = cache.get(key); if (element != null) { return (Map<Double, String>)element.getValue(); } Map<Double, String> map = uncachedKmMap(river, null); cache.put(new Element(key, map)); return map; } protected static Map<Double, String> uncachedKmMap( String river, Double queryKm ) { Map<Double, String> map = new TreeMap<Double, String>(new KmComparator()); if (queryKm != null) { Annotation annotation = AnnotationsFactory.getAnnotation(river, queryKm); if (annotation != null) { map.put(queryKm, annotation.getPosition().getValue()); } return map; } 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()); } return map; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :