You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Thomas Neidhart (JIRA)" <ji...@apache.org> on 2013/03/10 21:41:12 UTC

[jira] [Updated] (COLLECTIONS-424) Surprising exception by CompositeSet in a situation where CompositeCollection works fine

     [ https://issues.apache.org/jira/browse/COLLECTIONS-424?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Thomas Neidhart updated COLLECTIONS-424:
----------------------------------------

    Attachment: COLLECTIONS-424.patch

The patch removes the inheritance to CompositeCollection and cleans up the code with everything that does not make sense for a CompositeSet.

We also can not extend a composite set / collection with collections containing subtypes (e.g. Set<? extends E>) as this may lead to undefined behavior.

Consider the following case:

class A
class B extends A

a CompositeSet set which is composed of a Set<A> and a Set<B>

if set.add(A) would be called, the mutator could decide to add A to the Set<B> which in turn will lead to runtime errors.
                
> Surprising exception by CompositeSet in a situation where CompositeCollection works fine
> ----------------------------------------------------------------------------------------
>
>                 Key: COLLECTIONS-424
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-424
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Set
>    Affects Versions: 3.2.1
>         Environment: All environments
>            Reporter: Michael Pradel
>         Attachments: collections424.patch, COLLECTIONS-424.patch
>
>
> We have a method that uses a CompositeCollection. Here's a simplified version of it:
>   void m(CompositeCollection coll) {
>     coll.addComposited(new TreeBag());
>   }
> It works fine when the argument is a CompositeCollection, but it throws an exception when the argument is a CompositeSet. E.g.:
>   m(new CompositeCollection());  // OK
>   m(new CompositeSet());         // IllegalArgumentException
> Although the exception is documented in CompositeSet, this behavior is very surprising. Is there a way to have m() accept CompositeCollections without running into this exception? The only solution that comes to my mind is to dynamically check the type of 'coll' in m(), but this is a rather nasty work-around.
> A better solution may be to make the genericity of CompositeCollection explicit by adding a type parameter:
>   class CompositeCollection<T extends Collection> {
>     void addComposited(T c) { /* .. */ }
>   }
> 	
>   class CompositeSet extends CompositeCollection<Set> {
>     @Override void addComposited(Set c) { /* .. */ }
>   }
> This way, users of CompositeCollection must choose the kind of collections that can be composed and will not encounter surprises, such as the above.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira