commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael A. Smith" <...@apache.org>
Subject Re: [collection] Collections for primitives
Date Sat, 22 Jun 2002 20:56:58 GMT
On Sat, 22 Jun 2002, Ola Berg wrote:

> The collections for primitves has dangerous behaviour in the methods
> where you recieve an Object as an argument, where it just tries to
> cast the argument to an Integer (or Float etc). This will result in a
> ClassCastException, which could be appropriate somtimes, but not in
> all cases.
> 
> Shouldn\'t myIntList.contains( \"Foo\") result in false?

No.  The Collection.contains(Object) contract declares that a 
ClassCastException is thrown "if the type of the specified element is 
incompatible with this collection".  "Foo" is incompatible with an int 
collection.
http://java.sun.com/j2se/1.4/docs/api/java/util/Collection.html#contains(java.lang.Object)

This is not alted in the List contract.
http://java.sun.com/j2se/1.4/docs/api/java/util/List.html#contains(java.lang.Object)

> Shouldn\'t myIntList.[lastI | i]ndexOf( \"Foo\") result in -1?

No.  Same reasons as above.
http://java.sun.com/j2se/1.4/docs/api/java/util/List.html#indexOf(java.lang.Object)
http://java.sun.com/j2se/1.4/docs/api/java/util/List.html#lastIndexOf(java.lang.Object)
 
> Shouldn\'t myIntList.add( \"Foo\") result in false, indicating that
> the object couldn\'t be added?

No. Same reason as above.
http://java.sun.com/j2se/1.4/docs/api/java/util/List.html#add(java.lang.Object)

> And so on (remove() etc)?

No. Same reason as above.

> Should I fix this and send a patch to someone, or is it the desired
> behaviour? If it is desired, I could make javadoc comments about it
> instead.

The throwing of a ClassCastException may not seem "appropriate", but it 
is already documented in the Collection contract (and also the List 
contract and Map contract).  There is no reason to "fix" it because it 
follows the contract.  Added documentation restating the documentation 
in Collection and List is doable, but not necessary in my opinion.

regards,
michael


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message