sascha@3141: package de.intevation.flys.artifacts.model; sascha@3141: sascha@3141: import java.io.Serializable; sascha@3141: sascha@3141: import java.util.ArrayList; sascha@3141: import java.util.HashMap; sascha@3141: import java.util.List; sascha@3141: import java.util.Map; sascha@3141: sascha@3141: import org.apache.log4j.Logger; sascha@3141: felix@5335: /** felix@5335: * Gauge, km-range, main values. felix@5335: */ sascha@3141: public class GaugeRange sascha@3141: extends Range sascha@3141: { sascha@3141: private static Logger log = Logger.getLogger(GaugeRange.class); sascha@3141: sascha@3141: private static final class Sector implements Serializable { sascha@3141: sascha@3141: int sector; sascha@3141: double value; sascha@3141: sascha@3141: Sector(int sector, double value) { sascha@3141: this.sector = sector; sascha@3141: this.value = value; sascha@3141: } sascha@3141: } // class Sector sascha@3141: sascha@3421: protected String name; sascha@3421: sascha@3141: protected int gaugeId; sascha@3141: sascha@3141: protected Map mainValues; sascha@3141: protected List sectors; sascha@3141: sascha@3141: public GaugeRange() { sascha@3141: } sascha@3141: sascha@3141: public GaugeRange(double start, double end, int gaugeId) { sascha@3421: this(start, end, null, gaugeId); sascha@3421: } sascha@3421: sascha@3421: public GaugeRange( sascha@3421: double start, sascha@3421: double end, sascha@3421: String name, sascha@3421: int gaugeId sascha@3421: ) { sascha@3141: super(start, end); sascha@3421: this.name = name; sascha@3141: this.gaugeId = gaugeId; sascha@3141: mainValues = new HashMap(); sascha@3141: sectors = new ArrayList(3); sascha@3141: } sascha@3141: sascha@3141: public void addMainValue(String label, Double value) { sascha@3141: int idx = label.indexOf('('); sascha@3141: if (idx >= 0) { sascha@3141: label = label.substring(0, idx); sascha@3141: } sascha@3141: mainValues.put(label, value); sascha@3141: } sascha@3141: sascha@3141: protected Double getMainValue(String label) { sascha@3141: Double v = mainValues.get(label); sascha@3141: if (v == null) { sascha@3141: log.warn("Missing main value '" sascha@3141: + label + "' for gauge " + gaugeId); sascha@3141: } sascha@3141: return v; sascha@3141: } sascha@3141: sascha@3141: public void buildClasses() { sascha@3141: Double mnq = getMainValue("MNQ"); sascha@3141: Double mq = getMainValue("MQ"); sascha@3141: Double mhq = getMainValue("MHQ"); sascha@3141: Double hq5 = getMainValue("HQ5"); sascha@3141: sascha@3141: Double [][] pairs = { sascha@3141: { mnq, mq }, sascha@3141: { mq, mhq }, sascha@3141: { hq5, hq5 } }; sascha@3141: sascha@3141: for (int c = 0; c < pairs.length; ++c) { sascha@3141: Double [] pair = pairs[c]; sascha@3141: if (pair[0] != null && pair[1] != null) { sascha@3141: double value = 0.5*(pair[0] + pair[1]); sascha@3141: sectors.add(new Sector(c, value)); sascha@3141: } sascha@3141: } sascha@3141: } sascha@3141: sascha@3144: public double getSectorBorder(int sector) { sascha@3144: for (Sector s: sectors) { sascha@3144: if (s.sector == sector) { sascha@3144: return s.value; sascha@3144: } sascha@3144: } sascha@3144: return Double.NaN; sascha@3144: } sascha@3144: sascha@3141: public int classify(double value) { sascha@3141: for (Sector sector: sectors) { sascha@3141: if (value < sector.value) { sascha@3141: return sector.sector; sascha@3141: } sascha@3141: } sascha@3141: return sectors.size(); sascha@3141: } sascha@3151: sascha@3421: public String getName() { sascha@3421: return name; sascha@3421: } sascha@3421: sascha@3421: public void setName(String name) { sascha@3421: this.name = name; sascha@3421: } sascha@3421: sascha@3151: public String toString() { sascha@3151: StringBuilder sb = new StringBuilder("sectors: ["); sascha@3151: sascha@3151: for (int i = 0, S = sectors.size(); i < S; ++i) { sascha@3151: if (i > 0) sb.append(", "); sascha@3151: Sector s = sectors.get(i); sascha@3151: sb.append(s.sector).append(": ").append(s.value);; sascha@3151: } sascha@3151: sascha@3151: sb.append("] mainvalues: ").append(mainValues); sascha@3151: sascha@3151: return sb.toString(); sascha@3151: } sascha@3141: } sascha@3141: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :