Commit 57b0e19ec853b62f0bed89675c849cc1a194abc0
Committed by
Andrew Shvayka
1 parent
3d3aae60
util: LinkedHashMapRemoveEldest refactored
Showing
2 changed files
with
8 additions
and
6 deletions
... | ... | @@ -50,11 +50,11 @@ import java.util.function.BiConsumer; |
50 | 50 | @EqualsAndHashCode(callSuper = true) |
51 | 51 | public class LinkedHashMapRemoveEldest<K, V> extends LinkedHashMap<K, V> { |
52 | 52 | final long maxEntries; |
53 | - final BiConsumer<K, V> removeConsumer; | |
53 | + final BiConsumer<K, V> removalConsumer; | |
54 | 54 | |
55 | - public LinkedHashMapRemoveEldest(long maxEntries, BiConsumer<K, V> removeConsumer) { | |
55 | + public LinkedHashMapRemoveEldest(long maxEntries, BiConsumer<K, V> removalConsumer) { | |
56 | 56 | this.maxEntries = maxEntries; |
57 | - this.removeConsumer = removeConsumer; | |
57 | + this.removalConsumer = removalConsumer; | |
58 | 58 | } |
59 | 59 | |
60 | 60 | @Override |
... | ... | @@ -62,7 +62,7 @@ public class LinkedHashMapRemoveEldest<K, V> extends LinkedHashMap<K, V> { |
62 | 62 | if (size() <= maxEntries) { |
63 | 63 | return false; |
64 | 64 | } |
65 | - removeConsumer.accept(eldest.getKey(), eldest.getValue()); | |
65 | + removalConsumer.accept(eldest.getKey(), eldest.getValue()); | |
66 | 66 | return true; |
67 | 67 | } |
68 | 68 | } | ... | ... |
... | ... | @@ -37,6 +37,7 @@ import java.util.LinkedHashMap; |
37 | 37 | |
38 | 38 | import static org.hamcrest.CoreMatchers.instanceOf; |
39 | 39 | import static org.hamcrest.CoreMatchers.is; |
40 | +import static org.hamcrest.CoreMatchers.notNullValue; | |
40 | 41 | import static org.hamcrest.MatcherAssert.assertThat; |
41 | 42 | |
42 | 43 | public class LinkedHashMapRemoveEldestTest { |
... | ... | @@ -44,7 +45,7 @@ public class LinkedHashMapRemoveEldestTest { |
44 | 45 | public static final long MAX_ENTRIES = 10L; |
45 | 46 | long removeCount = 0; |
46 | 47 | |
47 | - void removeConsumer(Long id, String name) { | |
48 | + void removalConsumer(Long id, String name) { | |
48 | 49 | removeCount++; |
49 | 50 | assertThat(id, is(Matchers.lessThan(MAX_ENTRIES))); |
50 | 51 | assertThat(name, is(id.toString())); |
... | ... | @@ -54,9 +55,10 @@ public class LinkedHashMapRemoveEldestTest { |
54 | 55 | public void givenMap_whenOverSized_thenVerifyRemovedEldest() { |
55 | 56 | //given |
56 | 57 | LinkedHashMapRemoveEldest<Long, String> map = |
57 | - new LinkedHashMapRemoveEldest<>(MAX_ENTRIES, this::removeConsumer); | |
58 | + new LinkedHashMapRemoveEldest<>(MAX_ENTRIES, this::removalConsumer); | |
58 | 59 | |
59 | 60 | assertThat(map.getMaxEntries(), is(MAX_ENTRIES)); |
61 | + assertThat(map.getRemovalConsumer(), notNullValue()); | |
60 | 62 | assertThat(map, instanceOf(LinkedHashMap.class)); |
61 | 63 | assertThat(map, instanceOf(LinkedHashMapRemoveEldest.class)); |
62 | 64 | assertThat(map.size(), is(0)); | ... | ... |