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

Surprising exception by CompositeSet in a situation where CompositeCollection works fine

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.2.1
    • Fix Version/s: 4.0-alpha1, 4.0
    • Component/s: Set
    • Labels:
      None
    • Environment:

      All environments

      Description

      We have a method that uses a CompositeCollection. Here's a simplified version of it:

      void m(CompositeCollection coll)

      { coll.addComposited(new TreeBag()); }

      It works fine when the argument is a CompositeCollection, but it throws an exception when the argument is a CompositeSet. E.g.:

      m(new CompositeCollection()); // OK
      m(new CompositeSet()); // IllegalArgumentException

      Although the exception is documented in CompositeSet, this behavior is very surprising. Is there a way to have m() accept CompositeCollections without running into this exception? The only solution that comes to my mind is to dynamically check the type of 'coll' in m(), but this is a rather nasty work-around.

      A better solution may be to make the genericity of CompositeCollection explicit by adding a type parameter:

      class CompositeCollection<T extends Collection> {
      void addComposited(T c)

      { /* .. */ }
      }

      class CompositeSet extends CompositeCollection<Set> {
      @Override void addComposited(Set c) { /* .. */ }

      }

      This way, users of CompositeCollection must choose the kind of collections that can be composed and will not encounter surprises, such as the above.

        Attachments

        1. collections424.patch
          50 kB
          srirangadeepthi
        2. COLLECTIONS-424.patch
          30 kB
          Thomas Neidhart

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              michaelpradel Michael Pradel
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: