sascha@555: package de.intevation.gnv.geobackend.util; sascha@555: sascha@555: import java.util.LinkedHashMap; sascha@555: import java.util.Map; sascha@555: sascha@555: /** sascha@555: * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) sascha@555: */ sascha@555: public final class RedundancyRemover sascha@555: extends LinkedHashMap sascha@555: { sascha@885: /** ceil(1029 * 1.75) = 1801, which is prime sascha@555: * -> suitable for a hash map slot size. sascha@555: */ sascha@555: public static final int DEFAULT_LOOKBACK = sascha@555: Integer.getInteger( sascha@555: "de.intevation.gnv.geobackend.util.RedundancyRemover.lookback", sascha@555: 1029); sascha@555: sascha@555: private int maxCapacity; sascha@555: private int removed; sascha@555: sascha@555: public RedundancyRemover() { sascha@555: this(DEFAULT_LOOKBACK); sascha@555: } sascha@555: sascha@555: public RedundancyRemover(int maxCapacity) { sascha@555: super((int)Math.ceil(maxCapacity * 1.75f)); sascha@555: this.maxCapacity = maxCapacity; sascha@555: } sascha@555: sascha@555: protected boolean removeEldestEntry(Map.Entry eldest) { sascha@555: return size() > maxCapacity; sascha@555: } sascha@555: sascha@555: public int numRemoved() { sascha@555: return removed; sascha@555: } sascha@555: sascha@555: public Object filter(Object object) { sascha@555: if (object == null) { sascha@555: return object; sascha@555: } sascha@555: Object old = get(object); sascha@555: sascha@555: if (old != null) { sascha@555: if (old != object) { // count only identical sascha@555: ++removed; sascha@555: } sascha@555: return old; sascha@555: } sascha@555: put(object, object); sascha@555: return object; sascha@555: } sascha@555: } sascha@555: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :