Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/DischargeState.java @ 9390:f575ff573cbb
"Name der Peilung" columname minfo.
author | gernotbelger |
---|---|
date | Thu, 09 Aug 2018 15:22:31 +0200 |
parents | e4606eae8ea5 |
children |
comparison
equal
deleted
inserted
replaced
9389:63c086139391 | 9390:f575ff573cbb |
---|---|
6 * documentation coming with Dive4Elements River for details. | 6 * documentation coming with Dive4Elements River for details. |
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.artifacts.states; | 9 package org.dive4elements.river.artifacts.states; |
10 | 10 |
11 import java.util.List; | |
12 import java.util.Collections; | 11 import java.util.Collections; |
13 import java.util.Comparator; | 12 import java.util.Comparator; |
13 import java.util.List; | |
14 | 14 |
15 import org.apache.log4j.Logger; | 15 import org.apache.log4j.Logger; |
16 | |
17 import org.dive4elements.artifacts.Artifact; | 16 import org.dive4elements.artifacts.Artifact; |
18 import org.dive4elements.artifacts.CallContext; | 17 import org.dive4elements.artifacts.CallContext; |
19 | |
20 import org.dive4elements.artifacts.common.model.KVP; | 18 import org.dive4elements.artifacts.common.model.KVP; |
21 | 19 import org.dive4elements.river.artifacts.D4EArtifact; |
22 import org.dive4elements.river.model.DischargeZone; | 20 import org.dive4elements.river.model.DischargeZone; |
23 import org.dive4elements.river.model.River; | 21 import org.dive4elements.river.model.River; |
24 | |
25 import org.dive4elements.river.artifacts.D4EArtifact; | |
26 import org.dive4elements.river.utils.RiverUtils; | 22 import org.dive4elements.river.utils.RiverUtils; |
27 | |
28 | 23 |
29 public class DischargeState extends MultiIntArrayState { | 24 public class DischargeState extends MultiIntArrayState { |
30 | 25 |
31 public static final String MAIN_CHANNEL = "main_channel"; | 26 public static final String MAIN_CHANNEL = "main_channel"; |
32 public static final String TOTAL_CHANNEL = "total_channel"; | 27 public static final String TOTAL_CHANNEL = "total_channel"; |
33 | 28 |
34 | |
35 private static final Logger log = Logger.getLogger(DischargeState.class); | 29 private static final Logger log = Logger.getLogger(DischargeState.class); |
36 | |
37 | 30 |
38 /** Let client display a matrix. */ | 31 /** Let client display a matrix. */ |
39 @Override | 32 @Override |
40 public String getUIProvider() { | 33 public String getUIProvider() { |
41 return "parameter-matrix"; | 34 return "parameter-matrix-flowvelocity"; |
42 } | 35 } |
43 | |
44 | 36 |
45 /** | 37 /** |
46 * This method fetches all DischargeZones for a given river (extracted from | 38 * This method fetches all DischargeZones for a given river (extracted from |
47 * <i>artifact</i>) and returns a KVP[] where the key is the ID of the | 39 * <i>artifact</i>) and returns a KVP[] where the key is the ID of the |
48 * DischargeZone and the value is a string that consists of lower discharge | 40 * DischargeZone and the value is a string that consists of lower discharge |
49 * and upper discharge. | 41 * and upper discharge. |
50 * | 42 * |
51 * @param artifact Needs to be a D4EArtifact that provides river | 43 * @param artifact |
52 * information. | 44 * Needs to be a D4EArtifact that provides river |
53 * @param parameterName The name of a parameter. | 45 * information. |
46 * @param parameterName | |
47 * The name of a parameter. | |
54 * | 48 * |
55 * @return a KVP[]. | 49 * @return a KVP[]. |
56 */ | 50 */ |
57 @Override | 51 @Override |
58 protected KVP<Integer, String>[] getOptions( | 52 protected KVP<Integer, String>[] getOptions(final Artifact artifact, final String parameterName) throws IllegalArgumentException { |
59 Artifact artifact, | |
60 String parameterName | |
61 ) | |
62 throws IllegalArgumentException | |
63 { | |
64 if (!testParameterName(parameterName)) { | 53 if (!testParameterName(parameterName)) { |
65 throw new IllegalArgumentException( | 54 throw new IllegalArgumentException("Invalid parameter for state: '" + parameterName + "'"); |
66 "Invalid parameter for state: '" + parameterName + "'"); | |
67 } | 55 } |
68 | 56 |
69 List<DischargeZone> zones = getDischargeZones(artifact); | 57 final List<DischargeZone> zones = getDischargeZones(artifact); |
70 | 58 |
71 KVP[] kvp = new KVP[zones.size()]; | 59 final KVP[] kvp = new KVP[zones.size()]; |
72 | 60 |
73 Collections.sort(zones, new Comparator<DischargeZone>() { | 61 Collections.sort(zones, new Comparator<DischargeZone>() { |
74 @Override | 62 @Override |
75 public int compare(DischargeZone a, DischargeZone b) { | 63 public int compare(final DischargeZone a, final DischargeZone b) { |
76 return a.getValue().compareTo(b.getValue()); | 64 return a.getValue().compareTo(b.getValue()); |
77 } | 65 } |
78 }); | 66 }); |
79 | 67 |
80 int i = 0; | 68 int i = 0; |
81 | 69 |
82 for (DischargeZone zone: zones) { | 70 for (final DischargeZone zone : zones) { |
83 String lower = zone.getLowerDischarge(); | 71 final String lower = zone.getLowerDischarge(); |
84 String upper = zone.getUpperDischarge(); | 72 final String upper = zone.getUpperDischarge(); |
85 | 73 |
86 if (lower.equals(upper)) { | 74 if (lower.equals(upper)) { |
87 kvp[i] = new KVP(zone.getId(), lower); | 75 kvp[i] = new KVP(zone.getId(), lower); |
88 } | 76 } else { |
89 else { | |
90 kvp[i] = new KVP(zone.getId(), lower + " - " + upper); | 77 kvp[i] = new KVP(zone.getId(), lower + " - " + upper); |
91 } | 78 } |
92 i++; | 79 i++; |
93 } | 80 } |
94 | 81 |
95 return kvp; | 82 return kvp; |
96 } | 83 } |
97 | 84 |
98 | |
99 @Override | 85 @Override |
100 protected String getLabelFor( | 86 protected String getLabelFor(final CallContext cc, final String parameterName, final int value) throws IllegalArgumentException { |
101 CallContext cc, | |
102 String parameterName, | |
103 int value | |
104 ) throws IllegalArgumentException | |
105 { | |
106 if (!testParameterName(parameterName)) { | 87 if (!testParameterName(parameterName)) { |
107 throw new IllegalArgumentException( | 88 throw new IllegalArgumentException("Invalid parameter for state: '" + parameterName + "'"); |
108 "Invalid parameter for state: '" + parameterName + "'"); | |
109 } | 89 } |
110 | 90 |
111 DischargeZone zone = DischargeZone.getDischargeZoneById(value); | 91 final DischargeZone zone = DischargeZone.getDischargeZoneById(value); |
112 | 92 |
113 if (zone == null) { | 93 if (zone == null) { |
114 throw new IllegalArgumentException( | 94 throw new IllegalArgumentException("Invalid id for DischargeZone: '" + value + "'"); |
115 "Invalid id for DischargeZone: '" + value + "'"); | |
116 } | 95 } |
117 | 96 |
118 String lo = zone.getLowerDischarge(); | 97 final String lo = zone.getLowerDischarge(); |
119 String hi = zone.getUpperDischarge(); | 98 final String hi = zone.getUpperDischarge(); |
120 | 99 |
121 return hi != null && !lo.equals(hi) | 100 return hi != null && !lo.equals(hi) ? lo + " - " + hi : lo; |
122 ? lo + " - " + hi | |
123 : lo; | |
124 } | 101 } |
125 | |
126 | 102 |
127 /** | 103 /** |
128 * This method might be used to test, if a parameter name is handled by this | 104 * This method might be used to test, if a parameter name is handled by this |
129 * state. | 105 * state. |
130 * | 106 * |
131 * @param parameterName The name of a parameter. | 107 * @param parameterName |
108 * The name of a parameter. | |
132 * | 109 * |
133 * @return true, if parameterName is one of <i>MAIN_CHANNEL</i> or | 110 * @return true, if parameterName is one of <i>MAIN_CHANNEL</i> or |
134 * <i>TOTAL_CHANNEL</i>. Otherwise false. | 111 * <i>TOTAL_CHANNEL</i>. Otherwise false. |
135 */ | 112 */ |
136 protected boolean testParameterName(String parameterName) { | 113 protected boolean testParameterName(final String parameterName) { |
137 if (parameterName == null || parameterName.length() == 0) { | 114 if (parameterName == null || parameterName.length() == 0) { |
138 return false; | 115 return false; |
139 } | 116 } else if (parameterName.equals(MAIN_CHANNEL)) { |
140 else if (parameterName.equals(MAIN_CHANNEL)) { | |
141 return true; | 117 return true; |
142 } | 118 } else if (parameterName.equals(TOTAL_CHANNEL)) { |
143 else if (parameterName.equals(TOTAL_CHANNEL)) { | |
144 return true; | 119 return true; |
145 } | 120 } else { |
146 else { | |
147 return false; | 121 return false; |
148 } | 122 } |
149 } | 123 } |
150 | |
151 | 124 |
152 /** | 125 /** |
153 * Returns all discharge zones for a given river. The river information is | 126 * Returns all discharge zones for a given river. The river information is |
154 * extracted from <i>artifact</i> using RiverUtils.getRiver(). | 127 * extracted from <i>artifact</i> using RiverUtils.getRiver(). |
155 * | 128 * |
156 * @param artifact Needs to be a D4EArtifact that stores a rivername. | 129 * @param artifact |
130 * Needs to be a D4EArtifact that stores a rivername. | |
157 * | 131 * |
158 * @return a list of DischargeZones. | 132 * @return a list of DischargeZones. |
159 * | 133 * |
160 * @throws IllegalArgumentException if no river information is provided by | 134 * @throws IllegalArgumentException |
161 * <i>artifact</i>. | 135 * if no river information is provided by |
136 * <i>artifact</i>. | |
162 */ | 137 */ |
163 protected List<DischargeZone> getDischargeZones(Artifact artifact) | 138 protected List<DischargeZone> getDischargeZones(final Artifact artifact) throws IllegalArgumentException { |
164 throws IllegalArgumentException | 139 final River river = RiverUtils.getRiver((D4EArtifact) artifact); |
165 { | |
166 River river = RiverUtils.getRiver((D4EArtifact) artifact); | |
167 | 140 |
168 if (river == null) { | 141 if (river == null) { |
169 throw new IllegalArgumentException("No river found"); | 142 throw new IllegalArgumentException("No river found"); |
170 } | 143 } |
171 | 144 |
172 List<DischargeZone> zones = DischargeZone.getDischargeZones(river); | 145 final List<DischargeZone> zones = DischargeZone.getDischargeZones(river); |
173 | 146 |
174 log.debug("Found " + zones.size() + " DischargeZones."); | 147 log.debug("Found " + zones.size() + " DischargeZones."); |
175 | 148 |
176 return zones; | 149 return zones; |
177 } | 150 } |