comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/GaugeRange.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeRange.java@f987d25627aa
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.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 /**
13 * Gauge, km-range, main values.
14 */
15 public class GaugeRange
16 extends Range
17 {
18 private static Logger log = Logger.getLogger(GaugeRange.class);
19
20 private static final class Sector implements Serializable {
21
22 int sector;
23 double value;
24
25 Sector(int sector, double value) {
26 this.sector = sector;
27 this.value = value;
28 }
29 } // class Sector
30
31 protected String name;
32
33 protected int gaugeId;
34
35 /** Certain main value. */
36 protected Map<String, Double> mainValues;
37
38 protected List<Sector> sectors;
39
40
41 public GaugeRange() {
42 }
43
44
45 public GaugeRange(double start, double end, int gaugeId) {
46 this(start, end, null, gaugeId);
47 }
48
49
50 public GaugeRange(
51 double start,
52 double end,
53 String name,
54 int gaugeId
55 ) {
56 super(start, end);
57 this.name = name;
58 this.gaugeId = gaugeId;
59 mainValues = new HashMap<String, Double>();
60 sectors = new ArrayList<Sector>(3);
61 }
62
63
64 public void addMainValue(String label, Double value) {
65 int idx = label.indexOf('(');
66 if (idx >= 0) {
67 label = label.substring(0, idx);
68 }
69 mainValues.put(label, value);
70 }
71
72
73 protected Double getMainValue(String label) {
74 Double v = mainValues.get(label);
75 if (v == null) {
76 log.warn("Missing main value '"
77 + label + "' for gauge " + gaugeId);
78 }
79 return v;
80 }
81
82
83 public Map<String, Double> getMainValues() {
84 return mainValues;
85 }
86
87
88 public void buildClasses() {
89 Double mnq = getMainValue("MNQ");
90 Double mq = getMainValue("MQ");
91 Double mhq = getMainValue("MHQ");
92 Double hq5 = getMainValue("HQ5");
93
94 Double [][] pairs = {
95 { mnq, mq },
96 { mq, mhq },
97 { hq5, hq5 } };
98
99 for (int c = 0; c < pairs.length; ++c) {
100 Double [] pair = pairs[c];
101 if (pair[0] != null && pair[1] != null) {
102 double value = 0.5*(pair[0] + pair[1]);
103 sectors.add(new Sector(c, value));
104 }
105 }
106 }
107
108
109 public double getSectorBorder(int sector) {
110 for (Sector s: sectors) {
111 if (s.sector == sector) {
112 return s.value;
113 }
114 }
115 return Double.NaN;
116 }
117
118
119 public int classify(double value) {
120 for (Sector sector: sectors) {
121 if (value < sector.value) {
122 return sector.sector;
123 }
124 }
125 return sectors.size();
126 }
127
128
129 public String getName() {
130 return name;
131 }
132
133
134 public void setName(String name) {
135 this.name = name;
136 }
137
138
139 public String toString() {
140 StringBuilder sb = new StringBuilder("sectors: [");
141
142 for (int i = 0, S = sectors.size(); i < S; ++i) {
143 if (i > 0) sb.append(", ");
144 Sector s = sectors.get(i);
145 sb.append(s.sector).append(": ").append(s.value);;
146 }
147
148 sb.append("] mainvalues: ").append(mainValues);
149
150 return sb.toString();
151 }
152 }
153 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org