ReversibleCollection proposal

Peter Levart peter.levart at gmail.com
Sun Apr 18 08:26:18 UTC 2021


On 4/18/21 9:51 AM, Peter Levart wrote:
> public interface Collection<E> .... {
>         default Collection<E> reversed() { return this; }
>         default void addFirst(E e) { throw new 
> UnsupportedOperationException(); }
>         default void addLast(E e) { throw new 
> UnsupportedOperationException(); }
>         default E getFirst() { return iterator().next(); }
>         default E getLast() { return iterator().next(); }
>         default E removeFirst() { var i = iterator(); i.next(); 
> i.remove(); }
>         default E removeLast() { var i = iterator(); i.next(); 
> i.remove(); }
> }
>
>
> List, SortedSet and Deque would of course override them with 
> implementations as proposed. Would anything be wrong with above 
> implementations? 


Yeah, I forgot about the Queue. Would something like the following be 
too much throwing?


public interface Queue<E> ... {
         default Queue<E> reversed() { throw new 
UnsupportedOperationException(); }
         default void addFirst(E e) { throw new 
UnsupportedOperationException(); }
         default void addLast(E e) { add(e); }
         default E getFirst() { return element(); }
         default E getLast() { throw new UnsupportedOperationException(); }
         default E removeFirst() {  return remove(); }
         default E removeLast() { throw new 
UnsupportedOperationException(); }
}



Peter




More information about the core-libs-dev mailing list