comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java @ 8036:17542d100e75

Throw out old visitor model and use grain fraction filters instead. Fixed neighborhood wirinng of measument stations.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 16 Jul 2014 11:05:59 +0200
parents f2dc7992b8a3
children aa622bddfdac
comparison
equal deleted inserted replaced
8035:f2dc7992b8a3 8036:17542d100e75
8 package org.dive4elements.river.artifacts.model.minfo; 8 package org.dive4elements.river.artifacts.model.minfo;
9 9
10 import java.io.Serializable; 10 import java.io.Serializable;
11 import java.util.ArrayList; 11 import java.util.ArrayList;
12 import java.util.Collection; 12 import java.util.Collection;
13 import java.util.Collections;
13 import java.util.Date; 14 import java.util.Date;
14 import java.util.List; 15 import java.util.List;
15 import java.util.TreeMap; 16 import java.util.TreeMap;
16 17
17 import org.dive4elements.river.utils.EpsilonComparator; 18 import org.dive4elements.river.utils.EpsilonComparator;
36 if ("susp_sand_bed".equals(name)) return GF_SUSP_SAND_BED; 37 if ("susp_sand_bed".equals(name)) return GF_SUSP_SAND_BED;
37 if ("suspended_sediment".equals(name)) return GF_SUSP_SEDIMENT; 38 if ("suspended_sediment".equals(name)) return GF_SUSP_SEDIMENT;
38 return -1; 39 return -1;
39 } 40 }
40 41
42 public interface Visitor {
43 void visit(List<Station> stations);
44 }
45
41 public static class Value implements Serializable { 46 public static class Value implements Serializable {
47
48 public interface Filter {
49 boolean accept(Value value);
50 }
42 51
43 private double value; 52 private double value;
44 53
45 private Load load; 54 private Load load;
46 55
105 public static class Station implements Serializable { 114 public static class Station implements Serializable {
106 115
107 public static final int BED_LOAD = 0; 116 public static final int BED_LOAD = 0;
108 public static final int SUSPENDED = 1; 117 public static final int SUSPENDED = 1;
109 118
110 public interface Visitor {
111 boolean accept(Station station);
112 boolean accept(int grainFraction);
113 boolean visit(Value value, int grainFraction);
114 }
115
116 private double station; 119 private double station;
117 120
118 private int type; 121 private int type;
119 122
120 private List<List<Value>> grainFractions; 123 private List<List<Value>> grainFractions;
127 } 130 }
128 131
129 public Station(int type, double station) { 132 public Station(int type, double station) {
130 grainFractions = new ArrayList<List<Value>>(GF_MAX+1); 133 grainFractions = new ArrayList<List<Value>>(GF_MAX+1);
131 for (int i = 0; i < GF_MAX+1; ++i) { 134 for (int i = 0; i < GF_MAX+1; ++i) {
132 grainFractions.add(new ArrayList<Value>()); 135 grainFractions.add(null);
133 } 136 }
134 this.type = type; 137 this.type = type;
135 this.station = station; 138 this.station = station;
136 } 139 }
137 140
157 160
158 public Station getPrev() { 161 public Station getPrev() {
159 return prev; 162 return prev;
160 } 163 }
161 164
165 public Station getNext(boolean isKMUp) {
166 return isKMUp ? getNext() : getPrev();
167 }
168
169 public Station getPrev(boolean isKMUp) {
170 return isKMUp ? getPrev() : getNext();
171 }
172
162 public void addValue(int grainFraction, Value value) { 173 public void addValue(int grainFraction, Value value) {
163 grainFractions.get(grainFraction).add(value); 174 List<Value> values = grainFractions.get(grainFraction);
175 if (values == null) {
176 values = new ArrayList<Value>();
177 grainFractions.set(grainFraction, values);
178 }
179 values.add(value);
164 } 180 }
165 181
166 public boolean hasGrainFraction(String grainFraction) { 182 public boolean hasGrainFraction(String grainFraction) {
167 return hasGrainFraction(grainFractionIndex(grainFraction)); 183 return hasGrainFraction(grainFractionIndex(grainFraction));
168 } 184 }
169 185
170 public boolean hasGrainFraction(int grainFraction) { 186 public boolean hasGrainFraction(int grainFraction) {
171 List<Value> values = grainFractions.get(grainFraction); 187 List<Value> values = grainFractions.get(grainFraction);
172 return !values.isEmpty(); 188 return values != null && !values.isEmpty();
173 } 189 }
174 190
175 public void visit(Visitor visitor) { 191 public List<Value> filterGrainFraction(int grainFraction, Value.Filter filter) {
176 if (!visitor.accept(this)) { 192 List<Value> values = grainFractions.get(grainFraction);
177 return; 193 if (values == null || values.isEmpty()) {
178 } 194 return Collections.<Value>emptyList();
179 195 }
180 for (int i = 0; i < GF_MAX+1; ++i) { 196 List<Value> result = new ArrayList<Value>(values.size());
181 List<Value> values = grainFractions.get(i); 197 for (Value value: values) {
182 if (values.isEmpty() || !visitor.accept(i)) { 198 if (filter.accept(value)) {
183 continue; 199 result.add(value);
184 } 200 }
185 for (Value value: values) { 201 }
186 if (!visitor.visit(value, i)) { 202 return result;
187 break;
188 }
189 }
190 }
191 }
192
193 public boolean inside(double a, double b) {
194 return station >= a && station <= b;
195 } 203 }
196 204
197 public double findValueByLoadId(int id) { 205 public double findValueByLoadId(int id) {
198 for (List<Value> values: grainFractions) { 206 for (List<Value> values: grainFractions) {
199 double value = findValueByLoadId(values, id); 207 double value = findValueByLoadId(values, id);
206 214
207 private static final double findValueByLoadId( 215 private static final double findValueByLoadId(
208 List<Value> values, 216 List<Value> values,
209 int id 217 int id
210 ) { 218 ) {
219 if (values == null) {
220 return Double.NaN;
221 }
211 // List is ordered by station id -> binary search. 222 // List is ordered by station id -> binary search.
212 int lo = 0, hi = values.size()-1; 223 int lo = 0, hi = values.size()-1;
213 while (lo <= hi) { 224 while (lo <= hi) {
214 int mid = (lo + hi)/2; 225 int mid = (lo + hi)/2;
215 Value v = values.get(mid); 226 Value v = values.get(mid);
219 else return v.getValue(); 230 else return v.getValue();
220 } 231 }
221 232
222 return Double.NaN; 233 return Double.NaN;
223 } 234 }
235
236 public static Station firstOfType(List<Station> stations, int type) {
237 for (Station station: stations) {
238 if (station.getType() == type) {
239 return station;
240 }
241 }
242 return null;
243 }
224 } // class Station 244 } // class Station
225 245
226 246
227 private List<List<Station>> stations; 247 private List<List<Station>> stations;
228 248
248 this.stations = new ArrayList<List<Station>>(same.values()); 268 this.stations = new ArrayList<List<Station>>(same.values());
249 wireNeighbors(); 269 wireNeighbors();
250 } 270 }
251 271
252 private void wireNeighbors() { 272 private void wireNeighbors() {
253 for (int i = 0, N = stations.size()-1; i < N; ++i) { 273 for (int i = 0, N = stations.size(); i < N-1; ++i) {
254 for (Station current: stations.get(i)) { 274 for (Station current: stations.get(i)) {
255 int type = current.getType(); 275 int type = current.getType();
256 NEXT: for (int j = i+1; j < N; ++j) { 276 NEXT: for (int j = i+1; j < N; ++j) {
257 for (Station next: stations.get(j)) { 277 for (Station next: stations.get(j)) {
258 if (next.getType() == type) { 278 if (next.getType() == type) {
262 } 282 }
263 } 283 }
264 } 284 }
265 } 285 }
266 } 286 }
267 }
268
269 public interface Visitor {
270 void visit(List<Station> stations);
271 } 287 }
272 288
273 private void recursiveFindStations( 289 private void recursiveFindStations(
274 double a, double b, 290 double a, double b,
275 int lo, int hi, 291 int lo, int hi,

http://dive4elements.wald.intevation.org