[PATCH] Remove indirection via RiverUtils to obtain reference gauge and reduce casts

Wald Commits scm-commit at wald.intevation.org
Fri Jun 26 19:33:23 CEST 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1435339997 -7200
# Node ID 9483a5bf4219207d38c867be1f468b35e4a11392
# Parent  2faf6cef6f5f1a415119d1690ec351da77bb048e
Remove indirection via RiverUtils to obtain reference gauge and reduce casts.

diff -r 2faf6cef6f5f -r 9483a5bf4219 artifacts/src/main/java/org/dive4elements/river/artifacts/states/WQSelect.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/WQSelect.java	Fri Jun 26 18:11:27 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/WQSelect.java	Fri Jun 26 19:33:17 2015 +0200
@@ -180,9 +180,9 @@
         NumberFormat nf = NumberFormat.getInstance(
             Resources.getLocale(cc.getMeta()));
 
-        Gauge gauge = RiverUtils.getGauge(winfo);
-
-        boolean debug = log.isDebugEnabled();
+        RangeAccess rangeAccess = new RangeAccess(winfo);
+        Gauge gauge = rangeAccess.getRiver().determineRefGauge(
+            rangeAccess.getKmRange(), rangeAccess.isRange());
 
         StringBuilder label = new StringBuilder();
 
