[jira] Created: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

[jira] Created: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
TreeBag allows uncomparable item to be added, breaking toString
---------------------------------------------------------------

                 Key: COLLECTIONS-265
                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
             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 java.util.TreeMap.compare(TreeMap.java:1093)
        at java.util.TreeMap.getEntry(TreeMap.java:347)
        at java.util.TreeMap.get(TreeMap.java:265)
        at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
        at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
[...]

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.

Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)

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

Henri Yandell commented on COLLECTIONS-265:
-------------------------------------------

Maybe the solution is to change the toString so it doesn't try to get the count of the object if not comparable.

How would that be?

Could just be "Not a Comparable:java.lang.Object@47b480" or something.

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.

Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
In reply to this post by Walter Laan (Jira)

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

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);
                bag.add(object);  // exception thrown here



> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.

Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
In reply to this post by Walter Laan (Jira)

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

Henri Yandell commented on COLLECTIONS-265:
-------------------------------------------

Sorry for the delay David.

For 2) doesn't seem like it would stop the exception - toString dies when one object is added, not two?

For 1), the idea is to have a TreeMap with the comparable data, and a single Object with the uncomparable? Then all methods would need to include the uncomparable object with the comparable when returning?

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.

Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
In reply to this post by Walter Laan (Jira)

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

David Saff commented on COLLECTIONS-265:
----------------------------------------

I prefer #2, but stated it poorly.  I would prefer if the first add of an uncomparable Object raised an exception.  The example code above is the current, somewhat odd behavior: add accepts the object the first time, but rejects it the second.  I think it should reject it the first time for consistency.

I agree that #1 is odd.  It is the de facto behavior of java.util.TreeSet: that's not terribly important to me, but if it is to the maintainers, TreeBag could follow the example.

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.

Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
In reply to this post by Walter Laan (Jira)

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

Stephen Colebourne commented on COLLECTIONS-265:
------------------------------------------------

I would prefer an "instanceof Comparable" check on each add().

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.

Reply | Threaded
Open this post in threaded view
|

[jira] Updated: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
In reply to this post by Walter Laan (Jira)

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

Hasan Diwan updated COLLECTIONS-265:
------------------------------------

    Attachment: TreeBag.pat

adds the add method with the following semantics:

public boolean add(Object o) {
   if (o instanceof Comparable) {
      return super.add(o);
  }
  else return false;
}

can this be committed asap as well?

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Bag
>    Affects Versions: 3.2
>            Reporter: David Saff
>            Priority: Minor
>         Attachments: TreeBag.pat
>
>
> 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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.

Reply | Threaded
Open this post in threaded view
|

[jira] Updated: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
In reply to this post by Walter Laan (Jira)

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

Henri Yandell updated COLLECTIONS-265:
--------------------------------------

    Fix Version/s: 3.3

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Bag
>    Affects Versions: 3.2
>            Reporter: David Saff
>            Priority: Minor
>             Fix For: 3.3
>
>         Attachments: TreeBag.pat
>
>
> 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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.

Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
In reply to this post by Walter Laan (Jira)

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

Henri Yandell commented on COLLECTIONS-265:
-------------------------------------------

Returning false because the object was not comparable is not the contract agreed on in the javadoc:

     * @return <code>true</code> if the object was not already in the <code>uniqueSet</code>

Stephen - were you implying an Exception be thrown? ie) TreeBag will not allow any class that is not Comparable?

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Bag
>    Affects Versions: 3.2
>            Reporter: David Saff
>            Priority: Minor
>             Fix For: 3.3
>
>         Attachments: TreeBag.pat
>
>
> 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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.

Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
In reply to this post by Walter Laan (Jira)

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

Stephen Colebourne commented on COLLECTIONS-265:
------------------------------------------------

Yes, an exception on add() makes most sense. However, if there is a Comparator then the instanceof check should not happen.

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Bag
>    Affects Versions: 3.2
>            Reporter: David Saff
>            Priority: Minor
>             Fix For: 3.3
>
>         Attachments: TreeBag.pat
>
>
> 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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.

Reply | Threaded
Open this post in threaded view
|

[jira] Updated: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
In reply to this post by Walter Laan (Jira)

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

Henri Yandell updated COLLECTIONS-265:
--------------------------------------

    Attachment: COLLECTIONS-265.patch

Attaching a test/patch for comments.

Throws an IllegalArgumentException if comparator() returns null and the object is not Comparable.

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Bag
>    Affects Versions: 3.2
>            Reporter: David Saff
>            Priority: Minor
>             Fix For: 3.3
>
>         Attachments: COLLECTIONS-265.patch, TreeBag.pat
>
>
> 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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.

Reply | Threaded
Open this post in threaded view
|

[jira] Closed: (COLLECTIONS-265) TreeBag allows uncomparable item to be added, breaking toString

Walter Laan (Jira)
In reply to this post by Walter Laan (Jira)

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

Henri Yandell closed COLLECTIONS-265.
-------------------------------------

    Resolution: Fixed

svn ci -m "Applying my patch from COLLECTIONS-265. TreeBag no longer accepts non-Comparable classes when it naturally ordered (ie: no comparator has been set)" src

Sending        src/java/org/apache/commons/collections/TreeBag.java
Sending        src/java/org/apache/commons/collections/bag/TreeBag.java
Sending        src/test/org/apache/commons/collections/TestTreeBag.java
Sending        src/test/org/apache/commons/collections/bag/TestTreeBag.java
Transmitting file data ....
Committed revision 641166.

> TreeBag allows uncomparable item to be added, breaking toString
> ---------------------------------------------------------------
>
>                 Key: COLLECTIONS-265
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-265
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Bag
>    Affects Versions: 3.2
>            Reporter: David Saff
>            Priority: Minor
>             Fix For: 3.3
>
>         Attachments: COLLECTIONS-265.patch, TreeBag.pat
>
>
> 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 java.util.TreeMap.compare(TreeMap.java:1093)
> at java.util.TreeMap.getEntry(TreeMap.java:347)
> at java.util.TreeMap.get(TreeMap.java:265)
> at org.apache.commons.collections.bag.AbstractMapBag.getCount(AbstractMapBag.java:116)
> at org.apache.commons.collections.bag.AbstractMapBag.toString(AbstractMapBag.java:581)
> [...]
> 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.