Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Java Generics for MultiKeyMap-keys as method parameters #581

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions src/main/java/org/apache/commons/collections4/map/MultiKeyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public MultiKeyMap<K, V> 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<MultiKey<? extends K>, V> entry = decoratedHashEntry(hashCode);
while (entry != null) {
Expand All @@ -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<MultiKey<? extends K>, V> entry = decoratedHashEntry(hashCode);
while (entry != null) {
Expand All @@ -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<MultiKey<? extends K>, V> entry = decoratedHashEntry(hashCode);
while (entry != null) {
Expand All @@ -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<MultiKey<? extends K>, V> entry = decoratedHashEntry(hashCode);
while (entry != null) {
Expand Down Expand Up @@ -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<MultiKey<? extends K>, V> entry = decoratedHashEntry(hashCode);
while (entry != null) {
Expand All @@ -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<MultiKey<? extends K>, V> entry = decoratedHashEntry(hashCode);
while (entry != null) {
Expand All @@ -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<MultiKey<? extends K>, V> entry = decoratedHashEntry(hashCode);
while (entry != null) {
Expand All @@ -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<MultiKey<? extends K>, V> entry = decoratedHashEntry(hashCode);
while (entry != null) {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<MultiKey<? extends K>, V> entry,
final Object key1, final Object key2) {
final K key1, final K key2) {
final MultiKey<? extends K> multi = entry.getKey();
return
multi.size() == 2 &&
Expand All @@ -474,7 +474,7 @@ protected boolean isEqualKey(final AbstractHashedMap.HashEntry<MultiKey<? extend
* @return true if the key matches
*/
protected boolean isEqualKey(final AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry,
final Object key1, final Object key2, final Object key3) {
final K key1, final K key2, final K key3) {
final MultiKey<? extends K> multi = entry.getKey();
return
multi.size() == 3 &&
Expand All @@ -494,7 +494,7 @@ protected boolean isEqualKey(final AbstractHashedMap.HashEntry<MultiKey<? extend
* @return true if the key matches
*/
protected boolean isEqualKey(final AbstractHashedMap.HashEntry<MultiKey<? extends K>, 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<? extends K> multi = entry.getKey();
return
multi.size() == 4 &&
Expand All @@ -516,7 +516,7 @@ protected boolean isEqualKey(final AbstractHashedMap.HashEntry<MultiKey<? extend
* @return true if the key matches
*/
protected boolean isEqualKey(final AbstractHashedMap.HashEntry<MultiKey<? extends K>, 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<? extends K> multi = entry.getKey();
return
multi.size() == 5 &&
Expand Down Expand Up @@ -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<MultiKey<? extends K>, V> it = mapIterator();
while (it.hasNext()) {
Expand All @@ -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<MultiKey<? extends K>, V> it = mapIterator();
while (it.hasNext()) {
Expand All @@ -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<MultiKey<? extends K>, V> it = mapIterator();
while (it.hasNext()) {
Expand Down Expand Up @@ -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<MultiKey<? extends K>, V> it = mapIterator();
while (it.hasNext()) {
Expand All @@ -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<MultiKey<? extends K>, V> entry = decorated().data[index];
Expand All @@ -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<MultiKey<? extends K>, V> entry = decorated().data[index];
Expand All @@ -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<MultiKey<? extends K>, V> entry = decorated().data[index];
Expand All @@ -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<MultiKey<? extends K>, V> entry = decorated().data[index];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V> 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
Expand Down Expand Up @@ -428,7 +428,7 @@ public void testMultiKeyRemoveAll1() {
final MultiKeyMap<K, V> multimap = getMap();
assertEquals(12, multimap.size());

multimap.removeAll(I1);
multimap.removeAll((K) I1);
assertEquals(8, multimap.size());
for (final MapIterator<MultiKey<? extends K>, V> it = multimap.mapIterator(); it.hasNext();) {
final MultiKey<? extends K> key = it.next();
Expand All @@ -442,7 +442,7 @@ public void testMultiKeyRemoveAll2() {
final MultiKeyMap<K, V> multimap = getMap();
assertEquals(12, multimap.size());

multimap.removeAll(I2, I3);
multimap.removeAll((K) I2, (K) I3);
assertEquals(9, multimap.size());
for (final MapIterator<MultiKey<? extends K>, V> it = multimap.mapIterator(); it.hasNext();) {
final MultiKey<? extends K> key = it.next();
Expand All @@ -456,7 +456,7 @@ public void testMultiKeyRemoveAll3() {
final MultiKeyMap<K, V> 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<MultiKey<? extends K>, V> it = multimap.mapIterator(); it.hasNext();) {
final MultiKey<? extends K> key = it.next();
Expand All @@ -481,7 +481,7 @@ public void testMultiKeyRemoveAll4() {
final MultiKeyMap<K, V> 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<MultiKey<? extends K>, V> it = multimap.mapIterator(); it.hasNext();) {
final MultiKey<? extends K> key = it.next();
Expand Down
Loading