Mercurial > dive4elements > river
changeset 1678:03fbf1b30e72
Removed code duplication of guessWaterIncreasing()
flys-artifacts/trunk@2895 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 05 Oct 2011 13:16:44 +0000 |
parents | dd9dfe1e48fa |
children | 69929c471646 |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKms.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsImpl.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKms.java flys-artifacts/src/main/java/de/intevation/flys/exports/WDifferencesCurveGenerator.java flys-artifacts/src/main/java/de/intevation/flys/utils/DataUtil.java |
diffstat | 7 files changed, 58 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog Wed Oct 05 11:23:18 2011 +0000 +++ b/flys-artifacts/ChangeLog Wed Oct 05 13:16:44 2011 +0000 @@ -1,3 +1,21 @@ +2011-10-05 Sascha L. Teichmann <sascha.teichmann@intevation.de> + + Removed code duplication of guessWaterIncreasing() + + * src/main/java/de/intevation/flys/artifacts/model/WKms.java(allKms, allWs): + Added methods to fetch all kms and all ws. + + * src/main/java/de/intevation/flys/utils/DataUtil.java: Generalized to + get WKms as arguments. + + * src/main/java/de/intevation/flys/artifacts/model/WQKms.java, + src/main/java/de/intevation/flys/artifacts/model/WKmsImpl.java, + src/main/java/de/intevation/flys/artifacts/model/WQ.java: Implements + the extended WKms interface. + + * src/main/java/de/intevation/flys/exports/WDifferencesCurveGenerator.java: + Uses the generalized DataUtil.guessWaterIncreasing(). + 2011-10-05 Ingo Weinzierl <ingo@intevation.de> flys/issue347 (W-INFO / Wasserspiegellagenberechnung, Längsschnittdiagramm)
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKms.java Wed Oct 05 11:23:18 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKms.java Wed Oct 05 13:16:44 2011 +0000 @@ -1,5 +1,7 @@ package de.intevation.flys.artifacts.model; +import gnu.trove.TDoubleArrayList; + public interface WKms extends NamedObject { @@ -8,5 +10,9 @@ double getKm(int index); double getW(int index); + + TDoubleArrayList allKms(); + + TDoubleArrayList allWs(); } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsImpl.java Wed Oct 05 11:23:18 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WKmsImpl.java Wed Oct 05 13:16:44 2011 +0000 @@ -45,18 +45,31 @@ } + @Override public double getW(int index) { return ws.getQuick(index); } + @Override public double getKm(int index) { return kms.getQuick(index); } + @Override public int size() { return kms.size(); } + + @Override + public TDoubleArrayList allKms() { + return kms; + } + + @Override + public TDoubleArrayList allWs() { + return ws; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java Wed Oct 05 11:23:18 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQ.java Wed Oct 05 13:16:44 2011 +0000 @@ -1,9 +1,9 @@ package de.intevation.flys.artifacts.model; +import de.intevation.flys.utils.DataUtil; + import gnu.trove.TDoubleArrayList; -import java.util.Random; - import org.apache.log4j.Logger; public class WQ @@ -117,26 +117,7 @@ } public boolean guessWaterIncreasing(float factor) { - - int N = w.size(); - if (N < 2) return false; - - int samples = (int)(factor*N) + 1; - - int up = 0; - - Random rand = new Random(); - - for (int i = 0; i < samples; ++i) { - int pos2 = rand.nextInt(N-1) + 1; - if (pos2 == 0) continue; - int pos1 = rand.nextInt(pos2); - double w1 = w.getQuick(pos1); - double w2 = w.getQuick(pos2); - if (w2 > w1) ++up; - } - - return up > samples/2; + return DataUtil.guessWaterIncreasing(w, factor); } public int [] longestIncreasingWRangeIndices() {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKms.java Wed Oct 05 11:23:18 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WQKms.java Wed Oct 05 13:16:44 2011 +0000 @@ -90,6 +90,16 @@ return kms.getQuick(idx); } + @Override + public TDoubleArrayList allKms() { + return kms; + } + + @Override + public TDoubleArrayList allWs() { + return w; + } + public double[] getKms() { return kms.toNativeArray(); }
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/WDifferencesCurveGenerator.java Wed Oct 05 11:23:18 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WDifferencesCurveGenerator.java Wed Oct 05 13:16:44 2011 +0000 @@ -177,7 +177,7 @@ } addFirstAxisSeries(series); - if (DataUtil.guessWaterIncreasing(wkms)) { + if (DataUtil.guessWaterIncreasing(wkms.allWs())) { setInverted(true); } }
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/DataUtil.java Wed Oct 05 11:23:18 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/DataUtil.java Wed Oct 05 13:16:44 2011 +0000 @@ -2,18 +2,16 @@ import java.util.Random; -import de.intevation.flys.artifacts.model.WKms; +import gnu.trove.TDoubleArrayList; public class DataUtil { - // TODO: resolve duplicate in WQKms - public static boolean guessWaterIncreasing(WKms wkms) { - return guessWaterIncreasing(wkms, 0.05f); + public static boolean guessWaterIncreasing(TDoubleArrayList data) { + return guessWaterIncreasing(data, 0.05f); } - // TODO: resolve duplicate in WQKms - public static boolean guessWaterIncreasing(WKms wkms, float factor) { - int N = wkms.size(); + public static boolean guessWaterIncreasing(TDoubleArrayList data, float factor) { + int N = data.size(); if (N < 2) return false; int samples = (int)(factor*N) + 1; @@ -24,10 +22,9 @@ for (int i = 0; i < samples; ++i) { int pos2 = rand.nextInt(N-1) + 1; - if (pos2 == 0) continue; int pos1 = rand.nextInt(pos2); - double w1 = wkms.getW(pos1); - double w2 = wkms.getW(pos2); + double w1 = data.getQuick(pos1); + double w2 = data.getQuick(pos2); if (w2 > w1) ++up; }