Mercurial > dive4elements > gnv-client
comparison geo-backend/src/main/java/de/intevation/gnv/geobackend/util/RedundancyRemover.java @ 555:825781a39c70
Fixed gnv/issue107
geo-backend/trunk@632 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 26 Jan 2010 16:33:36 +0000 |
parents | |
children | 1c3efbd2fc5a |
comparison
equal
deleted
inserted
replaced
554:0ee3c0ed40e4 | 555:825781a39c70 |
---|---|
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 * -> 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 : |