Re: [threeten-develop] API simplicity; Period
Status: Alpha
Brought to you by:
scolebourne
From: Roger R. <Rog...@or...> - 2011-10-10 15:20:53
|
Hi Stephen, On 10/07/2011 06:50 PM, Stephen Colebourne wrote: > On 7 October 2011 20:11, Roger Riggs<Rog...@or...> wrote: ... >> The between() method belongs in LocalDate with the other operations >> on LocalDates. It makes more sense there and a matching between() >> using OffsetDates would be in the OffsetDate class. >> The between() method would complement the plus(), minus(), and with() >> operations. > Thats a poor API. Static factory methods should return the type of the > class on which they are called in general, or at least a related type. > You are proposing: > > Period p = LocalDate.between(date1, date2) > Period p = LocalDate.daysBetween(date1, date2) > Period p = LocalDate.weeksBetween(date1, date2) > Period p = LocalDate.monthsBetween(date1, date2) > Period p = LocalDate.yearsBetween(date1, date2) > .... > > which makes no sense to me. It should be clear that the distinguishing > factor in these methods is the period unit, thus the method should be > *on* the period unit, where only a single "between" method is needed. No, I should have more been more concrete, I was proposing a more fluent API, that takes advantage of the instances already available. Period p = date1.minus(date2); // returns the delta, just like other arithmetic operators Period p = date1.minus(date2).daysOnly(); // If the result should only have full days. Using "of" is not a very fluent usage since it is essentially going back to creating a new type from primitive types. It is necessary when creating objects from primitives and units/types. Roger |