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

COLLECTIONS-803: prevent duplicate call to convertKey on put for CaseInsensitiveMap #276

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,70 @@ protected Object convertKey(final Object key) {
return AbstractHashedMap.NULL;
}

/**
Simulant87 marked this conversation as resolved.
Show resolved Hide resolved
* Puts a key-value mapping into this map.
*
* @param key the key to add
* @param value the value to add
* @return the value previously mapped to this key, null if none
*/
@Override
public V put(final K key, final V value) {
final Object convertedKey = convertKey(key);
final int hashCode = hash(convertedKey);
final int index = hashIndex(hashCode, data.length);
HashEntry<K, V> entry = data[index];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(convertedKey, entry.key)) {
final V oldValue = entry.getValue();
updateEntry(entry, value);
return oldValue;
}
entry = entry.next;
}

addMappingWithoutKeyConversion(index, hashCode, convertedKey, value);
return null;
}

/**
* Adds a new key-value mapping into this map.
* <p>
* This implementation calls {@code createEntry()}, {@code addEntry()}
Simulant87 marked this conversation as resolved.
Show resolved Hide resolved
* and {@code checkCapacity()}.
* It also handles changes to {@code modCount} and {@code size}.
* Subclasses could override to fully control adds to the map.
*
* @param hashIndex the index into the data array to store at
* @param hashCode the hash code of the key to add
* @param key the key to add
* @param value the value to add
*/
private void addMappingWithoutKeyConversion(final int hashIndex, final int hashCode, final Object key, final V value) {
modCount++;
final HashEntry<K, V> entry = createEntryWithoutKeyConversion(data[hashIndex], hashCode, key, value);
addEntry(entry, hashIndex);
size++;
checkCapacity();
}

/**
* Creates an entry to store the key-value data.
* <p>
* This implementation creates a new HashEntry instance.
* Subclasses can override this to return a different storage class,
Simulant87 marked this conversation as resolved.
Show resolved Hide resolved
* or implement caching.
*
* @param next the next entry in sequence
* @param hashCode the hash code to use
* @param key the key to store
* @param value the value to store
* @return the newly created entry
*/
private HashEntry<K, V> createEntryWithoutKeyConversion(final HashEntry<K, V> next, final int hashCode, final Object key, final V value) {
return new HashEntry<>(next, hashCode, key, value);
}

/**
* Clones the map without cloning the keys or values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.Set;

import org.easymock.EasyMock;
import org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -147,4 +148,20 @@ public void testPutAll() {
|| !caseInsensitiveMap.containsValue("Three")); // ones collapsed
assertEquals("Four", caseInsensitiveMap.get(null));
}

// COLLECTIONS-803
@Test
@SuppressWarnings("unchecked")
public void testPutConvertKeyOnlyOnce() {
CaseInsensitiveMap mock = EasyMock.partialMockBuilder(CaseInsensitiveMap.class)
Simulant87 marked this conversation as resolved.
Show resolved Hide resolved
Simulant87 marked this conversation as resolved.
Show resolved Hide resolved
.addMockedMethod("convertKey")
.createMock();
mock.data = new AbstractHashedMap.HashEntry[16];
EasyMock.expect(mock.convertKey("Key")).andReturn("key").once();
EasyMock.replay(mock);

mock.put("Key", "value");

EasyMock.verify(mock);
}
}