comparison geo-backend/src/main/java/de/intevation/gnv/geobackend/util/RedundancyRemover.java @ 662:755dd2fa4a0a 0.5

merged geo-backend/0.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:53 +0200
parents 825781a39c70
children 1c3efbd2fc5a
comparison
equal deleted inserted replaced
657:af3f56758f59 662:755dd2fa4a0a
1 package de.intevation.gnv.geobackend.util;
2
3 import java.util.LinkedHashMap;
4 import java.util.Map;
5
6 /**
7 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
8 */
9 public final class RedundancyRemover
10 extends LinkedHashMap
11 {
12 /** ceil(1029 * 1.75) = 1801, which is prime
13 * -&gt; suitable for a hash map slot size.
14 */
15 public static final int DEFAULT_LOOKBACK =
16 Integer.getInteger(
17 "de.intevation.gnv.geobackend.util.RedundancyRemover.lookback",
18 1029);
19
20 private int maxCapacity;
21 private int removed;
22
23 public RedundancyRemover() {
24 this(DEFAULT_LOOKBACK);
25 }
26
27 public RedundancyRemover(int maxCapacity) {
28 super((int)Math.ceil(maxCapacity * 1.75f));
29 this.maxCapacity = maxCapacity;
30 }
31
32 protected boolean removeEldestEntry(Map.Entry eldest) {
33 return size() > maxCapacity;
34 }
35
36 public int numRemoved() {
37 return removed;
38 }
39
40 public Object filter(Object object) {
41 if (object == null) {
42 return object;
43 }
44 Object old = get(object);
45
46 if (old != null) {
47 if (old != object) { // count only identical
48 ++removed;
49 }
50 return old;
51 }
52 put(object, object);
53 return object;
54 }
55 }
56 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org