Mercurial > dive4elements > river
changeset 8063:41d9014bfa02
Sediment density: Got rid of stupid extra year tracking.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Wed, 30 Jul 2014 18:34:48 +0200 (2014-07-30) |
parents | 8489565ff563 |
children | 50102c41151e |
files | artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java |
diffstat | 1 files changed, 36 insertions(+), 43 deletions(-) [+] |
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java Wed Jul 30 16:50:26 2014 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensity.java Wed Jul 30 18:34:48 2014 +0200 @@ -9,17 +9,16 @@ package org.dive4elements.river.artifacts.model.minfo; import java.io.Serializable; - import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.apache.log4j.Logger; - import org.dive4elements.artifacts.common.utils.Config; @@ -37,8 +36,7 @@ public static final double SEDIMNET_DENSITY_FACTOR = getSedimentDensityFactor(); - private Map<Integer, List<SedimentDensityValue>> densities; - private List<Integer> years; + private TreeMap<Integer, List<SedimentDensityValue>> densities; /** Figures out the sediment density factor from global config. */ @@ -64,18 +62,13 @@ } public SedimentDensity() { - this.densities = new HashMap<Integer, List<SedimentDensityValue>>(); - this.years = new ArrayList<Integer>(); + densities = new TreeMap<Integer, List<SedimentDensityValue>>(); } public Map<Integer, List<SedimentDensityValue>> getDensities() { return densities; } - public void setDensities(Map<Integer, List<SedimentDensityValue>> densities) { - this.densities = densities; - } - public void addDensity(double km, double density, int year) { logger.debug("adding " + year); @@ -90,22 +83,6 @@ } list.add(new SedimentDensityValue(km, density, year)); - - // XXX: Why do we store a redundant list of years, too? - // Years are the keys of densities so they can be easily - // accessed by their key set! - if (!years.contains(key)) { - logger.debug("new year"); - years.add(key); - } - } - - public List<Integer> getYears() { - return years; - } - - public void setYears(List<Integer> years) { - this.years = years; } /** @@ -114,21 +91,32 @@ * If no measurement was found 1.8 is returned. */ public double getDensity(double km, int year) { - Collections.sort(this.years); - if (this.years.size() == 1 && years.get(0) <= year) { - logger.debug("get density from year " + year + " at km " + km); - return getDensityAtKm(densities.get(years.get(0)), km); + + if (densities.isEmpty()) { + return SEDIMNET_DENSITY_FACTOR; } - if (this.years.size() > 1) { - for (int i = 0, I = years.size()-1; i < I; i++) { - int y1 = years.get(i); - int y2 = years.get(i + 1); - if (year >= y1 && year < y2) { - return getDensityAtKm(densities.get(y1), km); - } - else if (year >= y2 && i == years.size() -1) { - return getDensityAtKm(densities.get(y2), km); - } + + if (densities.size() == 1) { + Map.Entry<Integer, List<SedimentDensityValue>> entry = densities.firstEntry(); + return entry.getKey() <= year + ? getDensityAtKm(entry.getValue(), km) + : SEDIMNET_DENSITY_FACTOR; + } + Iterator<Map.Entry<Integer, List<SedimentDensityValue>>> iter = + densities.entrySet().iterator(); + + Map.Entry<Integer, List<SedimentDensityValue>> last = iter.next(); + + while (iter.hasNext()) { + Map.Entry<Integer, List<SedimentDensityValue>> current = iter.next(); + last = current; + int y1 = last.getKey(); + int y2 = current.getKey(); + if (year >= y1 && year < y2) { + return getDensityAtKm(last.getValue(), km); + } + if (year >= y2 && !iter.hasNext()) { + return getDensityAtKm(current.getValue(), km); } } return SEDIMNET_DENSITY_FACTOR; @@ -138,8 +126,9 @@ public double[][] getAllDensities() { TreeMap<Double, Double> map = new TreeMap<Double,Double>(); - for (int year: years) { - for (SedimentDensityValue sdv: densities.get(year)) { + // XXX: This looks stupid. + for (List<SedimentDensityValue> sdvs: densities.values()) { + for (SedimentDensityValue sdv: sdvs) { map.put(sdv.getKm(), sdv.getDensity()); } } @@ -158,10 +147,14 @@ public double[][] getDensities(int year) { TreeMap<Double, Double> map = new TreeMap<Double,Double>(); + List<SedimentDensityValue> list = densities.get(year); + if (list == null) { + return new double[2][0]; + } for (SedimentDensityValue sdv: densities.get(year)) { map.put(sdv.getKm(), sdv.getDensity()); } - double[][] points = new double[2][map.keySet().size()]; + double[][] points = new double[2][map.size()]; int i = 0; for (Map.Entry<Double, Double> kmDens: map.entrySet()) { points[0][i] = kmDens.getKey();