Mercurial > dive4elements > river
diff artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java @ 9070:611a523fc42f
VegetationZoneAccessHelper, VegetationTablePanels verbessert
author | gernotbelger |
---|---|
date | Tue, 15 May 2018 18:04:36 +0200 |
parents | 5e38e2924c07 |
children | 850ce16034e9 |
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java Tue May 15 12:00:26 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/access/RangeAccess.java Tue May 15 18:04:36 2018 +0200 @@ -8,25 +8,23 @@ package org.dive4elements.river.artifacts.access; -import gnu.trove.TDoubleArrayList; - +import org.apache.commons.lang.math.DoubleRange; import org.apache.log4j.Logger; - import org.dive4elements.river.artifacts.D4EArtifact; import org.dive4elements.river.artifacts.WINFOArtifact; - +import org.dive4elements.river.utils.DoubleUtil; import org.dive4elements.river.utils.RiverUtils; -import org.dive4elements.river.utils.DoubleUtil; +import gnu.trove.TDoubleArrayList; /** For the moment, light-weight wrapper around RiverUtils. */ // TODO employ 'Caching' like other Accesses, remove usage of RiverUtils. -public class RangeAccess -extends RiverAccess -{ +public class RangeAccess extends RiverAccess { private static Logger log = Logger.getLogger(RangeAccess.class); - public static enum KM_MODE { RANGE, LOCATIONS, NONE }; + public static enum KM_MODE { + RANGE, LOCATIONS, NONE + }; /** The default step width between the start end end kilometer. */ public static final double DEFAULT_KM_STEPS = 0.1; @@ -44,32 +42,34 @@ public RangeAccess() { } - public RangeAccess(D4EArtifact artifact) { + public RangeAccess(final D4EArtifact artifact) { super(artifact); } - /** Evaluate the ld_mode data of artifact. */ public KM_MODE getKmRangeMode() { - if (mode != null) { - return mode; + if (this.mode != null) { + return this.mode; } - String modeData = getString("ld_mode"); + final String modeData = getString("ld_mode"); if (modeData == null || modeData.length() == 0) { - mode = KM_MODE.NONE; - } - else if (modeData.equals("distance")) { - mode = KM_MODE.RANGE; - } - else if (modeData.equals("locations")) { - mode = KM_MODE.LOCATIONS; - } - else { - mode = KM_MODE.NONE; + this.mode = KM_MODE.NONE; + } else if (modeData.equals("distance")) { + this.mode = KM_MODE.RANGE; + } else if (modeData.equals("locations")) { + this.mode = KM_MODE.LOCATIONS; + } else { + this.mode = KM_MODE.NONE; } - return mode; + return this.mode; + } + + public final DoubleRange getRange() { + final double from = getFrom(); + final double to = getTo(); + return new DoubleRange(from, to); } /** Check if the calculation mode is Range. */ @@ -82,39 +82,28 @@ * (from ld_locations data), null if not parameterized this way. */ public double[] getLocations() { - String locationStr = getString("ld_locations"); + final String locationStr = getString("ld_locations"); if (locationStr == null || locationStr.length() == 0) { if (getArtifact() instanceof WINFOArtifact) { - WINFOArtifact winfo = (WINFOArtifact) getArtifact(); - if (winfo.getReferenceStartKm() != null - && winfo.getReferenceEndKms() != null - ) { - return new double[] - { - winfo.getReferenceStartKm().doubleValue(), - winfo.getReferenceEndKms()[0] - }; - } - else if (winfo.getReferenceStartKm() != null) { - return new double[] - { - winfo.getReferenceStartKm().doubleValue(), - winfo.getReferenceStartKm().doubleValue() - }; + final WINFOArtifact winfo = (WINFOArtifact) getArtifact(); + if (winfo.getReferenceStartKm() != null && winfo.getReferenceEndKms() != null) { + return new double[] { winfo.getReferenceStartKm().doubleValue(), winfo.getReferenceEndKms()[0] }; + } else if (winfo.getReferenceStartKm() != null) { + return new double[] { winfo.getReferenceStartKm().doubleValue(), winfo.getReferenceStartKm().doubleValue() }; } } return null; } - String[] tmp = locationStr.split(" "); - TDoubleArrayList locations = new TDoubleArrayList(); + final String[] tmp = locationStr.split(" "); + final TDoubleArrayList locations = new TDoubleArrayList(); - for (String l: tmp) { + for (final String l : tmp) { try { locations.add(Double.parseDouble(l)); } - catch (NumberFormatException nfe) { + catch (final NumberFormatException nfe) { log.debug(nfe.getLocalizedMessage(), nfe); } } @@ -125,87 +114,89 @@ } public boolean hasFrom() { - return from != null || (from = getDouble("ld_from")) != null; + return this.from != null || (this.from = getDouble("ld_from")) != null; } public boolean hasTo() { - return to != null || (to = getDouble("ld_to")) != null; + return this.to != null || (this.to = getDouble("ld_to")) != null; } - /* If left_to_right is set to true this returns - * the smaller value of from and to. */ - public double getFrom(boolean left_to_right) { + /* + * If left_to_right is set to true this returns + * the smaller value of from and to. + */ + public double getFrom(final boolean left_to_right) { if (!left_to_right) { return getFrom(); } - double from = getFrom(); - double to = getTo(); + final double from = getFrom(); + final double to = getTo(); return from > to ? to : from; } /** Return ld_from data (in km). If not found, the min. */ public double getFrom() { - if (from == null) { - from = getDouble("ld_from"); + if (this.from == null) { + this.from = getDouble("ld_from"); } if (log.isDebugEnabled()) { - log.debug("from from data: '" + from + "'"); + log.debug("from from data: '" + this.from + "'"); } - if (from == null) { + if (this.from == null) { log.warn("No 'from' found. Assume min of river."); return getRiver().determineMinMaxDistance()[0]; } - return from.doubleValue(); + return this.from.doubleValue(); } - /* If left_to_right is set to true this returns - * the larger value of from and to. */ - public double getTo(boolean left_to_right) { + /* + * If left_to_right is set to true this returns + * the larger value of from and to. + */ + public double getTo(final boolean left_to_right) { if (!left_to_right) { return getTo(); } - double from = getFrom(); - double to = getTo(); + final double from = getFrom(); + final double to = getTo(); return from > to ? from : to; } /** Return ld_to data (in km), if not found, the max. */ public double getTo() { - if (to == null) { - to = getDouble("ld_to"); + if (this.to == null) { + this.to = getDouble("ld_to"); } if (log.isDebugEnabled()) { - log.debug("to from data: '" + to + "'"); + log.debug("to from data: '" + this.to + "'"); } - if (to == null) { + if (this.to == null) { log.warn("No 'to' found. Assume max of river."); return getRiver().determineMinMaxDistance()[1]; } - return to.doubleValue(); + return this.to.doubleValue(); } - /** Step width for calculation. */ public Double getStep() { - if (step == null) { - step = getDouble("ld_step"); + if (this.step == null) { + this.step = getDouble("ld_step"); } if (log.isDebugEnabled()) { - log.debug("step: '" + step + "'"); + log.debug("step: '" + this.step + "'"); } - return step; + return this.step; } - /** * Get min and max kilometer, independent of parametization * (ld_from/to vs ld_locations). @@ -213,37 +204,35 @@ public double[] getKmRange() { // TODO store kmRange in field. switch (getKmRangeMode()) { - case RANGE: { + case RANGE: { + return getKmFromTo(); + } + + case LOCATIONS: { + final double[] locs = getLocations(); + // if no locations, nPE. + if (locs == null) { + log.warn("no locations to get km range from."); + return new double[] { Double.NaN, Double.NaN }; + } + return new double[] { locs[0], locs[locs.length - 1] }; + } + + case NONE: { + final double[] locs = getLocations(); + if (locs != null) { + return new double[] { locs[0], locs[locs.length - 1] }; + } else { return getKmFromTo(); } - - case LOCATIONS: { - double[] locs = getLocations(); - // if no locations, nPE. - if (locs == null) { - log.warn("no locations to get km range from."); - return new double[] { Double.NaN, Double.NaN }; - } - return new double[] { locs[0], locs[locs.length-1] }; - } - - case NONE: { - double[] locs = getLocations(); - if (locs != null) { - return new double[] { locs[0], locs[locs.length-1] }; - } - else { - return getKmFromTo(); - } - } + } } return new double[] { Double.NaN, Double.NaN }; } - public double[] getKmFromTo() { - return RiverUtils.getKmFromTo(this.getArtifact()); + return RiverUtils.getKmFromTo(this.getArtifact()); } /**