DaveAKing / guava-libraries

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

Iterables.removeIfFromRandomAccessList(..) not working with Lists prohibiting duplicates #1596

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The List interface does not state that it must allow duplicate elements, it is 
only stated as the typical case:

"Unlike sets, lists typically allow duplicate elements. More formally, lists 
typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they 
typically allow multiple null elements if they allow null elements at all. It 
is not inconceivable that someone might wish to implement a list that prohibits 
duplicates, by throwing runtime exceptions when the user attempts to insert 
them, but we expect this usage to be rare." (java.util.List, JavaDoc).

When a list prohibits duplicates, 
com.google.common.collect.Iterables.removeIfFromRandomAccessList(List<T>, 
Predicate<? super T>) 
leads to exceptions, because an element temporarily may be contained muiltple 
times in the list. 
This is true in particular for EMF's 
org.eclipse.emf.common.util.AbstractEList<E>, which does not allow duplicates.

Since allowing of duplicates is not a hard constraint of the List interface, I 
would consider this a bug. In particular since this specialized method is 
called from com.google.common.collect.Iterables.removeIf(Iterable<T>, 
Predicate<? super T>), so the client has little control over what's going to 
happen.

Original issue reported on code.google.com by jens.von...@numberfour.eu on 29 Nov 2013 at 3:56

GoogleCodeExporter commented 9 years ago
One option might be to catch IAE in addition to UOE and fall back to the slower 
behavior if either is thrown. Since we're only setting elements in the list 
that are already in it, IAE would likely mean that duplicates were prohibited.

Original comment by cgdecker@google.com on 3 Dec 2013 at 5:02

GoogleCodeExporter commented 9 years ago
It looks like that would work for org.eclipse.emf.common.util.AbstractEList.

As an aside, looking through that class, it's violating the spec of List in its 
add(E) method at least. It returns false from add if the object is a duplicate 
and uniqueness is required, when it should throw IAE. List.add is specced to 
return true always unless an exception is thrown.

Original comment by cgdecker@google.com on 3 Dec 2013 at 5:09

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:10

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

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