comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeRange.java @ 3141:3582e87e9171

FixA: Made GaugeRange a top level class. flys-artifacts/trunk@4749 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 21 Jun 2012 15:39:34 +0000
parents
children 05a7298c4f20
comparison
equal deleted inserted replaced
3140:3d456d8bca6e 3141:3582e87e9171
1 package de.intevation.flys.artifacts.model;
2
3 import java.io.Serializable;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.log4j.Logger;
11
12 public class GaugeRange
13 extends Range
14 {
15 private static Logger log = Logger.getLogger(GaugeRange.class);
16
17 private static final class Sector implements Serializable {
18
19 int sector;
20 double value;
21
22 Sector(int sector, double value) {
23 this.sector = sector;
24 this.value = value;
25 }
26 } // class Sector
27
28 protected int gaugeId;
29
30 protected Map<String, Double> mainValues;
31 protected List<Sector> sectors;
32
33 public GaugeRange() {
34 }
35
36 public GaugeRange(double start, double end, int gaugeId) {
37 super(start, end);
38 this.gaugeId = gaugeId;
39 mainValues = new HashMap<String, Double>();
40 sectors = new ArrayList<Sector>(3);
41 }
42
43 public void addMainValue(String label, Double value) {
44 int idx = label.indexOf('(');
45 if (idx >= 0) {
46 label = label.substring(0, idx);
47 }
48 mainValues.put(label, value);
49 }
50
51 protected Double getMainValue(String label) {
52 Double v = mainValues.get(label);
53 if (v == null) {
54 log.warn("Missing main value '"
55 + label + "' for gauge " + gaugeId);
56 }
57 return v;
58 }
59
60 public void buildClasses() {
61 Double mnq = getMainValue("MNQ");
62 Double mq = getMainValue("MQ");
63 Double mhq = getMainValue("MHQ");
64 Double hq5 = getMainValue("HQ5");
65
66 Double [][] pairs = {
67 { mnq, mq },
68 { mq, mhq },
69 { hq5, hq5 } };
70
71 for (int c = 0; c < pairs.length; ++c) {
72 Double [] pair = pairs[c];
73 if (pair[0] != null && pair[1] != null) {
74 double value = 0.5*(pair[0] + pair[1]);
75 sectors.add(new Sector(c, value));
76 }
77 }
78 }
79
80 public int classify(double value) {
81 for (Sector sector: sectors) {
82 if (value < sector.value) {
83 return sector.sector;
84 }
85 }
86 return sectors.size();
87 }
88 }
89 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org