Mercurial > dive4elements > gnv-client
annotate geo-backend/src/main/java/de/intevation/gnv/geobackend/util/RedundancyRemover.java @ 1144:f444cfce0b3d
dummy merge for repo head
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:15:22 +0200 |
parents | ebeb56428409 |
children |
rev | line source |
---|---|
1127
ebeb56428409
Added license headers and license file.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
887
diff
changeset
|
1 /* |
ebeb56428409
Added license headers and license file.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
887
diff
changeset
|
2 * Copyright (c) 2010 by Intevation GmbH |
ebeb56428409
Added license headers and license file.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
887
diff
changeset
|
3 * |
ebeb56428409
Added license headers and license file.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
887
diff
changeset
|
4 * This program is free software under the LGPL (>=v2.1) |
ebeb56428409
Added license headers and license file.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
887
diff
changeset
|
5 * Read the file LGPL.txt coming with the software for details |
ebeb56428409
Added license headers and license file.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
887
diff
changeset
|
6 * or visit http://www.gnu.org/licenses/ if it does not exist. |
ebeb56428409
Added license headers and license file.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
887
diff
changeset
|
7 */ |
ebeb56428409
Added license headers and license file.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
887
diff
changeset
|
8 |
555
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 package de.intevation.gnv.geobackend.util; |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 import java.util.LinkedHashMap; |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 import java.util.Map; |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 /** |
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
|
15 * @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
|
16 */ |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 public final class RedundancyRemover |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 extends LinkedHashMap |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 { |
885
1c3efbd2fc5a
Removes trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
555
diff
changeset
|
20 /** ceil(1029 * 1.75) = 1801, which is prime |
555
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 * -> suitable for a hash map slot size. |
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 static final int DEFAULT_LOOKBACK = |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 Integer.getInteger( |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 "de.intevation.gnv.geobackend.util.RedundancyRemover.lookback", |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 1029); |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 private int maxCapacity; |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 private int removed; |
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 public RedundancyRemover() { |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 this(DEFAULT_LOOKBACK); |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 } |
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 public RedundancyRemover(int maxCapacity) { |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 super((int)Math.ceil(maxCapacity * 1.75f)); |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 this.maxCapacity = maxCapacity; |
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 protected boolean removeEldestEntry(Map.Entry eldest) { |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 return size() > maxCapacity; |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 } |
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 public int numRemoved() { |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 return removed; |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 } |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 public Object filter(Object object) { |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 if (object == null) { |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 return object; |
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 Object old = get(object); |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 if (old != null) { |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 if (old != object) { // count only identical |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 ++removed; |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 } |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 return old; |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 } |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 put(object, object); |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 return object; |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 } |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 } |
825781a39c70
Fixed gnv/issue107
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |