Permalink
Browse files

Merge pull request #202 from henri-tremblay/equals

ConcurrentMap contract requires to give a result based on equals, not on reference equality
  • Loading branch information...
nitsanw committed Nov 17, 2017
2 parents b36bdd0 + fe6d72a commit a2191c1e0243c8cd4690990d6b250b26a3ca8bfa
@@ -340,7 +340,9 @@ private final void initialize( int initial_sz ) {
/** Atomically do a {@link #remove(Object)} if-and-only-if the key is mapped
* to a value which is <code>equals</code> to the given value.
* @throws NullPointerException if the specified key or value is null */
public boolean remove ( Object key,Object val ) { return putIfMatch( key,TOMBSTONE, val ) == val; }
public boolean remove ( Object key,Object val ) {
return Objects.equals(putIfMatch( key,TOMBSTONE, val ), val);
}
/** Atomically do a <code>put(key,val)</code> if-and-only-if the key is
* mapped to some value already.
@@ -353,7 +355,7 @@ private final void initialize( int initial_sz ) {
* @throws NullPointerException if the specified key or value is null */
@Override
public boolean replace ( TypeK key, TypeV oldValue, TypeV newValue ) {
return putIfMatch( key, newValue, oldValue ) == oldValue;
return Objects.equals(putIfMatch( key, newValue, oldValue ), oldValue);
}
@@ -483,6 +483,26 @@ public void testNonBlockingHashMapIteratorMultithreaded() throws InterruptedExce
assertEquals("values().iterator() count", itemCount, iteratorCount);
}
// --- Tests on equality of values
@Test
public void replaceResultIsBasedOnEquality() {
NonBlockingHashMap<Integer, Integer> map = new NonBlockingHashMap<>();
Integer initialValue = new Integer(10);
map.put(1, initialValue);
assertTrue(map.replace(1, initialValue, 20));
assertTrue(map.replace(1, new Integer(20), 30));
}
@Test
public void removeResultIsBasedOnEquality() {
NonBlockingHashMap<Integer, Integer> map = new NonBlockingHashMap<>();
Integer initialValue = new Integer(10);
map.put(1, initialValue);
assertTrue(map.remove(1, initialValue));
map.put(1, initialValue);
assertTrue(map.remove(1, new Integer(10)));
}
// Throw a ClassCastException if I see a tombstone during key-compares
private static class KeyBonk
{

0 comments on commit a2191c1

Please sign in to comment.