Objects.nonNull() (original) (raw)

Rémi Forax forax at univ-mlv.fr
Fri Jan 14 00:27:38 UTC 2011


On 01/14/2011 01:20 AM, mark.reinhold at oracle.com wrote:

Date: Thu, 13 Jan 2011 18:15:30 -0500 From: brian.goetz at oracle.com ...

Between checkNonNull() and throwIfNull(), I lean towards the former. In any case the answer should be driven by what is more obvious to the reader. Agreed. So let's look at some typical code: public Moo fooWrapper(String x, String y) { return foo(throwIfNull(x), throwIfNull(y)); } vs public Moo fooWrapper(String x, String y) { return foo(checkNonNull(x), checkNonNull(y)); } Since throwing should be the exceptional path, it feels to me that having throw in the name sets slightly the wrong expectations. ... Agreed. I'm still troubled by the "check" prefix, though. It implies that the named condition will be tested but it doesn't clearly relate the result of that test to the method's exception-throwing behavior. Here's an idea: Why not treat this as a (degenerate) kind of conversion operation? Call it asNonNull(x) -- it (trivially) converts its argument to a non-null value, and if it can't then it throws an NPE. public Moo fooWrapper(String x, String y) { return foo(asNonNull(x), asNonNull(y)); } Of all the names we've considered, this looks the most natural to me. - Mark

'as' is used in C# or in Groovy to transform a value to another type.

There is also assertNonNull or claimNonNull.

Rémi



More information about the core-libs-dev mailing list