[Lada-commits] [PATCH] Use Intervall class for generation of weekly Probe objects

Wald Commits scm-commit at wald.intevation.org
Sat Oct 1 15:56:33 CEST 2016


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1475322931 -7200
# Node ID e544d76a311f84baaba805d1613ffa39c4b981ee
# Parent  7974d3cc974f248bac18e7ed8014c1b21fa95c22
Use Intervall class for generation of weekly Probe objects.

diff -r 7974d3cc974f -r e544d76a311f src/main/java/de/intevation/lada/factory/ProbeFactory.java
--- a/src/main/java/de/intevation/lada/factory/ProbeFactory.java	Fri Sep 30 22:39:48 2016 +0200
+++ b/src/main/java/de/intevation/lada/factory/ProbeFactory.java	Sat Oct 01 13:55:31 2016 +0200
@@ -48,7 +48,10 @@
     private static Hashtable<String, int[]> fieldsTable;
 
     public ProbeFactory() {
-        int[] T = { Calendar.DAY_OF_YEAR, Calendar.DAY_OF_YEAR, 1 };
+        int[] T  = { Calendar.DAY_OF_YEAR, Calendar.DAY_OF_YEAR, 1 };
+        int[] W  = { Calendar.DAY_OF_YEAR, Calendar.DAY_OF_YEAR, 7 };
+        int[] W2 = { Calendar.DAY_OF_YEAR, Calendar.DAY_OF_YEAR, 14 };
+        int[] W4 = { Calendar.DAY_OF_YEAR, Calendar.DAY_OF_YEAR, 28 };
 
         int[] M = { Calendar.MONTH, Calendar.DAY_OF_MONTH, 1 };
         int[] Q = { Calendar.MONTH, Calendar.DAY_OF_MONTH, 3 };
@@ -57,19 +60,22 @@
         this.fieldsTable = new Hashtable<String, int[]>();
 
         this.fieldsTable.put("T", T);
+        this.fieldsTable.put("W", W);
+        this.fieldsTable.put("W2", W2);
+        this.fieldsTable.put("W4", W4);
         this.fieldsTable.put("M", M);
         this.fieldsTable.put("Q", Q);
         this.fieldsTable.put("H", H);
     }
 
     private class Intervall {
-        private int teilVon;
-        private int teilBis;
-        private int offset;
+        private final int teilVon;
+        private final int teilBis;
+        private final int offset;
 
-        private int intervallField;
-        private int subIntField;
-        private int intervallFactor;
+        private final int intervallField;
+        private final int subIntField;
+        private final int intervallFactor;
 
         private Calendar from;
 
@@ -88,14 +94,24 @@
             this.intervallFactor = fieldsTable
                 .get(messprogramm.getProbenintervall())[2];
 
+            this.from = (Calendar)start.clone();
+
             /* Align with beginning of next interval
-             * like first day of next quarter.*/
-            int startIntField = start.get(intervallField);
-            this.from = (Calendar)start.clone();
-            from.set(
-                intervallField,
-                startIntField + startIntField % intervallFactor
-            );
+             * like first day of next quarter or Monday of next week.*/
+            if (intervallField == Calendar.DAY_OF_YEAR
+                && intervallFactor % 7 == 0
+            ) {
+                if (from.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
+                    from.add(Calendar.WEEK_OF_YEAR, 1);
+                    from.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
+                }
+            } else {
+                int startIntField = start.get(intervallField);
+                from.set(
+                    intervallField,
+                    startIntField + startIntField % intervallFactor
+                );
+            }
             from = adjustSubIntField(from, teilVon);
             if (start.after(from)) {
                 // to next intervall if start not at first day of intervall
@@ -114,11 +130,16 @@
         */
         private Calendar adjustSubIntField(Calendar cal, int teil) {
             int intValue = cal.get(intervallField);
-            intValue = intValue - intValue % intervallFactor;
-            cal.set(intervallField, intValue);
+            int adjust = offset;
 
-            int subIntValue = intervallField == subIntField ? intValue : 0
-                + offset + Math.min(teil, getDuration());
+            if (intervallField != subIntField) {
+                intValue = intValue - intValue % intervallFactor;
+                cal.set(intervallField, intValue);
+            } else {
+                adjust += intValue - 1;
+            }
+
+            int subIntValue = adjust + Math.min(teil, getDuration());
             cal.set(subIntField, subIntValue);
 
             return cal;
@@ -211,7 +232,7 @@
         List<LProbe> proben = new ArrayList<LProbe>();
 
         if (fieldsTable.keySet().contains(messprogramm.getProbenintervall())) {
-            proben.addAll(generateMonthly(messprogramm, start, end));
+            proben.addAll(generateIntervall(messprogramm, start, end));
         }
         else {
             Date[][] intervals = calculateIntervals(start, end, messprogramm);
@@ -243,20 +264,7 @@
         end.setTime(interval[1]);
         int endDay = end.get(Calendar.DAY_OF_YEAR);
 
-        // If fixed interval (W, W2, W4)
-        if ("W".equals(messprogramm.getProbenintervall())) {
-            Calendar realStart = getMonday(start);
-            proben.addAll(generate(messprogramm, realStart, end, 7));
-        }
-        else if ("W2".equals(messprogramm.getProbenintervall())) {
-            Calendar realStart = getMonday(start);
-            proben.addAll(generate(messprogramm, realStart, end, 14));
-        }
-        else if ("W4".equals(messprogramm.getProbenintervall())) {
-            Calendar realStart = getMonday(start);
-            proben.addAll(generate(messprogramm, realStart, end, 28));
-        }
-        else if ("J".equals(messprogramm.getProbenintervall())) {
+        if ("J".equals(messprogramm.getProbenintervall())) {
             int offset = messprogramm.getIntervallOffset();
             int teilVon = messprogramm.getTeilintervallVon();
             int teilBis = messprogramm.getTeilintervallBis();
@@ -276,7 +284,7 @@
         }
     }
 
-    private List<LProbe> generateMonthly(
+    private List<LProbe> generateIntervall(
         Messprogramm messprogramm,
         Calendar start,
         Calendar end


More information about the Lada-commits mailing list