commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Saff (JIRA)" <>
Subject [jira] Commented: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString
Date Tue, 11 Sep 2007 18:10:32 GMT


David Saff commented on COLLECTIONS-265:

It would prevent the exception, but I can think of two other possibilities that seem more
orthogonal to existing functionality:

1) A java.util.TreeSet will hold a single uncomparable element, and treat it the same as a
single comparable element, until a second element is added.  TreeBag could do the same.  
2) Or, TreeBag could gripe on add, just like both TreeBag and TreeSet do on the addition of
a second incomparable object:

		TreeBag bag = new TreeBag();
		Object object = new Object();
		bag.add(object);  // exception thrown here

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>                 Key: COLLECTIONS-265
>                 URL:
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Bag
>    Affects Versions: 3.2
>            Reporter: David Saff
>            Priority: Minor
> The following code throws an exception not when the Object is added, but when toString
is called:
> 		TreeBag bag = new TreeBag();
> 		bag.add(new Object());
> 		bag.toString();
> Trace:
> java.lang.ClassCastException: java.lang.Object
> 	at
> 	at java.util.TreeMap.getEntry(
> 	at java.util.TreeMap.get(
> 	at org.apache.commons.collections.bag.AbstractMapBag.getCount(
> 	at org.apache.commons.collections.bag.AbstractMapBag.toString(
> [...]
> In a client program, toString should never throw an exception--it makes debugging much
harder, for one thing.  I believe that TreeBag should defend against the addition of uncomparable
objects, so that toString will never throw an exception.

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message