[concurrency-interest] ConcurrentMap.replace

Eric Zoerner eric.zoerner@gemstone.com
Fri, 05 Dec 2003 10:38:13 -0800


Actually, I do have a use case for the replace method to return the old value. 
In cache eviction, you need to replace an existing value with a NullObject, but 
you need the previous value, if there is one, in order to 1) know if it had been 
already evicted, in which case you might do nothing else; and 2) put the old 
value into an event object that is passed to a listener that is interested in 
the cache eviction and may want to know what the old value was. In any case, 
getting the old value at the time of replace is impossible without race 
conditions unless the replace method returns the old value.

Eric Zoerner wrote:
> Doug, thanks for adding the two-arg replace method, and I agree with 
> your analysis. However, I was expecting the two-arg replace method to 
> return the oldValue, not a boolean. I don't have a use case for why it 
> should return the oldValue, but it seems like it would provide more 
> information than a boolean. If the return value is null, then we know 
> the key didn't exist and the replace didn't happen (same as the current 
> return of false). Otherwise we get the object that was replaced, similar 
> to the return value from put and putIfAbsent. [Of course, if the Map 
> supports null values then there is ambiguity, but that is also true of 
> putIfAbsent].
> 
> - Eric
>