diff geo-backend/src/main/java/de/intevation/gnv/geobackend/util/RedundancyRemover.java @ 1129:ccfa07b88476

merged geo-backend
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:01 +0200
parents ebeb56428409
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/geo-backend/src/main/java/de/intevation/gnv/geobackend/util/RedundancyRemover.java	Fri Sep 28 12:14:01 2012 +0200
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2010 by Intevation GmbH
+ *
+ * This program is free software under the LGPL (>=v2.1)
+ * Read the file LGPL.txt coming with the software for details
+ * or visit http://www.gnu.org/licenses/ if it does not exist.
+ */
+
+package de.intevation.gnv.geobackend.util;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
+ */
+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