You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2002/05/28 21:49:30 UTC

DO NOT REPLY [Bug 9467] New: - Bag interface violates Collection contract

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9467>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9467

Bag interface violates Collection contract

           Summary: Bag interface violates Collection contract
           Product: Commons
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Minor
          Priority: Other
         Component: Collections
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: pjack@sfaf.org


The following methods as defined by the Bag interface violate their 
corresponding definitions in the Collection interface:

add(Object), addAll(Collection)
  The Collection interface specifies that the add method should return true if 
the collection changes as a result of the call.  Since a bag's size always 
increases as a result of an add operation, Bag.add should always return true.  
The bag interface specifies that add(Object) will only return true if the 
object was not already in the unique set.
  
remove(Object)
  The Collection interface states that only one occurrence of a given object 
should be removed as a result of this method.  The Bag interface specifies that 
all occurrences will be removed.

removeAll(Collection)
retainAll(Collection)
containsAll(Collection)
  The Collection specification does not respect cardinality for these methods; 
the Bag interface does.

The add, addAll and remove methods can be fixed easily, and code relying on the 
old behavior can be rewritten simply using existing Bag methods:

   boolean r = bag.add(obj)

can be replaced with:

   boolean r = !bag.contains(obj);
   bag.add(obj)

And

   boolean r = bag.remove(obj)

can be replaced with

   boolean r = bag.remove(obj, bag.getCount(obj));

The existing bulk operations could be implemented with static utility methods 
in CollectionUtils (or possibly BagUtils), which would allow a smooth migration 
path for code that relies on the current bulk operation behavior.

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>