Possible JSR-310 (date/time API) stream methods

Brian Goetz brian.goetz at oracle.com
Thu Mar 7 12:44:35 PST 2013

I think a useful mental model for "can I easily make a stream out of 
this" is "can I easily make an Iterator for this."  (Of course, there 
are other ways to construct a stream other than an Iterator.)

So if you are tempted to write methods that would return an array or a 
Collection, consider whether instead (or in addition) to return a Stream.

For the examples you give, making streams would be trivial.  Its just a 
question of whether its a good idea from the 310 perspective.

On 3/7/2013 2:57 PM, Stephen Colebourne wrote:
> This is a call for opinions as to whether JSR-310 should think about
> adding stream-based methods.
> It seems that the most likely use case is a stream of dates in a
> range, either sequential or according to some rule. We already have a
> "some rule" type concept in TemporalAdjuster, but that is general,
> rather than LocalDate specific, so I've looked at UnaryOperator. One
> possible pair of methods would be:
>   // methods on LocalDate
>   public Stream<LocalDate> datesUntil(LocalDate endDate) {
>     return datesUntil(endDate, d -> d.plusDays(1));
>   }
>   public Stream<LocalDate> datesUntil(LocalDate endDate,
> UnaryOperator<LocalDate> generator) {
>     // some code to generate a stream checking for being beyond the end date
>     // initial perusal suggests this might be a little complex...
>   }
>   // user code
> startDate.datesUntil(endDate, d -> d.plusWeeks(1));
> startDate.datesUntil(endDate, d -> d.with(nextHoliday()));
> Does the basic stream library support this kind of stuff without
> JSR-310 needing to do it?
> Does this look useful, or are there other/alternative more useful methods?
> Should it only be for dates, or other objects like LocalDateTime or
> ZonedDateTime?
> Would it justify its place in the library given users could just write
> a for loop?
> Any other thoughts?
> BTW, the following code wouldn't work as the user expects:
>   startDate.datesUntil(endDate, d -> d.plusMonths(1));
> (Jan31 + 1 month = Feb28 ... Feb28 + 1 month = March28!!)
> Stephen

More information about the lambda-dev mailing list