Hello!

Thank you for your comments. I logged an issue:
https://bugs.openjdk.java.net/browse/JDK-8146218

And prepared a patch:
http://amaembo.github.io/cr/8146218/webrev-1/
(sorry for not using official code-review server, I cannot access it
currently, hopefully this problem will be solved soon). Please review,
especially the specification: English is not my native language.

The patch introduces two methods in LocalDate class:
datesUntil(LocalDate endExclusive) and
datesUntil(LocalDate endExclusive, Period step).

The implementation of the former one is just a one-liner. I decided to
produce an empty stream if endExclusive is before than this to stay in
sync with IntStream.range/LongStream.range.

The latter method is more tricky. It could be implemented using the
takeWhile() method, but I wanted to make the sized stream, so it can
parallelize perfectly and its count method is guaranteed to work in
constant time (see JDK-8067969). Thus it's necessary to precompute
exactly the size of the stream.

Zero and negative periods are not supported. On the one hand it might
be useful in some scenarios (assuming the ending date is before the
start date). On the other hand it's possible to define some weird
period like Period.of(0,1,-30) which may go in both directions
depending on current date. Period like Period.of(400,0,-146097) is
equivalent to ZERO, so it would never advance, but isZero would return
false (and even normalization will not help). Theoretically it's
possible to handle all such cases, but it would make the method and
its specification much more complex. Thus I would like to disable
negative periods at all. Though I'm open to discussion.

Probably I added too many test cases. If it poses the problem I may
try to reduce the number of tests.

With best regards,
Tagir Valeev.

SC> JSR-310 was developed in parallel with the Stream API. While we
SC> briefly considered adding some stream methods, it wasn't obvious what
SC> would be of value.

SC> A key question is whether the methods are either specific to a narrow
SC> use case or more general. Specific methods like months() on Year are
SC> very limited. You'd need:
SC>  Year::months()
SC>  Year::dates()
SC>  YearMonth::dates()
SC> Only three methods, so not too bad. There would be some use cases, but
SC> enough to include them? Not sure.

SC> A method that provides all days between two dates:
SC>  Stream<LocalDate> datesUntil(LocalDate endExclusive);
SC> would have some value as it is more common and trickier to write.

SC> A method that provides Period addition looping might make more sense:
SC>  Stream<LocalDate> datesUntil(LocalDate endExclusive, Period step);
SC> This is actually quite hard to write correctly, as it requires
SC> multiplying the period by the loop index, not adding the period each
SC> time (to handle end-of-month effects).

SC> But where to stop?
SC>  Stream<Instant> instantsUntil(Instant endExclusive, Duration step);
SC>  Stream<ZonedDateTime> instantsUntil(Instant endExclusive, Duration step);
SC>  Stream<OffsetDateTime> instantsUntil(Instant endExclusive, Duration step);
SC> ...


SC> I think the place to start is the two methods on LocalDate which are
SC> tricky enough to be justified. Then perhaps the methods on
SC> Year/YearMonth.

SC> Stephen


SC> On 10 December 2015 at 16:31, Tagir F. Valeev <amae...@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.
>>

Reply via email to