annotate geo-backend/src/main/java/de/intevation/gnv/geobackend/util/RedundancyRemover.java @ 895:eb777022b628

Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary geo-backend/trunk@958 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 20 Apr 2010 12:42:17 +0000
parents b757def3ff55
children ebeb56428409
rev   line source
555
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.gnv.geobackend.util;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import java.util.LinkedHashMap;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import java.util.Map;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 /**
887
b757def3ff55 Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 885
diff changeset
7 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
555
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 */
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 public final class RedundancyRemover
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 extends LinkedHashMap
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 {
885
1c3efbd2fc5a Removes trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 555
diff changeset
12 /** ceil(1029 * 1.75) = 1801, which is prime
555
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 * -&gt; suitable for a hash map slot size.
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 */
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 public static final int DEFAULT_LOOKBACK =
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 Integer.getInteger(
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 "de.intevation.gnv.geobackend.util.RedundancyRemover.lookback",
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 1029);
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 private int maxCapacity;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 private int removed;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 public RedundancyRemover() {
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 this(DEFAULT_LOOKBACK);
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 }
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 public RedundancyRemover(int maxCapacity) {
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 super((int)Math.ceil(maxCapacity * 1.75f));
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 this.maxCapacity = maxCapacity;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 }
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 protected boolean removeEldestEntry(Map.Entry eldest) {
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 return size() > maxCapacity;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 }
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 public int numRemoved() {
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 return removed;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 }
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 public Object filter(Object object) {
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 if (object == null) {
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 return object;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 }
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 Object old = get(object);
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 if (old != null) {
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 if (old != object) { // count only identical
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 ++removed;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 }
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 return old;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 }
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 put(object, object);
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 return object;
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 }
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 }
825781a39c70 Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org