rzel / concurrentlinkedhashmap

Automatically exported from code.google.com/p/concurrentlinkedhashmap
Apache License 2.0
0 stars 0 forks source link

Identity problem #10

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
java.util.concurrent.ConcurrentMap api:

boolean remove(Object key,Object value)
Remove entry for key only if currently mapped to given value. Acts as 

  if ((map.containsKey(key) && map.get(key).equals(value)) {
     map.remove(key);
     return true;
 } else return false;

can the parameter value be a Identity ?

Original issue reported on code.google.com by xxiaozai...@gmail.com on 15 Sep 2009 at 9:01

GoogleCodeExporter commented 9 years ago
Hi xxiaozaichu,

Sorry for the long delay, but I will try to become active again. Life keeps you 
scrambling to keep up. :)

Thanks for reporting the issue, but alas its not a bug!

The Identity was introduced because #containsValue() needs to perform an 
equality 
check. I saw that ConcurrentHashMap's implementation locks the entire 
hashtable, 
commenting that it was done due to the ABA problem. While I understood the 
comment, 
I didn't see why it would be a concern. So instead of using an iterator I 
introduced 
the Identity to conform to the implementation semantics. I plan to weaken it by 
using an Iterator<V> and allow for the ABA problem now that I feel more 
comfortable 
with it.

The Identity is just a pass through for reference checks. The #remove(key, 
value) 
returns back a boolean on success. However, I need the Node<K, V> instance in 
order 
to unlink it from the list. So instead I get the instance, perform the value 
check, 
and try to remove it conditionally. If successful, I can unlink the node.

You are welcome to look at the unit tests to validate that this works 
correctly. 
Please let me know if you see any flaw in my logic.

Original comment by Ben.Manes@gmail.com on 20 Oct 2009 at 8:05