annotate flys-artifacts/src/main/java/de/intevation/flys/utils/DateAverager.java @ 4031:e4e345d81a65

issue889/2
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 04 Oct 2012 14:54:44 +0200
parents d520a0869972
children
rev   line source
3007
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.utils;
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import gnu.trove.TLongArrayList;
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import java.util.Date;
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 public class DateAverager
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 {
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 protected TLongArrayList dates;
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 public DateAverager() {
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 dates = new TLongArrayList();
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 }
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 public void add(Date date) {
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 dates.add(date.getTime());
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 }
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 public Date getAverage() {
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 int N = dates.size();
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 if (N == 0) {
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 return null;
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 }
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 long min = dates.min();
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 long sum = 0L;
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 for (int i = 0; i < N; ++i) {
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 sum += dates.getQuick(i) - min;
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 }
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 return new Date(min + (long)Math.round(sum/(double)N));
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 }
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 public void clear() {
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 dates.resetQuick();
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 }
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 }
d520a0869972 Added class to average dates without overflows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org