[ 
https://issues.apache.org/jira/browse/COLLECTIONS-531?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14012671#comment-14012671
 ] 

Thomas Neidhart commented on COLLECTIONS-531:
---------------------------------------------

Committed in r1598357.

I have verified that the change does not break b/c and it should be fine.

Additionally added a note to the javadoc that providing incompatible types, 
e.g. by casting, will result in a ClassCastException at runtime. This is 
probably a worthwhile distinction to the isEqualCollection(C, C) method which 
does support incompatible types (although will always return false for 
non-empty collections in such a case).

Thanks for the report!

> Generic Wildcards specified in CollectionUtils#isEqualCollection(Collection, 
> Collection, Equator) may throw ClassCastException in certain cases
> -----------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: COLLECTIONS-531
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-531
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Collection
>    Affects Versions: 4.0
>            Reporter: Dipanjan Laha
>            Priority: Minor
>             Fix For: 4.1
>
>         Attachments: IsEqualCollectionTest.java
>
>
> CollectionUtils#isEqualCollection(Collection, Collection, Equator) is defined 
> as
> {code}
> public static boolean isEqualCollection(final Collection<?> a, final 
> Collection<?> b, final Equator<?> equator) {
> ...
> }
> {code}
> This makes it possible to invoke it with a code like
> {code}
> public static class IntegerEquator implements Equator<Integer> {
>         public boolean equate(Integer o1, Integer o2) {
>             return o1.intValue() == o2.intValue();
>         }
>         public int hash(Integer o) {
>             return o.intValue();
>         }
>     }
>     @Test
>     public void test() {
>         List<Long> longList = Arrays.asList(1L, 2L);
>         List<Integer> intList = Arrays.asList(1, 2);
>         assertTrue(CollectionUtils.isEqualCollection(longList, intList, new 
> IntegerEquator()));
>     }
> {code}
> which compiles perfectly but throws a ClassCastException as Long cannot be 
> cast to an Integer. However, the generics should be defined such that this is 
> stopped at compile time itself.
> If we modify the method to something like
> {code}
> public static <E> boolean isEqualCollection(final Collection<? extends E> a, 
> final Collection<? extends E> b, final Equator<? super E> equator) {
> ...
> }
> {code}
> the above example would give a compile time error. imho we should modify this 
> method with bounded wildcards. I don't think this change would break any 
> existing binaries if the method is being used correctly, otherwise it is 
> probably throwing ClassCastExceptions anyway.
> Test case attached



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to