Mercurial > dive4elements > gnv-client
comparison 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 |
comparison
equal
deleted
inserted
replaced
1119:7c4f81f74c47 | 1129:ccfa07b88476 |
---|---|
1 /* | |
2 * Copyright (c) 2010 by Intevation GmbH | |
3 * | |
4 * This program is free software under the LGPL (>=v2.1) | |
5 * Read the file LGPL.txt coming with the software for details | |
6 * or visit http://www.gnu.org/licenses/ if it does not exist. | |
7 */ | |
8 | |
9 package de.intevation.gnv.geobackend.util; | |
10 | |
11 import java.util.LinkedHashMap; | |
12 import java.util.Map; | |
13 | |
14 /** | |
15 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | |
16 */ | |
17 public final class RedundancyRemover | |
18 extends LinkedHashMap | |
19 { | |
20 /** ceil(1029 * 1.75) = 1801, which is prime | |
21 * -> suitable for a hash map slot size. | |
22 */ | |
23 public static final int DEFAULT_LOOKBACK = | |
24 Integer.getInteger( | |
25 "de.intevation.gnv.geobackend.util.RedundancyRemover.lookback", | |
26 1029); | |
27 | |
28 private int maxCapacity; | |
29 private int removed; | |
30 | |
31 public RedundancyRemover() { | |
32 this(DEFAULT_LOOKBACK); | |
33 } | |
34 | |
35 public RedundancyRemover(int maxCapacity) { | |
36 super((int)Math.ceil(maxCapacity * 1.75f)); | |
37 this.maxCapacity = maxCapacity; | |
38 } | |
39 | |
40 protected boolean removeEldestEntry(Map.Entry eldest) { | |
41 return size() > maxCapacity; | |
42 } | |
43 | |
44 public int numRemoved() { | |
45 return removed; | |
46 } | |
47 | |
48 public Object filter(Object object) { | |
49 if (object == null) { | |
50 return object; | |
51 } | |
52 Object old = get(object); | |
53 | |
54 if (old != null) { | |
55 if (old != object) { // count only identical | |
56 ++removed; | |
57 } | |
58 return old; | |
59 } | |
60 put(object, object); | |
61 return object; | |
62 } | |
63 } | |
64 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |