[PATCH 1 of 6] Changed to list of DateRange, added methods in DateRange to support year ranges

Wald Commits scm-commit at wald.intevation.org
Tue Jun 19 14:25:08 CEST 2018


# HG changeset patch
# User mschaefer
# Date 1529410532 -7200
# Node ID 9b4f5e61c02e58448e905ba5f9d3df6026d1c185
# Parent  cd24db77f0446d7db936b12db2a61d343a1b5693
Changed to list of DateRange, added methods in DateRange to support year ranges

diff -r cd24db77f044 -r 9b4f5e61c02e artifacts/src/main/java/org/dive4elements/river/artifacts/common/EpochYearAccessHelper.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/common/EpochYearAccessHelper.java	Thu Jun 14 17:51:30 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/common/EpochYearAccessHelper.java	Tue Jun 19 14:15:32 2018 +0200
@@ -9,10 +9,15 @@
  */
 package org.dive4elements.river.artifacts.common;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.log4j.Logger;
 import org.dive4elements.artifacts.Artifact;
 import org.dive4elements.river.artifacts.D4EArtifact;
 import org.dive4elements.river.artifacts.access.Access;
+import org.dive4elements.river.artifacts.model.DateRange;
+import org.dive4elements.river.backend.utils.DateUtil;
 
 import gnu.trove.TIntArrayList;
 
@@ -68,8 +73,8 @@
         return null;
     }
 
