mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

TransformingRandomAccessList throws unsupported operation exception when trying to call add method #1874

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi guys,

When you're just creating a list using the Lists class or by using a Spring 
Data method which uses guava internally , you won't be able to add any element 
to the list because the internal class (TransformingRandomAccessList) is 
extending AbstractList and doesn't override the add method (with 2 params). 
This means the super class add method will be called which throws by default 
UnsupportedOperationException. 
I mean yeah, you could wrap this into another list implementation but I still 
don't think this should be the default behaviour... it looks pretty ugly.

Cheers,
Iulian

Original issue reported on code.google.com by cicoron...@gmail.com on 1 Nov 2014 at 12:41

GoogleCodeExporter commented 9 years ago
This is definitely working as intended.  There's not really any sensible 
non-confusing semantics here: there's no way to add the element back to the 
original list, since we have no way of undoing the Function.  If we tried 
something hacky like having an internal List representing elements added onto 
the end, we'd still be unable to support List.add(int, E), which just seems 
weird if you're supporting some adds but not others.

The whole point of this method is to provide a *view*, not a copy: it's not 
supposed to hold onto any data of its own, just the original list and the 
Function.

Simply rejecting all mutations except removes is really the only 
self-consistent semantics possible here.

Original comment by lowas...@google.com on 1 Nov 2014 at 12:46

GoogleCodeExporter commented 9 years ago
Ok, I understand your point... just thinking that should at least be 
documented...

Original comment by cicoron...@gmail.com on 1 Nov 2014 at 4:00

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 5 Nov 2014 at 11:34