changeset 9155:9b4f5e61c02e

Changed to list of DateRange, added methods in DateRange to support year ranges
author mschaefer
date Tue, 19 Jun 2018 14:15:32 +0200
parents cd24db77f044
children 568961ff709a
files artifacts/src/main/java/org/dive4elements/river/artifacts/common/EpochYearAccessHelper.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/DateRange.java artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/CollisionAccess.java artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationAccess.java
diffstat 4 files changed, 59 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- 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 + "'");
--- 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 :
--- 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
--- 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;
     }

http://dive4elements.wald.intevation.org