Producing streams in java.time?
Stephen Colebourne
scolebourne at joda.org
Wed Dec 23 15:20:06 UTC 2015
JSR-310 was developed in parallel with the Stream API. While we
briefly considered adding some stream methods, it wasn't obvious what
would be of value.
A key question is whether the methods are either specific to a narrow
use case or more general. Specific methods like months() on Year are
very limited. You'd need:
Year::months()
Year::dates()
YearMonth::dates()
Only three methods, so not too bad. There would be some use cases, but
enough to include them? Not sure.
A method that provides all days between two dates:
Stream<LocalDate> datesUntil(LocalDate endExclusive);
would have some value as it is more common and trickier to write.
A method that provides Period addition looping might make more sense:
Stream<LocalDate> datesUntil(LocalDate endExclusive, Period step);
This is actually quite hard to write correctly, as it requires
multiplying the period by the loop index, not adding the period each
time (to handle end-of-month effects).
But where to stop?
Stream<Instant> instantsUntil(Instant endExclusive, Duration step);
Stream<ZonedDateTime> instantsUntil(Instant endExclusive, Duration step);
Stream<OffsetDateTime> instantsUntil(Instant endExclusive, Duration step);
...
I think the place to start is the two methods on LocalDate which are
tricky enough to be justified. Then perhaps the methods on
Year/YearMonth.
Stephen
On 10 December 2015 at 16:31, Tagir F. Valeev <amaembo at gmail.com> wrote:
> - in class Year:
>
> // Returns sequential ordered stream of all months within this Year:
> Stream<YearMonth> months();
> // Returns sequential ordered stream of all days within this Year:
> Stream<LocalDate> days();
> // Returns sequential ordered stream of all years starting from this
> // Year until the supplied year, exclusive
> Stream<Year> yearsUntil(Temporal endExclusive);
>
> - in class YearMonth:
>
> // Returns sequential ordered stream of all days within this YearMonth:
> Stream<LocalDate> days();
> // Returns sequential ordered stream of all months starting from this
> // YearMonth until the supplied YearMonth, exclusive
> Stream<YearMonth> monthsUntil(Temporal endExclusive);
>
> - in class LocalDate:
>
> // Returns sequential ordered stream of all months starting from this
> // LocalDate until the supplied LocalDate, exclusive
> Stream<LocalDate> daysUntil(Temporal endExclusive);
>
> The implementation of these methods could be quite simple. For example:
>
> class Year {
> public Stream<LocalDate> days() {
> return IntStream.rangeClosed(1, length()).mapToObj(this::atDay);
> }
> }
>
> What do you think?
>
> With best regards,
> Tagir Valeev.
>
More information about the core-libs-dev
mailing list