From b4d13b1d89ac7bc5e9615258bf9963bb7d545620 Mon Sep 17 00:00:00 2001 From: Chrimle Date: Sun, 8 Dec 2024 23:21:36 +0100 Subject: [PATCH] Use Java Generics for `MultiKeyMap`-keys as method parameters --- .../commons/collections4/map/MultiKeyMap.java | 58 +++++++++---------- .../collections4/map/MultiKeyMapTest.java | 24 ++++---- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/map/MultiKeyMap.java b/src/main/java/org/apache/commons/collections4/map/MultiKeyMap.java index 71e468f6fe..19cd48def4 100644 --- a/src/main/java/org/apache/commons/collections4/map/MultiKeyMap.java +++ b/src/main/java/org/apache/commons/collections4/map/MultiKeyMap.java @@ -162,7 +162,7 @@ public MultiKeyMap clone() { * @param key2 the second key * @return true if the map contains the key */ - public boolean containsKey(final Object key1, final Object key2) { + public boolean containsKey(final K key1, final K key2) { final int hashCode = hash(key1, key2); AbstractHashedMap.HashEntry, V> entry = decoratedHashEntry(hashCode); while (entry != null) { @@ -182,7 +182,7 @@ public boolean containsKey(final Object key1, final Object key2) { * @param key3 the third key * @return true if the map contains the key */ - public boolean containsKey(final Object key1, final Object key2, final Object key3) { + public boolean containsKey(final K key1, final K key2, final K key3) { final int hashCode = hash(key1, key2, key3); AbstractHashedMap.HashEntry, V> entry = decoratedHashEntry(hashCode); while (entry != null) { @@ -203,7 +203,7 @@ public boolean containsKey(final Object key1, final Object key2, final Object ke * @param key4 the fourth key * @return true if the map contains the key */ - public boolean containsKey(final Object key1, final Object key2, final Object key3, final Object key4) { + public boolean containsKey(final K key1, final K key2, final K key3, final K key4) { final int hashCode = hash(key1, key2, key3, key4); AbstractHashedMap.HashEntry, V> entry = decoratedHashEntry(hashCode); while (entry != null) { @@ -225,7 +225,7 @@ public boolean containsKey(final Object key1, final Object key2, final Object ke * @param key5 the fifth key * @return true if the map contains the key */ - public boolean containsKey(final Object key1, final Object key2, final Object key3, final Object key4, final Object key5) { + public boolean containsKey(final K key1, final K key2, final K key3, final K key4, final K key5) { final int hashCode = hash(key1, key2, key3, key4, key5); AbstractHashedMap.HashEntry, V> entry = decoratedHashEntry(hashCode); while (entry != null) { @@ -260,7 +260,7 @@ int decoratedHashIndex(final int hashCode) { * @param key2 the second key * @return the mapped value, null if no match */ - public V get(final Object key1, final Object key2) { + public V get(final K key1, final K key2) { final int hashCode = hash(key1, key2); AbstractHashedMap.HashEntry, V> entry = decoratedHashEntry(hashCode); while (entry != null) { @@ -280,7 +280,7 @@ public V get(final Object key1, final Object key2) { * @param key3 the third key * @return the mapped value, null if no match */ - public V get(final Object key1, final Object key2, final Object key3) { + public V get(final K key1, final K key2, final K key3) { final int hashCode = hash(key1, key2, key3); AbstractHashedMap.HashEntry, V> entry = decoratedHashEntry(hashCode); while (entry != null) { @@ -301,7 +301,7 @@ public V get(final Object key1, final Object key2, final Object key3) { * @param key4 the fourth key * @return the mapped value, null if no match */ - public V get(final Object key1, final Object key2, final Object key3, final Object key4) { + public V get(final K key1, final K key2, final K key3, final K key4) { final int hashCode = hash(key1, key2, key3, key4); AbstractHashedMap.HashEntry, V> entry = decoratedHashEntry(hashCode); while (entry != null) { @@ -323,7 +323,7 @@ public V get(final Object key1, final Object key2, final Object key3, final Obje * @param key5 the fifth key * @return the mapped value, null if no match */ - public V get(final Object key1, final Object key2, final Object key3, final Object key4, final Object key5) { + public V get(final K key1, final K key2, final K key3, final K key4, final K key5) { final int hashCode = hash(key1, key2, key3, key4, key5); AbstractHashedMap.HashEntry, V> entry = decoratedHashEntry(hashCode); while (entry != null) { @@ -342,7 +342,7 @@ public V get(final Object key1, final Object key2, final Object key3, final Obje * @param key2 the second key * @return the hash code */ - protected int hash(final Object key1, final Object key2) { + protected int hash(final K key1, final K key2) { int h = 0; if (key1 != null) { h ^= key1.hashCode(); @@ -365,7 +365,7 @@ protected int hash(final Object key1, final Object key2) { * @param key3 the third key * @return the hash code */ - protected int hash(final Object key1, final Object key2, final Object key3) { + protected int hash(final K key1, final K key2, final K key3) { int h = 0; if (key1 != null) { h ^= key1.hashCode(); @@ -392,7 +392,7 @@ protected int hash(final Object key1, final Object key2, final Object key3) { * @param key4 the fourth key * @return the hash code */ - protected int hash(final Object key1, final Object key2, final Object key3, final Object key4) { + protected int hash(final K key1, final K key2, final K key3, final K key4) { int h = 0; if (key1 != null) { h ^= key1.hashCode(); @@ -423,7 +423,7 @@ protected int hash(final Object key1, final Object key2, final Object key3, fina * @param key5 the fifth key * @return the hash code */ - protected int hash(final Object key1, final Object key2, final Object key3, final Object key4, final Object key5) { + protected int hash(final K key1, final K key2, final K key3, final K key4, final K key5) { int h = 0; if (key1 != null) { h ^= key1.hashCode(); @@ -456,7 +456,7 @@ protected int hash(final Object key1, final Object key2, final Object key3, fina * @return true if the key matches */ protected boolean isEqualKey(final AbstractHashedMap.HashEntry, V> entry, - final Object key1, final Object key2) { + final K key1, final K key2) { final MultiKey multi = entry.getKey(); return multi.size() == 2 && @@ -474,7 +474,7 @@ protected boolean isEqualKey(final AbstractHashedMap.HashEntry, V> entry, - final Object key1, final Object key2, final Object key3) { + final K key1, final K key2, final K key3) { final MultiKey multi = entry.getKey(); return multi.size() == 3 && @@ -494,7 +494,7 @@ protected boolean isEqualKey(final AbstractHashedMap.HashEntry, V> entry, - final Object key1, final Object key2, final Object key3, final Object key4) { + final K key1, final K key2, final K key3, final K key4) { final MultiKey multi = entry.getKey(); return multi.size() == 4 && @@ -516,7 +516,7 @@ protected boolean isEqualKey(final AbstractHashedMap.HashEntry, V> entry, - final Object key1, final Object key2, final Object key3, final Object key4, final Object key5) { + final K key1, final K key2, final K key3, final K key4, final K key5) { final MultiKey multi = entry.getKey(); return multi.size() == 5 && @@ -689,7 +689,7 @@ private void readObject(final ObjectInputStream in) throws IOException, ClassNot * @param key1 the first key * @return true if any elements were removed */ - public boolean removeAll(final Object key1) { + public boolean removeAll(final K key1) { boolean modified = false; final MapIterator, V> it = mapIterator(); while (it.hasNext()) { @@ -714,7 +714,7 @@ public boolean removeAll(final Object key1) { * @param key2 the second key * @return true if any elements were removed */ - public boolean removeAll(final Object key1, final Object key2) { + public boolean removeAll(final K key1, final K key2) { boolean modified = false; final MapIterator, V> it = mapIterator(); while (it.hasNext()) { @@ -741,7 +741,7 @@ public boolean removeAll(final Object key1, final Object key2) { * @param key3 the third key * @return true if any elements were removed */ - public boolean removeAll(final Object key1, final Object key2, final Object key3) { + public boolean removeAll(final K key1, final K key2, final K key3) { boolean modified = false; final MapIterator, V> it = mapIterator(); while (it.hasNext()) { @@ -770,7 +770,7 @@ public boolean removeAll(final Object key1, final Object key2, final Object key3 * @param key4 the fourth key * @return true if any elements were removed */ - public boolean removeAll(final Object key1, final Object key2, final Object key3, final Object key4) { + public boolean removeAll(final K key1, final K key2, final K key3, final K key4) { boolean modified = false; final MapIterator, V> it = mapIterator(); while (it.hasNext()) { @@ -793,9 +793,9 @@ public boolean removeAll(final Object key1, final Object key2, final Object key3 * @param key1 the first key * @param key2 the second key * @return the value mapped to the removed key, null if key not in map - * @since 4.0 (previous name: remove(Object, Object)) + * @since 4.0 (previous name: remove(K, K)) */ - public V removeMultiKey(final Object key1, final Object key2) { + public V removeMultiKey(final K key1, final K key2) { final int hashCode = hash(key1, key2); final int index = decoratedHashIndex(hashCode); AbstractHashedMap.HashEntry, V> entry = decorated().data[index]; @@ -819,9 +819,9 @@ public V removeMultiKey(final Object key1, final Object key2) { * @param key2 the second key * @param key3 the third key * @return the value mapped to the removed key, null if key not in map - * @since 4.0 (previous name: remove(Object, Object, Object)) + * @since 4.0 (previous name: remove(K, K, K)) */ - public V removeMultiKey(final Object key1, final Object key2, final Object key3) { + public V removeMultiKey(final K key1, final K key2, final K key3) { final int hashCode = hash(key1, key2, key3); final int index = decoratedHashIndex(hashCode); AbstractHashedMap.HashEntry, V> entry = decorated().data[index]; @@ -846,9 +846,9 @@ public V removeMultiKey(final Object key1, final Object key2, final Object key3) * @param key3 the third key * @param key4 the fourth key * @return the value mapped to the removed key, null if key not in map - * @since 4.0 (previous name: remove(Object, Object, Object, Object)) + * @since 4.0 (previous name: remove(K, K, K, K)) */ - public V removeMultiKey(final Object key1, final Object key2, final Object key3, final Object key4) { + public V removeMultiKey(final K key1, final K key2, final K key3, final K key4) { final int hashCode = hash(key1, key2, key3, key4); final int index = decoratedHashIndex(hashCode); AbstractHashedMap.HashEntry, V> entry = decorated().data[index]; @@ -874,10 +874,10 @@ public V removeMultiKey(final Object key1, final Object key2, final Object key3, * @param key4 the fourth key * @param key5 the fifth key * @return the value mapped to the removed key, null if key not in map - * @since 4.0 (previous name: remove(Object, Object, Object, Object, Object)) + * @since 4.0 (previous name: remove(K, K, K, K, K)) */ - public V removeMultiKey(final Object key1, final Object key2, final Object key3, - final Object key4, final Object key5) { + public V removeMultiKey(final K key1, final K key2, final K key3, + final K key4, final K key5) { final int hashCode = hash(key1, key2, key3, key4, key5); final int index = decoratedHashIndex(hashCode); AbstractHashedMap.HashEntry, V> entry = decorated().data[index]; diff --git a/src/test/java/org/apache/commons/collections4/map/MultiKeyMapTest.java b/src/test/java/org/apache/commons/collections4/map/MultiKeyMapTest.java index 456269a275..34d9d0cd61 100644 --- a/src/test/java/org/apache/commons/collections4/map/MultiKeyMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/MultiKeyMapTest.java @@ -172,19 +172,19 @@ public void testLRUMultiKeyMap() { assertEquals(2, map.size()); map.put((K) I1, (K) I4, (V) "1-4"); assertEquals(2, map.size()); - assertTrue(map.containsKey(I1, I3)); - assertTrue(map.containsKey(I1, I4)); - assertFalse(map.containsKey(I1, I2)); + assertTrue(map.containsKey((K) I1,(K) I3)); + assertTrue(map.containsKey((K) I1,(K) I4)); + assertFalse(map.containsKey((K) I1, (K) I2)); final MultiKeyMap cloned = map.clone(); assertEquals(2, map.size()); - assertTrue(cloned.containsKey(I1, I3)); - assertTrue(cloned.containsKey(I1, I4)); - assertFalse(cloned.containsKey(I1, I2)); + assertTrue(cloned.containsKey((K) I1, (K) I3)); + assertTrue(cloned.containsKey((K) I1, (K) I4)); + assertFalse(cloned.containsKey((K) I1, (K) I2)); cloned.put((K) I1, (K) I5, (V) "1-5"); assertEquals(2, cloned.size()); - assertTrue(cloned.containsKey(I1, I4)); - assertTrue(cloned.containsKey(I1, I5)); + assertTrue(cloned.containsKey((K) I1, (K) I4)); + assertTrue(cloned.containsKey((K) I1, (K) I5)); } @Test @@ -428,7 +428,7 @@ public void testMultiKeyRemoveAll1() { final MultiKeyMap multimap = getMap(); assertEquals(12, multimap.size()); - multimap.removeAll(I1); + multimap.removeAll((K) I1); assertEquals(8, multimap.size()); for (final MapIterator, V> it = multimap.mapIterator(); it.hasNext();) { final MultiKey key = it.next(); @@ -442,7 +442,7 @@ public void testMultiKeyRemoveAll2() { final MultiKeyMap multimap = getMap(); assertEquals(12, multimap.size()); - multimap.removeAll(I2, I3); + multimap.removeAll((K) I2, (K) I3); assertEquals(9, multimap.size()); for (final MapIterator, V> it = multimap.mapIterator(); it.hasNext();) { final MultiKey key = it.next(); @@ -456,7 +456,7 @@ public void testMultiKeyRemoveAll3() { final MultiKeyMap multimap = getMap(); assertEquals(12, multimap.size()); - multimap.removeAll(I1, I1, I2); + multimap.removeAll((K) I1, (K) I1, (K) I2); assertEquals(9, multimap.size()); for (final MapIterator, V> it = multimap.mapIterator(); it.hasNext();) { final MultiKey key = it.next(); @@ -481,7 +481,7 @@ public void testMultiKeyRemoveAll4() { final MultiKeyMap multimap = getMap(); assertEquals(12, multimap.size()); - multimap.removeAll(I1, I1, I2, I3); + multimap.removeAll((K) I1, (K) I1, (K) I2, (K) I3); assertEquals(10, multimap.size()); for (final MapIterator, V> it = multimap.mapIterator(); it.hasNext();) { final MultiKey key = it.next();