RFR 9: 8138963 : java.lang.Objects new method to default to non-null
Roger Riggs
Roger.Riggs at Oracle.com
Tue Oct 6 14:23:15 UTC 2015
HI Paul,
It simplifies code that looks like this:
followRedirects = builder.followRedirects == null ?
Redirect.NEVER : builder.followRedirects;
To:
followRedirects = Objects.nonNullOrElse(builder.followRedirects,
Redirect.NEVER);
It makes the source code more concise by not repeating the expression
twice and
avoids evaluating the expression more than once.
Roger
On 10/6/2015 10:11 AM, Paul Benedict wrote:
> If the second value is allowed to be null, then I don't find this new
> method useful. What value do you see in it? It doesn't provide any
> extra functionality to a simple if-else/ternary test, right? I throw
> my lot in with Stephen that the return value must be non-null.
>
>
> Cheers,
> Paul
>
> On Tue, Oct 6, 2015 at 9:06 AM, Roger Riggs <Roger.Riggs at oracle.com
> <mailto:Roger.Riggs at oracle.com>> wrote:
>
> Hi Paul,
>
> Hence the question about the method name.
> It already documents explicitly that the nullDefault value is
> returned and does not restrict the value.
>
> To be a direct replacement for the current code, it should return
> the 2nd value regardless of whether it is null or not.
>
> Roger
>
>
>
>
> On 10/6/2015 9:56 AM, Paul Benedict wrote:
>> It's quite possible for the second argument to be null. Is that
>> your intention? I am not sure it makes sense, but it's not
>> harmful either. I recommend you can either (1) explicitly
>> document that's a possibility and this method could still return
>> null or (2) prevent it by calling requireNonNull.
>>
>>
>> Cheers,
>> Paul
>>
>> On Tue, Oct 6, 2015 at 8:43 AM, Roger Riggs
>> <Roger.Riggs at oracle.com <mailto:Roger.Riggs at oracle.com>> wrote:
>>
>> Java.lang.Objects contains a number of convenience methods to
>> make it easier to handle references that are null.
>> For example, toString(obj, nullDefault),
>>
>> A new method is proposed to return the reference or a default
>> value if the reference is null.
>> static <T> T nonNull(T obj, T nullDefault);
>>
>> Alternatives to the method name include
>> nonNullOrElse ( using the java.util.Optional name pattern) or
>> nonNullOrDefault
>>
>> Please review and comment.
>>
>> Webrev:
>> http://cr.openjdk.java.net/~rriggs/webrev-object-non-null/
>> <http://cr.openjdk.java.net/%7Erriggs/webrev-object-non-null/>
>>
>> Issue:
>> https://bugs.openjdk.java.net/browse/JDK-8138963
>>
>> Thanks, Roger
>>
>>
>
>
More information about the core-libs-dev
mailing list