@@ -195,9 +195,7 @@
 
                 if (mv != null && mv.length() > 0) {
                     tmp = mv + ": " + tmp;
-                    if (debug) {
-                        log.debug("Add main value: '" + mv + "'");
-                    }
+                    log.debug("Add main value: '" + mv + "'");
                 }
                 if (label.length() > 0) {
                     label.append(';');
@@ -250,12 +248,14 @@
         XMLUtils.ElementCreator cr,
         Artifact    artifact,
         String      name,
-        CallContext context)
-    {
-        double[] minmaxW     = determineMinMaxW(artifact);
-        double[] minmaxWFree = determineMinMaxWFree(artifact);
-        double[] minmaxQ     = determineMinMaxQAtGauge(artifact);
-        double[] minmaxQFree = determineMinMaxQ(artifact);
+        CallContext context
+    ){
+        WINFOArtifact winfo = (WINFOArtifact) artifact;
+
+        double[] minmaxW     = determineMinMaxW(winfo);
+        double[] minmaxWFree = determineMinMaxWFree(winfo);
+        double[] minmaxQ     = determineMinMaxQAtGauge(winfo);
+        double[] minmaxQFree = determineMinMaxQ(winfo);
 
         if (name.equals("wq_from")) {
             Element minW = createItem(cr, new String[] {
@@ -400,10 +400,13 @@
      *
      * @return the min and max W values for the current gauge.
      */
-    protected double[] determineMinMaxW(Artifact artifact) {
+    protected double[] determineMinMaxW(WINFOArtifact winfo) {
         log.debug("WQSelect.determineCurrentGauge");
 
-        Gauge    gauge   = RiverUtils.getGauge((WINFOArtifact) artifact);
+        RangeAccess rangeAccess = new RangeAccess(winfo);
+        Gauge gauge = rangeAccess.getRiver().determineRefGauge(
+            rangeAccess.getKmRange(), rangeAccess.isRange());
+
         double[] minmaxW = gauge != null ? gauge.determineMinMaxW() : null;
 
         double minW = minmaxW != null ? minmaxW[0] : Double.MIN_VALUE;
@@ -422,10 +425,9 @@
      *
      * @return the min and max W values.
      */
-    protected double[] determineMinMaxWFree(Artifact artifact) {
+    protected double[] determineMinMaxWFree(WINFOArtifact winfo) {
         log.debug("WQSelect.determineMinMaxWFree");
 
-        WINFOArtifact winfo = (WINFOArtifact) artifact;
         WstValueTable valueTable = WstValueTableFactory.getTable(
                 RiverUtils.getRiver(winfo));
 
@@ -457,13 +459,14 @@
      *
      * @return the min and max Q values for the current gauge.
      */
-    protected double[] determineMinMaxQAtGauge(Artifact artifact) {
+    protected double[] determineMinMaxQAtGauge(WINFOArtifact winfo) {
         log.debug("WQSelect.determineMinMaxQAtGauge");
 
-        WINFOArtifact flysArtifact = (WINFOArtifact) artifact;
+        RangeAccess rangeAccess = new RangeAccess(winfo);
+        River river = rangeAccess.getRiver();
+        Gauge gauge = river.determineRefGauge(
+            rangeAccess.getKmRange(), rangeAccess.isRange());
 
-        River river = RiverUtils.getRiver(flysArtifact);
-        Gauge gauge = RiverUtils.getGauge(flysArtifact);
         Wst   wst   = WstFactory.getWst(river);
 
         double[] minmaxQ = gauge != null
@@ -485,10 +488,9 @@
      *
      * @return the min and max Q values for the current kilometer range.
      */
-    protected double[] determineMinMaxQ(Artifact artifact) {
+    protected double[] determineMinMaxQ(WINFOArtifact winfo) {
         log.debug("WQSelect.determineMinMaxQ");
 
-        WINFOArtifact winfo = (WINFOArtifact) artifact;
         WstValueTable valueTable = WstValueTableFactory.getTable(
                 RiverUtils.getRiver(winfo));
 
@@ -535,10 +537,10 @@
 
 
         if (!isRange) {
-            return validateSingle(artifact);
+            return validateSingle(flys);
         }
         else {
-            return validateRange(artifact);
+            return validateRange(flys);
         }
     }
 
@@ -565,13 +567,12 @@
     }
 
 
-    protected boolean validateSingle(Artifact artifact)
+    protected boolean validateSingle(WINFOArtifact artifact)
     throws    IllegalArgumentException
     {
         log.debug("WQSelect.validateSingle");
 
-        WINFOArtifact flys = (WINFOArtifact) artifact;
-        StateData    data = getData(flys, WQ_SINGLE);
+        StateData    data = getData(artifact, WQ_SINGLE);
 
         String tmp = data != null ? (String) data.getValue() : null;
 
@@ -593,7 +594,7 @@
 
         all.sort();
 
-        RiverUtils.WQ_MODE mode = RiverUtils.getWQMode(flys);
+        RiverUtils.WQ_MODE mode = RiverUtils.getWQMode(artifact);
 
         log.debug("WQ Mode: " + mode);
 
@@ -622,21 +623,20 @@
     }
 
 
-    protected boolean validateRange(Artifact artifact)
+    protected boolean validateRange(WINFOArtifact artifact)
     throws    IllegalArgumentException
     {
         log.debug("WQSelect.validateRange");
 
-        WINFOArtifact     flys = (WINFOArtifact) artifact;
-        RiverUtils.WQ_MODE mode = RiverUtils.getWQMode(flys);
+        RiverUtils.WQ_MODE mode = RiverUtils.getWQMode(artifact);
 
         if (mode == null) {
             throw new IllegalArgumentException("error_feed_invalid_wq_mode");
         }
 
-        StateData dFrom = flys.getData(WQ_FROM);
-        StateData dTo   = flys.getData(WQ_TO);
-        StateData dStep = flys.getData(WQ_STEP);
+        StateData dFrom = artifact.getData(WQ_FROM);
+        StateData dTo   = artifact.getData(WQ_TO);
+        StateData dStep = artifact.getData(WQ_STEP);
 
         String fromStr = dFrom != null ? (String) dFrom.getValue() : null;
         String toStr   = dTo != null ? (String) dTo.getValue() : null;
@@ -685,7 +685,7 @@
      * @return true, if everything was fine, otherwise an exception is thrown.
      */
     protected boolean validateGaugeW(
-        Artifact    artifact,
+        WINFOArtifact    artifact,
         double from,
         double to,
         double step)
@@ -711,7 +711,7 @@
      * @return true, if everything was fine, otherwise an exception is thrown.
      */
     protected boolean validateGaugeQ(
-        Artifact artifact,
+        WINFOArtifact artifact,
         double   from,
         double   to,
         double   step)
@@ -737,7 +737,7 @@
      * @return true, if everything was fine, otherwise an exception is thrown.
      */
     protected boolean validateFreeQ(
-        Artifact artifact,
+        WINFOArtifact artifact,
         double   from,
         double   to,
         double   step)
@@ -763,7 +763,7 @@
      * @return true, if everything was fine, otherwise an exception is thrown.
      */
     protected boolean validateFreeW(
-        Artifact artifact,
+        WINFOArtifact artifact,
         double   from,
         double   to,
         double   step)
diff -r 2faf6cef6f5f -r 9483a5bf4219 artifacts/src/main/java/org/dive4elements/river/utils/RiverUtils.java
--- a/artifacts/src/main/java/org/dive4elements/river/utils/RiverUtils.java	Fri Jun 26 18:11:27 2015 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/RiverUtils.java	Fri Jun 26 19:33:17 2015 +0200
@@ -441,7 +441,7 @@
      * @param flys the artifact in question.
      * @return Reference / first gauge of locations of river of artifact.
      */
-    public static Gauge getGauge(D4EArtifact flys) {
+    private static Gauge getGauge(D4EArtifact flys) {
         River river = getRiver(flys);
         RangeAccess rangeAccess = new RangeAccess(flys);
         double[] dist = rangeAccess.getKmRange();


More information about the Dive4Elements-commits mailing list