RFR 8050819: Please add java.util.Stream.ofNullable(T object)

Paul Sandoz Paul.Sandoz at oracle.com
Wed Jan 28 17:09:30 UTC 2015


On Jan 27, 2015, at 11:49 PM, Stuart Marks <stuart.marks at oracle.com> wrote:

> 
> On 1/26/15 2:15 PM, Remi Forax wrote:
>> 
>> On 01/26/2015 07:42 PM, Alan Bateman wrote:
>>> On 26/01/2015 18:32, Paul Sandoz wrote:
>>>> On Jan 20, 2015, at 7:05 PM, Paul Sandoz <Paul.Sandoz at oracle.com> wrote:
>>>>> Hi,
>>>>> 
>>>>> http://cr.openjdk.java.net/~psandoz/jdk9/JDK-8050819-Stream-ofNullable/webrev/
>>>>> 
>>>>> Stream.ofNullable can make it easier to work fluently when streams are
>>>>> produced, for example within flatMap.
>>>>> 
>>>> So far there have been "null" reviews :-)
> 
> I think I saw an empty review fly by. :-)
> 
> One quick note on the tests: the test data factory is
> 
>    TestData.Factory.ofSupplier("[0, 1)", ...)
> 
> which would seem to me to contain a single value zero, but they all use one as the value. Not that it really matters, but I was puzzling over it.

Thanks, it should be "{1}", i updated locally (in multiple places in the test source file).


> 
>>> This looks okay to me, and the naming consistent with Optional.
>>> 
>>> -Alan.
>> 
>> I'm not sure this method pull it's own weight,
>> it can be written like this:
>>   Optional.ofNullable(t).stream()
> 
> It's certainly possible, but it seems like an unnecessary level of indirection. The need for something like this has come up multiple times, and it seems logical for it to be next to Stream.of() and Stream.empty().

I was sitting on the fence with this feature for a while. There are many ways this can be surfaced but having a concise way that is easily found does seem useful.


> 
>> I'm pretty sure that the JIT will remove the creation of the intermediary Optional
>> and adding a method in the JDK that considers that null is an acceptable value
>> is in my opinion the wrong signal to send.
> 
> I guess having a method like this makes it easier for people to deal with their possibly-null-producing code in a new streams world. Such null-producing code already exists. I guess you could say that providing this method encourages people to continue producing nulls, but everybody is already doing this today, so....
> 

I had the same view as Remi (mostly still do) with regards to the wrong signal, but there is stuff out there that returns null and when one finds oneself on the wrong side of that null it's rather annoying.

Paul.



More information about the core-libs-dev mailing list