Uploaded image for project: 'Commons Collections'
  1. Commons Collections
  2. COLLECTIONS-265

TreeBag allows uncomparable item to be added, breaking toString

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.2
    • 4.0-alpha1, 4.0
    • Bag
    • None

    Description

      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.

      Attachments

        1. TreeBag.pat
          0.6 kB
          Hasan Diwan
        2. COLLECTIONS-265.patch
          3 kB
          Henri Yandell

        Activity

          People

            Unassigned Unassigned
            dsaff David Saff
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: