[PATCH 2 of 2] Factored our date formatter

Wald Commits scm-commit at wald.intevation.org
Mon Aug 19 12:45:38 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1376909127 -7200
# Node ID 90f66b4fc34d3cfd5325d8839c2d4bf9f8b8b993
# Parent  b55111e13acd312400aa06ce19eda3d8ad11de5a
Factored our date formatter.

diff -r b55111e13acd -r 90f66b4fc34d artifacts/src/main/java/org/dive4elements/river/artifacts/states/fixation/FixAnalysisCompute.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/fixation/FixAnalysisCompute.java	Mon Aug 19 12:44:11 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/fixation/FixAnalysisCompute.java	Mon Aug 19 12:45:27 2013 +0200
@@ -11,9 +11,7 @@
 import java.text.DateFormat;
 import java.util.Collection;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.log4j.Logger;
 
@@ -46,6 +44,7 @@
 import org.dive4elements.river.artifacts.states.DefaultState;
 import org.dive4elements.river.utils.Formatter;
 import org.dive4elements.river.utils.IdGenerator;
+import org.dive4elements.river.utils.UniqueDateFormatter;
 
 /**
  * @author <a href="mailto:raimund.renkert at intevation.de">Raimund Renkert</a>
@@ -340,46 +339,5 @@
                 I18N_DEVIATION)));
         return res;
     }
-
-    /** Little hack to format dates unique if they collide. */
-    private static class UniqueDateFormatter {
-
-        private DateFormat df;
-        private DateFormat lf;
-        private Map<String, int[]> collisions;
-
-        UniqueDateFormatter(
-            DateFormat df,
-            DateFormat lf,
-            Collection<Date> dates
-        ) {
-            this.df = df;
-            this.lf = lf;
-            collisions = build(dates);
-        }
-
-        private Map<String, int []> build(Collection<Date> dates) {
-            Map<String, int []> collisions = new HashMap<String, int[]>();
-            for (Date d: dates) {
-                String s = df.format(d);
-                int [] count = collisions.get(s);
-                if (count == null) {
-                    collisions.put(s, count = new int[1]);
-                }
-                if (++count[0] > 1) {
-                    log.debug("date collsion found: " + d);
-                }
-            }
-            return collisions;
-        }
-
-        String format(Date date) {
-            String s = df.format(date);
-            int [] count = collisions.get(s);
-            return count == null || count[0] < 2
-                ? s
-                : lf.format(date);
-        }
-    } // class UniqueDateFormatter
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
diff -r b55111e13acd -r 90f66b4fc34d artifacts/src/main/java/org/dive4elements/river/utils/UniqueDateFormatter.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/utils/UniqueDateFormatter.java	Mon Aug 19 12:45:27 2013 +0200
@@ -0,0 +1,59 @@
+/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+
+package org.dive4elements.river.utils;
+
+import java.text.DateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
+public class UniqueDateFormatter {
+
+    private static Logger log = Logger.getLogger(UniqueDateFormatter.class);
+
+    private DateFormat df;
+    private DateFormat lf;
+    private Map<String, int[]> collisions;
+
+    public UniqueDateFormatter(
+        DateFormat df,
+        DateFormat lf,
+        Collection<Date> dates
+    ) {
+        this.df = df;
+        this.lf = lf;
+        collisions = build(dates);
+    }
+
+    private Map<String, int []> build(Collection<Date> dates) {
+        Map<String, int []> collisions = new HashMap<String, int[]>();
+        for (Date d: dates) {
+            String s = df.format(d);
+            int [] count = collisions.get(s);
+            if (count == null) {
+                collisions.put(s, count = new int[1]);
+            }
+            if (++count[0] > 1) {
+                log.debug("date collsion found: " + d);
+            }
+        }
+        return collisions;
+    }
+
+    public String format(Date date) {
+        String s = df.format(date);
+        int [] count = collisions.get(s);
+        return count == null || count[0] < 2
+            ? s
+            : lf.format(date);
+    }
+}


More information about the Dive4elements-commits mailing list