Why CHM.remove(key, value) returns false if the value is null rather than throws NPE?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Why CHM.remove(key, value) returns false if the value is null rather than throws NPE?

JSR166 Concurrency mailing list
It seems that ConcurrentHashMap.remove(key, value) is a unique method with such tolerance for null. Here is the change: http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java?r1=1.68&r2=1.69. Why it was decided to do this?

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Why CHM.remove(key, value) returns false if the value is null rather than throws NPE?

JSR166 Concurrency mailing list
On 12/15/18 10:28 AM, Roman Leventov via Concurrency-interest wrote:
> It seems that ConcurrentHashMap.remove(key, value) is a unique method
> with such tolerance for null. Here is the
> change: http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java?r1=1.68&r2=1.69.
> Why it was decided to do this?

That diff is from 2005, so probably no one remembers why this was
transiently done, but the current version throws NPE on null.

-Doug

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Why CHM.remove(key, value) returns false if the value is null rather than throws NPE?

JSR166 Concurrency mailing list
The current version only throws NPE on null *key*:

https://java-browser.yawk.at/java/11/java.base/java/util/concurrent/ConcurrentHashMap.java#1544

- Jonas

On 2018-12-15 18:59, Doug Lea via Concurrency-interest wrote:

> On 12/15/18 10:28 AM, Roman Leventov via Concurrency-interest wrote:
>> It seems that ConcurrentHashMap.remove(key, value) is a unique method
>> with such tolerance for null. Here is the
>> change: http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java?r1=1.68&r2=1.69.
>> Why it was decided to do this?
>
> That diff is from 2005, so probably no one remembers why this was
> transiently done, but the current version throws NPE on null.
>
> -Doug
>
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Why CHM.remove(key, value) returns false if the value is null rather than throws NPE?

JSR166 Concurrency mailing list
The Map interface permits null values, so (by LSP) CHM can't penalize if someone probes for one.

However, looking at the archives there was a period of several months in which a CHM iterator might return null:

But this was fixed in the Mustang release. (Does anyone still remember Mustang?)

On Sat, Dec 15, 2018 at 10:10 AM Jonas Konrad via Concurrency-interest <[hidden email]> wrote:
The current version only throws NPE on null *key*:

https://java-browser.yawk.at/java/11/java.base/java/util/concurrent/ConcurrentHashMap.java#1544

- Jonas

On 2018-12-15 18:59, Doug Lea via Concurrency-interest wrote:
> On 12/15/18 10:28 AM, Roman Leventov via Concurrency-interest wrote:
>> It seems that ConcurrentHashMap.remove(key, value) is a unique method
>> with such tolerance for null. Here is the
>> change: http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java?r1=1.68&r2=1.69.
>> Why it was decided to do this?
>
> That diff is from 2005, so probably no one remembers why this was
> transiently done, but the current version throws NPE on null.
>
> -Doug
>
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Why CHM.remove(key, value) returns false if the value is null rather than throws NPE?

JSR166 Concurrency mailing list
Map has provisions in its javadoc for maps that do not support null
values. Map.remove and ConcurrentMap.remove explicitly allow throwing
NPE if the map does not support null keys / values. Also note that this
is the same for null *keys* (Maps can support them optionally), but CHM
throws an NPE for those even though you could argue that it should just
return false for the same reason.

On the other hand the current behavior (NPE on key but not value) is the
most in-line with the pseudocode spec in Map.remove(O,O) -
CHM.containsKey would throw NPE with a null key, but the pseudocode will
not throw NPE with a null value.

- Jonas

On 2018-12-15 21:47, Joe Bowbeer wrote:

> The Map interface permits null values, so (by LSP) CHM can't penalize if
> someone probes for one.
>
> However, looking at the archives there was a period of several months in
> which a CHM iterator might return null:
>
> https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6312056
>
> But this was fixed in the Mustang release. (Does anyone still remember
> Mustang?)
>
> On Sat, Dec 15, 2018 at 10:10 AM Jonas Konrad via Concurrency-interest
> <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     The current version only throws NPE on null *key*:
>
>     https://java-browser.yawk.at/java/11/java.base/java/util/concurrent/ConcurrentHashMap.java#1544
>
>     - Jonas
>
>     On 2018-12-15 18:59, Doug Lea via Concurrency-interest wrote:
>      > On 12/15/18 10:28 AM, Roman Leventov via Concurrency-interest wrote:
>      >> It seems that ConcurrentHashMap.remove(key, value) is a unique
>     method
>      >> with such tolerance for null. Here is the
>      >> change:
>     http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java?r1=1.68&r2=1.69.
>      >> Why it was decided to do this?
>      >
>      > That diff is from 2005, so probably no one remembers why this was
>      > transiently done, but the current version throws NPE on null.
>      >
>      > -Doug
>      >
>      > _______________________________________________
>      > Concurrency-interest mailing list
>      > [hidden email]
>     <mailto:[hidden email]>
>      > http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>      >
>     _______________________________________________
>     Concurrency-interest mailing list
>     [hidden email]
>     <mailto:[hidden email]>
>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Why CHM.remove(key, value) returns false if the value is null rather than throws NPE?

JSR166 Concurrency mailing list
However, the pseudocode spec of replace(K, V, V) is similar (https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#replace-K-V-V-) and would also imply that false should be returned if the second argument is null. But CHM throws NPE if the second argument of replace(K, V, V) is null.

On Sun, 16 Dec 2018 at 09:48, Jonas Konrad via Concurrency-interest <[hidden email]> wrote:
Map has provisions in its javadoc for maps that do not support null
values. Map.remove and ConcurrentMap.remove explicitly allow throwing
NPE if the map does not support null keys / values. Also note that this
is the same for null *keys* (Maps can support them optionally), but CHM
throws an NPE for those even though you could argue that it should just
return false for the same reason.

On the other hand the current behavior (NPE on key but not value) is the
most in-line with the pseudocode spec in Map.remove(O,O) -
CHM.containsKey would throw NPE with a null key, but the pseudocode will
not throw NPE with a null value.

- Jonas

On 2018-12-15 21:47, Joe Bowbeer wrote:
> The Map interface permits null values, so (by LSP) CHM can't penalize if
> someone probes for one.
>
> However, looking at the archives there was a period of several months in
> which a CHM iterator might return null:
>
> https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6312056
>
> But this was fixed in the Mustang release. (Does anyone still remember
> Mustang?)
>
> On Sat, Dec 15, 2018 at 10:10 AM Jonas Konrad via Concurrency-interest
> <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     The current version only throws NPE on null *key*:
>
>     https://java-browser.yawk.at/java/11/java.base/java/util/concurrent/ConcurrentHashMap.java#1544
>
>     - Jonas
>
>     On 2018-12-15 18:59, Doug Lea via Concurrency-interest wrote:
>      > On 12/15/18 10:28 AM, Roman Leventov via Concurrency-interest wrote:
>      >> It seems that ConcurrentHashMap.remove(key, value) is a unique
>     method
>      >> with such tolerance for null. Here is the
>      >> change:
>     http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java?r1=1.68&r2=1.69.
>      >> Why it was decided to do this?
>      >
>      > That diff is from 2005, so probably no one remembers why this was
>      > transiently done, but the current version throws NPE on null.
>      >
>      > -Doug
>      >
>      > _______________________________________________
>      > Concurrency-interest mailing list
>      > [hidden email]
>     <mailto:[hidden email]>
>      > http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>      >
>     _______________________________________________
>     Concurrency-interest mailing list
>     [hidden email]
>     <mailto:[hidden email]>
>     http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest