Mercurial > dive4elements > river
changeset 8755:30b1ddadf275
(issue1801) Unify reference gauge finding code
The basic way as described in the method comment of the
determineRefGauge method is now used in the WINFOArtifact,
MainValuesService and RiverUtils.getGauge method.
RiverUtils.getGauge previously just returned the first
gauge found. While this is now a behavior change I believe
that it is always more correct then the undeterministic
behavior of the previous implmenentation.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Wed, 24 Jun 2015 14:07:26 +0200 |
parents | 574f8b80799f |
children | 26dedebbe39f |
files | artifacts/src/main/java/org/dive4elements/river/artifacts/WINFOArtifact.java artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java artifacts/src/main/java/org/dive4elements/river/artifacts/services/MainValuesService.java artifacts/src/main/java/org/dive4elements/river/utils/RiverUtils.java backend/src/main/java/org/dive4elements/river/model/River.java |
diffstat | 5 files changed, 36 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/WINFOArtifact.java Fri Jun 19 17:42:23 2015 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/WINFOArtifact.java Wed Jun 24 14:07:26 2015 +0200 @@ -258,7 +258,7 @@ log.debug("'free' calculation (km " + refKm + ")"); } else { - Gauge gauge = determineRefGauge(range, river); + Gauge gauge = river.determineRefGauge(range, rangeAccess.isRange()); if (gauge == null) { return error( @@ -730,7 +730,7 @@ log.debug("range: " + Arrays.toString(range)); } - Gauge g = determineRefGauge(range, rangeAccess.getRiver()); + Gauge g = rangeAccess.getRiver().determineRefGauge(range, rangeAccess.isRange()); if (g == null) { log.warn("no gauge found for km: " + range[0]); return null; @@ -782,36 +782,15 @@ /** - * Determine reference gauge dependent on direction of calculation - * for a range calculation, otherwise dependent on flow direction. - */ - public Gauge determineRefGauge(double[] range, River river) { - if (isRange()) { - return river.determineGaugeByPosition( - range[0], - range[0] > range[1]); - } - else { - return river.determineGaugeByPosition(range[0]); - } - } - - /** * Determines the selected mode of distance/range input. * + * Compatibility wrapper around RangeAccess. + * * @return true, if the range mode is selected otherwise false. */ public boolean isRange() { - StateData mode = getData("ld_mode"); - - if (mode == null) { - log.warn("No mode location/range chosen. Defaults to range."); - return true; - } - - String value = (String) mode.getValue(); - - return value.equals("distance"); + RangeAccess rangeAccess = new RangeAccess(this); + return rangeAccess.isRange(); }
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java Fri Jun 19 17:42:23 2015 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java Wed Jun 24 14:07:26 2015 +0200 @@ -72,6 +72,11 @@ return mode; } + /** Check if the calculation mode is Range. */ + public boolean isRange() { + return getKmRangeMode() == KM_MODE.RANGE; + } + /** * Return sorted array of locations at which stuff was calculated * (from ld_locations data), null if not parameterized this way.
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/services/MainValuesService.java Fri Jun 19 17:42:23 2015 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/services/MainValuesService.java Wed Jun 24 14:07:26 2015 +0200 @@ -32,6 +32,8 @@ import org.dive4elements.river.artifacts.model.RiverFactory; +import static org.dive4elements.river.backend.utils.EpsilonComparator.CMP; + /** * This service returns the main values of a river's gauge based on the start @@ -84,7 +86,8 @@ } double[] minmax = getRequestedStartEnd(data, river); - Gauge gauge = river.determineGauge(minmax[0], minmax[1]); + Gauge gauge = river.determineRefGauge(minmax, + CMP.compare(minmax[0], minmax[1]) != 0); if (gauge == null) { return error("no gauge found.");
--- a/artifacts/src/main/java/org/dive4elements/river/utils/RiverUtils.java Fri Jun 19 17:42:23 2015 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/utils/RiverUtils.java Wed Jun 24 14:07:26 2015 +0200 @@ -434,37 +434,19 @@ /** * Return the (first) Gauge corresponding to the given location(s) of * the artifact. + * + * This method is left for compatibility. Use river.determineRefGauge() + * directly in new code. + * * @param flys the artifact in question. - * @return (First) gauge of locations of river of artifact. + * @return Reference / first gauge of locations of river of artifact. */ public static Gauge getGauge(D4EArtifact flys) { River river = getRiver(flys); - - if (river == null) { - log.debug("no river found"); - return null; - } - RangeAccess rangeAccess = new RangeAccess(flys); double[] dist = rangeAccess.getKmRange(); - if (dist == null) { - log.debug("no range found"); - return null; - } - - if (log.isDebugEnabled()) { - log.debug("Determine gauge for:"); - log.debug("... river: " + river.getName()); - log.debug("... distance: " + dist[0] + " - " + dist[1]); - } - - Gauge gauge = river.determineGauge(dist[0], dist[1]); - - String name = gauge != null ? gauge.getName() : "'n/a"; - log.debug("Found gauge: " + name); - - return gauge; + return river.determineRefGauge(dist, rangeAccess.isRange()); }
--- a/backend/src/main/java/org/dive4elements/river/model/River.java Fri Jun 19 17:42:23 2015 +0200 +++ b/backend/src/main/java/org/dive4elements/river/model/River.java Wed Jun 24 14:07:26 2015 +0200 @@ -390,6 +390,21 @@ } /** + * Determine reference gauge dependent on direction of calculation + * for a range calculation, otherwise dependent on flow direction. + */ + public Gauge determineRefGauge(double[] range, boolean isRange) { + if (isRange) { + return determineGaugeByPosition( + range[0], + range[0] > range[1]); + } + else { + return determineGaugeByPosition(range[0]); + } + } + + /** * Returns the min and max distance of this river. The first position in the * resulting array contains the min distance, the second position the max * distance.