On Saturday, April 17, 2021 11:48 CEST, Stephen Colebourne <scolebou...@joda.org> wrote:
> On Fri, 16 Apr 2021 at 18:41, Stuart Marks <stuart.ma...@oracle.com> wrote: > > This is a proposal to add a ReversibleCollection interface to the > > Collections > > Framework. I'm looking for comments on overall design before I work on > > detailed > > specifications and tests. Please send such comments as replies on this > > email thread. > > I think this could be an interesting addition to the framework. > > > # Ordering and Reversibility > > Reading this section, it seems like ordering is a more significant > quality than reversibility. Yet the API is named "reversible". That > seems odd, esepcially given the stream characteristic. Since `reversible = ordered + sized`, using reversible makes sense imho (even though in the context of the Collections Framework, reversible <-> ordered, since all collections are sized). Also, I think "reversible" is much less likely to clash with existing code (e.g. a quick GitHub search shows 1 result for `ReversibleCollection`, while there's plenty for `OrderedCollection`). > > > SortedSet::addFirst and addLast throw UnsupportedOperationException. This > > is because > > SortedSet's ordering is determined by the comparison method and cannot be > > controlled > > explicitly. > > This seems undesirable. Maybe SortedSet should not implement > reversible/ordered? Maybe they should add to the set but validate > whether they would be in first/last position? Simply allowing users to > get a new instance with a different (eg. reversed) comparator would > meet much of the use case. I'd argue throwing UOE is the right thing to do. Not having `SortedSet` implement `ReversibleSet` doesn't make sense to me. Adding with validation is reasonable in itself, but then you'd have to specify `IllegalArgumentException` in `ReversibleCollection` (where you'd have a hard time specifying the conditions under which it might be thrown without explicitly referencing `SortedSet`), just to accommodate these 2 methods which would be very rarely used. > > Also, SortedSet uses first() and last(), yet the proposed interface > uses getFirst() and getLast(). Since `Deque` uses `getFirst()` and `getLast()`, it's impossible to match all existing methods in the different interfaces. > > Stephen Kind regards, Anthony