changeset 3007:d520a0869972

Added class to average dates without overflows. flys-artifacts/trunk@4563 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 30 May 2012 17:01:54 +0000
parents 1b1af1597401
children 9e0500d64524
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/utils/DateAverager.java
diffstat 2 files changed, 42 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed May 30 15:38:23 2012 +0000
+++ b/flys-artifacts/ChangeLog	Wed May 30 17:01:54 2012 +0000
@@ -1,7 +1,12 @@
+2012-05-30	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/utils/DateAverager.java:
+	  New. Averages a list of dates preventing overflows.
+
 2012-05-30	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java:
-	  Added a km filter to find columns with touches a given km.
+	  Added a km filter to find columns which touches a given km.
 
 	* src/main/java/de/intevation/flys/artifacts/model/FixingsFilterBuilder.java:
 	  Expose the new km filter to the fixing overview service. Syntax:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/DateAverager.java	Wed May 30 17:01:54 2012 +0000
@@ -0,0 +1,36 @@
+package de.intevation.flys.utils;
+
+import gnu.trove.TLongArrayList;
+
+import java.util.Date;
+
+public class DateAverager
+{
+   protected TLongArrayList dates;
+
+    public DateAverager() {
+        dates = new TLongArrayList();
+    }
+
+    public void add(Date date) {
+        dates.add(date.getTime());
+    }
+
+    public Date getAverage() {
+        int N = dates.size();
+        if (N == 0) {
+            return null;
+        }
+        long min = dates.min();
+        long sum = 0L;
+        for (int i = 0; i < N; ++i) {
+            sum += dates.getQuick(i) - min;
+        }
+        return new Date(min + (long)Math.round(sum/(double)N));
+    }
+
+    public void clear() {
+        dates.resetQuick();
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org