comparison artifacts/src/main/java/org/dive4elements/river/utils/UniqueDateFormatter.java @ 6865:90f66b4fc34d

Factored our date formatter.
author Sascha L. Teichmann <teichmann@intevation.de>
date Mon, 19 Aug 2013 12:45:27 +0200
parents
children 0a5239a1e46e
comparison
equal deleted inserted replaced
6864:b55111e13acd 6865:90f66b4fc34d
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU AGPL (>=v3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details.
7 */
8
9 package org.dive4elements.river.utils;
10
11 import java.text.DateFormat;
12 import java.util.Collection;
13 import java.util.Date;
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import org.apache.log4j.Logger;
18
19 public class UniqueDateFormatter {
20
21 private static Logger log = Logger.getLogger(UniqueDateFormatter.class);
22
23 private DateFormat df;
24 private DateFormat lf;
25 private Map<String, int[]> collisions;
26
27 public UniqueDateFormatter(
28 DateFormat df,
29 DateFormat lf,
30 Collection<Date> dates
31 ) {
32 this.df = df;
33 this.lf = lf;
34 collisions = build(dates);
35 }
36
37 private Map<String, int []> build(Collection<Date> dates) {
38 Map<String, int []> collisions = new HashMap<String, int[]>();
39 for (Date d: dates) {
40 String s = df.format(d);
41 int [] count = collisions.get(s);
42 if (count == null) {
43 collisions.put(s, count = new int[1]);
44 }
45 if (++count[0] > 1) {
46 log.debug("date collsion found: " + d);
47 }
48 }
49 return collisions;
50 }
51
52 public String format(Date date) {
53 String s = df.format(date);
54 int [] count = collisions.get(s);
55 return count == null || count[0] < 2
56 ? s
57 : lf.format(date);
58 }
59 }

http://dive4elements.wald.intevation.org