Mercurial > dive4elements > gnv-client
comparison geo-backend/src/main/java/de/intevation/gnv/geobackend/util/RedundancyRemover.java @ 899:3f9fc88aec2b
merged geo-backend/1.0
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:57 +0200 |
parents | b757def3ff55 |
children | ebeb56428409 |
comparison
equal
deleted
inserted
replaced
875:5e9efdda6894 | 899:3f9fc88aec2b |
---|---|
1 package de.intevation.gnv.geobackend.util; | |
2 | |
3 import java.util.LinkedHashMap; | |
4 import java.util.Map; | |
5 | |
6 /** | |
7 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | |
8 */ | |
9 public final class RedundancyRemover | |
10 extends LinkedHashMap | |
11 { | |
12 /** ceil(1029 * 1.75) = 1801, which is prime | |
13 * -> 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 : |