-    public int[][] getEpochs(final String epochSelectedValue, final String epochKey) {
-        int epochs[][] = null;
+    public List<DateRange> getEpochs(final String epochSelectedValue, final String epochKey) {
+        final List<DateRange> epochs = new ArrayList<>();
         // if (epochs != null) {
         // return epochs;
         // }
@@ -79,23 +84,15 @@
         }
 
         final String data = getString(epochKey);
-
         if (data == null) {
             log.warn("No 'epochs' parameter specified!");
             return null;
         }
 
-        final String[] parts = data.split(";");
-
-        epochs = new int[parts.length][];
-
-        for (int i = 0; i < parts.length; i++) {
-            final String[] values = parts[i].split(",");
-            final TIntArrayList ints = new TIntArrayList();
+        for (final String part : data.split(";")) {
+            final String[] values = part.split(",");
             try {
-                ints.add(Integer.parseInt(values[0]));
-                ints.add(Integer.parseInt(values[1]));
-                epochs[i] = ints.toNativeArray();
+                epochs.add(new DateRange(DateUtil.getStartDateFromYear(Integer.parseInt(values[0])), DateUtil.getEndDateFromYear(Integer.parseInt(values[1]))));
             }
             catch (final NumberFormatException nfe) {
                 log.warn("Cannot parse int from string: '" + values + "'");
diff -r cd24db77f044 -r 9b4f5e61c02e artifacts/src/main/java/org/dive4elements/river/artifacts/model/DateRange.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/DateRange.java	Thu Jun 14 17:51:30 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/DateRange.java	Tue Jun 19 14:15:32 2018 +0200
@@ -11,6 +11,8 @@
 import java.io.Serializable;
 import java.util.Date;
 
+import org.dive4elements.artifacts.common.utils.DateUtils;
+
 public class DateRange
 implements   Serializable
 {
@@ -19,28 +21,28 @@
     protected Date from;
     protected Date to;
 
-    public DateRange(Date from, Date to) {
+    public DateRange(final Date from, final Date to) {
         this.from = from;
         this.to   = to;
     }
 
     public Date getFrom() {
-        return from;
+        return this.from;
     }
 
-    public void setFrom(Date from) {
+    public void setFrom(final Date from) {
         this.from = from;
     }
 
     public Date getTo() {
-        return to;
+        return this.to;
     }
 
-    public void setTo(Date to) {
+    public void setTo(final Date to) {
         this.to = to;
     }
 
-    private static final boolean equalDates(Date a, Date b) {
+    private static final boolean equalDates(final Date a, final Date b) {
         if (a == null && b != null) return false;
         if (a != null && b == null) return false;
         if (a == null) return true;
@@ -48,12 +50,20 @@
     }
 
     @Override
-    public boolean equals(Object other) {
+    public boolean equals(final Object other) {
         if (!(other instanceof DateRange)) {
             return false;
         }
-        DateRange o = (DateRange)other;
-        return equalDates(from, o.from) && equalDates(to, o.to);
+        final DateRange o = (DateRange)other;
+        return equalDates(this.from, o.from) && equalDates(this.to, o.to);
+    }
+
+    public int getFromYear() {
+        return DateUtils.getYearFromDate(this.from);
+    }
+
+    public int getToYear() {
+        return DateUtils.getYearFromDate(this.to);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r cd24db77f044 -r 9b4f5e61c02e artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/CollisionAccess.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/CollisionAccess.java	Thu Jun 14 17:51:30 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/CollisionAccess.java	Tue Jun 19 14:15:32 2018 +0200
@@ -10,8 +10,12 @@
 
 package org.dive4elements.river.artifacts.sinfo.collision;
 
+import java.util.Arrays;
+import java.util.List;
+
 import org.dive4elements.river.artifacts.access.RangeAccess;
 import org.dive4elements.river.artifacts.common.EpochYearAccessHelper;
+import org.dive4elements.river.artifacts.model.DateRange;
 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact;
 import org.dive4elements.river.artifacts.sinfo.SinfoCalcMode;
 
@@ -48,8 +52,26 @@
         return this.helper.getYears("state.sinfo.year", "years");
     }
 
-    public int[][] getEpochs() {
+    public List<DateRange> getEpochs() {
         return this.helper.getEpochs("state.sinfo.epoch", "epochs");
     }
 
+    /**
+     * String expression of the years or epoches collection, resp.
+     */
+    public String getYearsHeader() {
+        if (getYears() != null) {
+            final String ys = Arrays.toString(getYears());
+            return ys.substring(1, ys.length() - 1);
+        }
+        else {
+            String ys = "";
+            String sep = "";
+            for (final DateRange dr : getEpochs()) {
+                ys += String.format("%s%d-%d", sep, dr.getFromYear(), dr.getToYear());
+                sep = ", ";
+            }
+            return ys;
+        }
+    }
 }
\ No newline at end of file
diff -r cd24db77f044 -r 9b4f5e61c02e artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationAccess.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationAccess.java	Thu Jun 14 17:51:30 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationAccess.java	Tue Jun 19 14:15:32 2018 +0200
@@ -10,8 +10,12 @@
 
 package org.dive4elements.river.artifacts.uinfo.inundationduration;
 
+import java.util.List;
+
+import org.dive4elements.artifacts.common.utils.DateUtils;
 import org.dive4elements.river.artifacts.access.RangeAccess;
 import org.dive4elements.river.artifacts.common.EpochYearAccessHelper;
+import org.dive4elements.river.artifacts.model.DateRange;
 import org.dive4elements.river.artifacts.uinfo.UINFOArtifact;
 import org.dive4elements.river.artifacts.uinfo.UinfoCalcMode;
 
@@ -67,9 +71,9 @@
     }
 
     public int[] getTotalEpoch() {
-        final int[][] epochs = this.helper.getEpochs("state.uinfo.totalepoch", "totalepoch");
-        if (epochs != null) {
-            return epochs[0];
+        final List<DateRange> epochs = this.helper.getEpochs("state.uinfo.totalepoch", "totalepoch");
+        if (!epochs.isEmpty()) {
+            return new int[] { DateUtils.getYearFromDate(epochs.get(0).getFrom()), DateUtils.getYearFromDate(epochs.get(0).getTo()) };
         }
         return null;
     }


More information about the Dive4Elements-commits mailing list