[Lada-commits] [PATCH 3 of 3] Use Intervall-class for generation of yearly Probe objects
Wald Commits
scm-commit at wald.intevation.org
Tue Oct 11 16:39:30 CEST 2016
# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1476196684 -7200
# Node ID 99c6c644773b29f84ba02c919a4cc5fd1ee91d61
# Parent 6499f2410c420b6bd3f30cf7845bdd4c349f1326
Use Intervall-class for generation of yearly Probe objects.
All intervalls are now based on this class and obsolete methods
were removed.
diff -r 6499f2410c42 -r 99c6c644773b src/main/java/de/intevation/lada/factory/ProbeFactory.java
--- a/src/main/java/de/intevation/lada/factory/ProbeFactory.java Mon Oct 10 17:23:39 2016 +0200
+++ b/src/main/java/de/intevation/lada/factory/ProbeFactory.java Tue Oct 11 16:38:04 2016 +0200
@@ -45,6 +45,9 @@
*/
public class ProbeFactory {
+ // Day of year representing February 28
+ private static final int FEBRUARY_28 = 58;
+
private static Hashtable<String, int[]> fieldsTable;
public ProbeFactory() {
@@ -57,6 +60,8 @@
int[] Q = { Calendar.MONTH, Calendar.DAY_OF_MONTH, 3 };
int[] H = { Calendar.MONTH, Calendar.DAY_OF_MONTH, 6 };
+ int[] J = { Calendar.YEAR, Calendar.DAY_OF_YEAR, 1 };
+
this.fieldsTable = new Hashtable<String, int[]>();
this.fieldsTable.put("T", T);
@@ -66,6 +71,7 @@
this.fieldsTable.put("M", M);
this.fieldsTable.put("Q", Q);
this.fieldsTable.put("H", H);
+ this.fieldsTable.put("J", J);
}
private class Intervall {
@@ -135,6 +141,13 @@
if (intervallField != subIntField) {
intValue = intValue - intValue % intervallFactor;
cal.set(intervallField, intValue);
+
+ if (subIntField == Calendar.DAY_OF_YEAR) {
+ // Adjust in leap year
+ teil += startInLeapYear() && teil > FEBRUARY_28
+ ? 1
+ : 0;
+ }
} else {
adjust += intValue - 1;
}
@@ -156,6 +169,7 @@
}
int duration = 0;
Calendar tmp = (Calendar)from.clone();
+
/* reset to beginning of intervall, e.g. first day of quarter
* to compensate possible overflow if
* teilVon > maximum of intervallField: */
@@ -165,6 +179,7 @@
intValue - intValue % intervallFactor
);
tmp.set(subIntField, tmp.getActualMinimum(subIntField));
+
for (int i = 0; i < intervallFactor; i++) {
duration += tmp.getActualMaximum(subIntField);
tmp.add(intervallField, 1);
@@ -229,68 +244,6 @@
end.set(Calendar.MINUTE, 59);
end.set(Calendar.SECOND, 59);
- List<LProbe> proben = new ArrayList<LProbe>();
-
- if (fieldsTable.keySet().contains(messprogramm.getProbenintervall())) {
- proben.addAll(generateIntervall(messprogramm, start, end));
- }
- else {
- Date[][] intervals = calculateIntervals(start, end, messprogramm);
- for (Date[] interval : intervals) {
- createProben(interval, messprogramm, proben);
- }
- }
- return proben;
- }
-
- /**
- * Create LProbe objects using the interval and messprogramm details.
- *
- * @param interval The time interval for probe objects.
- * @param messprogramm The messprogramm containing details.
- * @param proben An (empty) list for probe objects
- * filled by this method
- * @return
- */
- private void createProben(
- Date[] interval,
- Messprogramm messprogramm,
- List<LProbe> proben
- ) {
- Calendar start = Calendar.getInstance();
- start.setTime(interval[0]);
- int startDay = start.get(Calendar.DAY_OF_YEAR);
- Calendar end = Calendar.getInstance();
- end.setTime(interval[1]);
- int endDay = end.get(Calendar.DAY_OF_YEAR);
-
- if ("J".equals(messprogramm.getProbenintervall())) {
- int offset = messprogramm.getIntervallOffset();
- int teilVon = messprogramm.getTeilintervallVon();
- int teilBis = messprogramm.getTeilintervallBis();
-
- if (teilVon >= startDay + offset
- && teilBis <= endDay + offset
- ) {
- start.add(Calendar.DATE, teilVon - startDay + offset);
- Date startDate = start.getTime();
- end.add(Calendar.DATE, teilBis - endDay + offset);
- Date endDate = end.getTime();
- LProbe probe = createObjects(messprogramm, startDate, endDate);
- if (probe != null) {
- proben.add(probe);
- }
- }
- }
- }
-
- private List<LProbe> generateIntervall(
- Messprogramm messprogramm,
- Calendar start,
- Calendar end
- ) {
- logger.debug("start: " + start);
-
int gueltigVon = messprogramm.getGueltigVon();
int gueltigBis = messprogramm.getGueltigBis();
@@ -307,11 +260,11 @@
* March 1 and thus has to be adapted in a leap year. */
int leapDay = intervall.startInLeapYear() ? 1 : 0;
int actualGueltigVon =
- gueltigVon > 58
+ gueltigVon > FEBRUARY_28
? gueltigVon + leapDay
: gueltigVon;
int actualGueltigBis =
- gueltigBis > 58
+ gueltigBis > FEBRUARY_28
? gueltigBis + leapDay
: gueltigBis;
@@ -465,69 +418,6 @@
}
/**
- * Determine the interval for probe generation using a start date, end date
- * and the messprogramm.
- *
- * @param start Calendar object defining the start of the first interval
- * @param end Calendar object defining the end of the last interval.
- * @param messprogramm The messprogramm
- *
- * @return An array of start/end pairs.
- */
- private Date[][] calculateIntervals(
- Calendar start,
- Calendar end,
- Messprogramm messprogramm
- ) {
- int realStart = start.get(Calendar.DAY_OF_YEAR);
- int realEnd = end.get(Calendar.DAY_OF_YEAR);
- int startYear = start.get(Calendar.YEAR);
- int endYear = end.get(Calendar.YEAR);
- int years = (endYear - startYear) + 1;
- Date[][] intervals = new Date[years][2];
- for (int i = 0; i < years; i++) {
- Calendar cStart = Calendar.getInstance();
- cStart.set(
- startYear + i,
- start.get(Calendar.MONTH),
- start.get(Calendar.DAY_OF_MONTH)
- );
- if (messprogramm.getGueltigVon() <= 0
- || (realStart > messprogramm.getGueltigVon() && i == 0)
- ) {
- intervals[0][0] = start.getTime();
- }
- else {
- start.add(Calendar.DATE,
- messprogramm.getGueltigVon() - realStart);
- Date startDate = start.getTime();
- intervals[i][0] = startDate;
- }
-
- Calendar cEnd = Calendar.getInstance();
- cEnd.set(
- startYear + i,
- end.get(Calendar.MONTH),
- end.get(Calendar.DAY_OF_MONTH)
- );
- if (messprogramm.getGueltigBis() <= 0
- || (realEnd < messprogramm.getGueltigBis() && i == years - 1)
- ) {
- intervals[i][1] = cEnd.getTime();
- }
- else {
- cEnd.add(Calendar.DATE,
- messprogramm.getGueltigBis() - realEnd);
- Date endDate = cEnd.getTime();
- intervals[i][1] = endDate;
- }
- }
-
- return intervals;
- }
-
-
- /**
* Search for the umwelt id using the 'deskriptor'.
*
* @param probe The probe object.
More information about the Lada-commits
mailing list