comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/CrossSectionKMService.java @ 1961:4781096f31f8

Mainly documentation. flys-artifacts/trunk@3368 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 08 Dec 2011 12:01:24 +0000
parents f7d890f4855f
children e0b081105a82
comparison
equal deleted inserted replaced
1960:be06dbc2ed1d 1961:4781096f31f8
1 package de.intevation.flys.artifacts.services; 1 package de.intevation.flys.artifacts.services;
2 2
3 import de.intevation.artifacts.CallMeta; 3 import de.intevation.artifacts.CallMeta;
4 import de.intevation.artifacts.GlobalContext; 4 import de.intevation.artifacts.GlobalContext;
5 5
6 import de.intevation.artifacts.common.ArtifactNamespaceContext;
6 import de.intevation.artifacts.common.utils.XMLUtils; 7 import de.intevation.artifacts.common.utils.XMLUtils;
7 8
8 import de.intevation.flys.artifacts.cache.CacheFactory; 9 import de.intevation.flys.artifacts.cache.CacheFactory;
9 10
10 import de.intevation.flys.backend.SessionHolder; 11 import de.intevation.flys.backend.SessionHolder;
11 12
12 import de.intevation.flys.model.CrossSection; 13 import de.intevation.flys.model.CrossSection;
13 import de.intevation.flys.model.CrossSectionLine; 14 import de.intevation.flys.model.CrossSectionLine;
14 15
16 import java.util.AbstractMap;
15 import java.util.ArrayDeque; 17 import java.util.ArrayDeque;
16 import java.util.Deque; 18 import java.util.Deque;
17 import java.util.List; 19 import java.util.List;
18 import java.util.Map; 20 import java.util.Map;
19 import java.util.NavigableMap; 21 import java.util.NavigableMap;
29 31
30 import org.w3c.dom.Document; 32 import org.w3c.dom.Document;
31 import org.w3c.dom.Element; 33 import org.w3c.dom.Element;
32 import org.w3c.dom.NodeList; 34 import org.w3c.dom.NodeList;
33 35
36
37 /**
38 * Service to find the next/previous km (measurement) of cross sections.
39 * Looking at the query for a single cross-section id at a single km, the
40 * service does the following:
41 *
42 * It returns the km itself if a measurement at that km was found and
43 * the N nearest other measurement points in both directions.
44 *
45 * That means, you can pass N=0 to find out whether a measurement at given km
46 * exists.
47 *
48 * If less than N neighbours exist in one direction, less are delivered
49 * (e.g. given measurements at [0,2,3,4,5,7,8,9] a query for km=8, N=3 will
50 * result in [4,5,7,8,9]).
51 */
34 public class CrossSectionKMService 52 public class CrossSectionKMService
35 extends FLYSService 53 extends FLYSService
36 { 54 {
37 private static Logger logger = 55 private static Logger logger =
38 Logger.getLogger(CrossSectionKMService.class); 56 Logger.getLogger(CrossSectionKMService.class);
39 57
40 public static final String CACHE_NAME = "cross-section-kms"; 58 public static final String CACHE_NAME = "cross-section-kms";
41 59
60
61 /** Trivial constructor. */
42 public CrossSectionKMService() { 62 public CrossSectionKMService() {
43 } 63 }
44 64
65
66 /**
67 * @param data
68 */
45 @Override 69 @Override
46 public Document doProcess( 70 public Document doProcess(
47 Document data, 71 Document data,
48 GlobalContext globalContext, 72 GlobalContext globalContext,
49 CallMeta callMeta 73 CallMeta callMeta
50 ) { 74 ) {
51 logger.debug("CrossSectionKMService.doProcess"); 75 logger.debug("CrossSectionKMService.doProcess");
52 76
53 NodeList crossSectionNodes = 77 NodeList crossSectionNodes =
54 data.getElementsByTagName("cross-section"); 78 data.getElementsByTagName("art:cross-section");
55 79
56 Cache cache = CacheFactory.getCache(CACHE_NAME); 80 Cache cache = CacheFactory.getCache(CACHE_NAME);
57 81
58 Document document = XMLUtils.newDocument(); 82 Document document = XMLUtils.newDocument();
59 83
134 document.appendChild(all); 158 document.appendChild(all);
135 159
136 return document; 160 return document;
137 } 161 }
138 162
163
164 /**
165 * @param km the kilometer from which to start searching for other
166 * measurements
167 * @param N number of neighboring measurements to find.
168 */
139 public static Deque<Map.Entry<Double, Integer>> nearestNeighbors( 169 public static Deque<Map.Entry<Double, Integer>> nearestNeighbors(
140 NavigableMap<Double, Integer> map, 170 NavigableMap<Double, Integer> map,
141 double km, 171 double km,
142 int N 172 int N
143 ) { 173 ) {
144 Deque<Map.Entry<Double, Integer>> result = 174 Deque<Map.Entry<Double, Integer>> result =
145 new ArrayDeque<Map.Entry<Double, Integer>>(2*N); 175 new ArrayDeque<Map.Entry<Double, Integer>>(2*N);
146 176
177 if(map.get(km) != null) {
178 result.add(new AbstractMap.SimpleEntry<Double, Integer>(km,map.get(km)));
179
180 }
181
147 int i = 0; 182 int i = 0;
148 for (Map.Entry<Double, Integer> entry: 183 for (Map.Entry<Double, Integer> entry:
149 map.headMap(km, false).descendingMap().entrySet()) { 184 map.headMap(km, false).descendingMap().entrySet()) {
150 if (i++ >= N) { 185 if (i++ >= N) {
151 break; 186 break;
163 } 198 }
164 199
165 return result; 200 return result;
166 } 201 }
167 202
203
204 /**
205 * @param crossSectionId id of queried cross-section (in db).
206 * @return Mapping from kilometer to db-id.
207 */
168 public static NavigableMap<Double, Integer> getUncached( 208 public static NavigableMap<Double, Integer> getUncached(
169 Integer crossSectionId 209 Integer crossSectionId
170 ) { 210 ) {
171 NavigableMap<Double, Integer> result = 211 NavigableMap<Double, Integer> result =
172 new ConcurrentSkipListMap<Double, Integer>(); 212 new ConcurrentSkipListMap<Double, Integer>();

http://dive4elements.wald.intevation.org