Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java @ 1095:f465785ed1ae
Refactored the code to fetch the km range/locations into a utility class.
flys-artifacts/trunk@2598 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 26 Aug 2011 14:58:35 +0000 |
parents | |
children | af73f196eccc |
comparison
equal
deleted
inserted
replaced
1094:b1c6d945848d | 1095:f465785ed1ae |
---|---|
1 package de.intevation.flys.utils; | |
2 | |
3 import gnu.trove.TDoubleArrayList; | |
4 | |
5 import de.intevation.flys.artifacts.FLYSArtifact; | |
6 | |
7 | |
8 public class FLYSUtils { | |
9 | |
10 public static enum KM_MODE { RANGE, LOCATIONS, NONE }; | |
11 | |
12 | |
13 private FLYSUtils() { | |
14 } | |
15 | |
16 | |
17 public static KM_MODE getKmRangeMode(FLYSArtifact flys) { | |
18 String mode = flys.getDataAsString("ld_mode"); | |
19 | |
20 if (mode == null || mode.length() == 0) { | |
21 return KM_MODE.NONE; | |
22 } | |
23 else if (mode.equals("distance")) { | |
24 return KM_MODE.RANGE; | |
25 } | |
26 else if (mode.equals("locations")) { | |
27 return KM_MODE.LOCATIONS; | |
28 } | |
29 else { | |
30 return KM_MODE.NONE; | |
31 } | |
32 } | |
33 | |
34 | |
35 public static double[] getKmRange(FLYSArtifact flys) { | |
36 switch (getKmRangeMode(flys)) { | |
37 case RANGE: { | |
38 return getKmFromTo(flys); | |
39 } | |
40 | |
41 case LOCATIONS: { | |
42 double[] locs = getLocations(flys); | |
43 return new double[] { locs[0], locs[locs.length-1] }; | |
44 } | |
45 | |
46 case NONE: { | |
47 double[] locs = getLocations(flys); | |
48 if (locs != null) { | |
49 return new double[] { locs[0], locs[locs.length-1] }; | |
50 } | |
51 else { | |
52 return getKmFromTo(flys); | |
53 } | |
54 } | |
55 } | |
56 | |
57 return new double[] { Double.NaN, Double.NaN }; | |
58 } | |
59 | |
60 | |
61 public static double[] getKmFromTo(FLYSArtifact flys) { | |
62 String strFrom = flys.getDataAsString("ld_from"); | |
63 String strTo = flys.getDataAsString("ld_to"); | |
64 | |
65 if (strFrom == null || strTo == null) { | |
66 return null; | |
67 } | |
68 | |
69 try { | |
70 return new double[] { | |
71 Double.parseDouble(strFrom), | |
72 Double.parseDouble(strTo) }; | |
73 } | |
74 catch (NumberFormatException nfe) { | |
75 return null; | |
76 } | |
77 } | |
78 | |
79 | |
80 public static double[] getLocations(FLYSArtifact flys) { | |
81 String locationStr = flys.getDataAsString("ld_locations"); | |
82 | |
83 if (locationStr == null || locationStr.length() == 0) { | |
84 return null; | |
85 } | |
86 | |
87 String[] tmp = locationStr.split(" "); | |
88 TDoubleArrayList locations = new TDoubleArrayList(); | |
89 | |
90 for (String l: tmp) { | |
91 try { | |
92 locations.add(Double.parseDouble(l)); | |
93 } | |
94 catch (NumberFormatException nfe) { | |
95 } | |
96 } | |
97 | |
98 locations.sort(); | |
99 | |
100 return locations.toNativeArray(); | |
101 } | |
102 } |