view geo-backend/src/main/java/de/intevation/gnv/geobackend/util/RedundancyRemover.java @ 659:d45bca50b4d7

Changed log4j version to 1.2.14 geo-backend/trunk@663 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 05 Feb 2010 14:12:25 +0000
parents 825781a39c70
children 1c3efbd2fc5a
line wrap: on
line source
package de.intevation.gnv.geobackend.util;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
 */
public final class RedundancyRemover
extends            LinkedHashMap
{
    /** ceil(1029 * 1.75) = 1801, which is prime 
     * -&gt; suitable for a hash map slot size.
     */
    public static final int DEFAULT_LOOKBACK =
        Integer.getInteger(
            "de.intevation.gnv.geobackend.util.RedundancyRemover.lookback",
            1029);

    private int maxCapacity;
    private int removed;

    public RedundancyRemover() {
        this(DEFAULT_LOOKBACK);
    }

    public RedundancyRemover(int maxCapacity) {
        super((int)Math.ceil(maxCapacity * 1.75f));
        this.maxCapacity = maxCapacity;
    }

    protected boolean removeEldestEntry(Map.Entry eldest) {
        return size() > maxCapacity;
    }

    public int numRemoved() {
        return removed;
    }

    public Object filter(Object object) {
        if (object == null) {
            return object;
        }
        Object old = get(object);

        if (old != null) {
            if (old != object) { // count only identical
                ++removed;
            }
            return old;
        }
        put(object, object);
        return object;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org