view artifacts/src/main/java/org/dive4elements/river/utils/UniqueDateFormatter.java @ 8755:30b1ddadf275

(issue1801) Unify reference gauge finding code The basic way as described in the method comment of the determineRefGauge method is now used in the WINFOArtifact, MainValuesService and RiverUtils.getGauge method. RiverUtils.getGauge previously just returned the first gauge found. While this is now a behavior change I believe that it is always more correct then the undeterministic behavior of the previous implmenentation.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 24 Jun 2015 14:07:26 +0200
parents 90f66b4fc34d
children 0a5239a1e46e
line wrap: on
line source
/* 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);
    }
}

http://dive4elements.wald.intevation.org