net.sf.collections15
Interface Bag<E>

All Superinterfaces:
java.util.Collection<E>, java.lang.Iterable<E>
All Known Subinterfaces:
SortedBag<E>

public interface Bag<E>
extends java.util.Collection<E>

Defines a collection that counts the number of times an object appears in the collection.

Suppose you have a Bag that contains {a, a, b, c}. Calling getCount(Object) on a would return 2, while calling uniqueSet() would return {a, b, c}.

Since:
Commons Collections15 1.0
Version:
$Revision: 1.2 $ $Date: 2004/10/17 01:02:42 $
Author:
Chuck Burdick, Stephen Colebourne

Method Summary
 boolean add(E object)
          Adds one copy the specified object to the Bag.
 boolean add(E object, int nCopies)
          Adds nCopies copies of the specified object to the Bag.
 boolean containsAll(java.util.Collection<?> coll)
          Returns true if the bag contains all elements in the given collection.
 boolean containsAllCardinally(java.util.Collection<?> coll)
          Returns true if the bag contains all elements in the given collection, respecting cardinality.
 int getCount(java.lang.Object object)
          Returns the number of occurrences (cardinality) of the given object currently in the bag.
 java.util.Iterator<E> iterator()
          Returns an Iterator over the entire set of members, including copies due to cardinality.
 boolean remove(java.lang.Object object)
          Removes a single occurrence of the given object from the bag.
 boolean remove(java.lang.Object object, int nCopies)
          Removes nCopies copies of the specified object from the Bag.
 boolean removeAll(java.util.Collection<?> coll)
          Remove all occurrences of every element in the given collection.
 boolean removeAllCardinally(java.util.Collection<?> coll)
          Remove all elements represented in the given collection, respecting cardinality.
 boolean removeAllCopies(java.lang.Object object)
          Removes all occurrences of the given object from the bag.
 boolean retainAll(java.util.Collection<?> coll)
          Remove all elements of the bag that are not in the given collection.
 boolean retainAllCardinally(java.util.Collection<?> coll)
          Remove any members of the bag that are not in the given collection, respecting cardinality.
 int size()
          Returns the total number of items in the bag across all types.
 java.util.Set<E> uniqueSet()
          Returns a Set of unique elements in the Bag.
 
Methods inherited from interface java.util.Collection
addAll, clear, contains, equals, hashCode, isEmpty, toArray, toArray
 

Method Detail

getCount

int getCount(java.lang.Object object)
Returns the number of occurrences (cardinality) of the given object currently in the bag. If the object does not exist in the bag, return 0.

Parameters:
object - the object to search for
Returns:
the number of occurrences of the object, zero if not found

add

boolean add(E object)
Adds one copy the specified object to the Bag.

If the object is already in the uniqueSet() then increment its count as reported by getCount(Object). Otherwise add it to the uniqueSet() and report its count as 1.

Since this method always increases the size of the bag, it always returns true, in accordance with the Collection.add(Object) contract.

Specified by:
add in interface java.util.Collection<E>
Parameters:
object - the object to add
Returns:
true, to indicate that the collection changed as a result of this call.

add

boolean add(E object,
            int nCopies)
Adds nCopies copies of the specified object to the Bag.

If the object is already in the uniqueSet() then increment its count as reported by getCount(Object). Otherwise add it to the uniqueSet() and report its count as nCopies.

Parameters:
object - the object to add
nCopies - the number of copies to add. Cannot be negative.
Returns:
true, if the collection changed as a result of this call (which can only occur if nCopies > 0).
Throws:
java.lang.IllegalArgumentException - if nCopies is negative.

remove

boolean remove(java.lang.Object object)
Removes a single occurrence of the given object from the bag.

If there was only one copy of the object in the set, this will also remove the object from the uniqueSet().

Specified by:
remove in interface java.util.Collection<E>
Returns:
true if this call changed the collection.

removeAllCopies

boolean removeAllCopies(java.lang.Object object)
Removes all occurrences of the given object from the bag.

This will also remove the object from the uniqueSet().

Parameters:
object - the object to remove
Returns:
true if this call changed the collection.

remove

boolean remove(java.lang.Object object,
               int nCopies)
Removes nCopies copies of the specified object from the Bag.

If the number of copies to remove is greater than the actual number of copies in the Bag, no error is thrown.

Parameters:
object - the object to remove
nCopies - the number of copies to remove. Cannot be negative.
Returns:
true if this call changed the collection
Throws:
java.lang.IllegalArgumentException - if nCopies is negative.

uniqueSet

java.util.Set<E> uniqueSet()
Returns a Set of unique elements in the Bag.

Uniqueness constraints are the same as those in Set.

Returns:
the Set of unique Bag elements

size

int size()
Returns the total number of items in the bag across all types.

Specified by:
size in interface java.util.Collection<E>
Returns:
the total size of the Bag

containsAll

boolean containsAll(java.util.Collection<?> coll)
Returns true if the bag contains all elements in the given collection. More specifically, this method returns true only if contains(e) returns true for each element, e, in the specified collection, coll.

Specified by:
containsAll in interface java.util.Collection<E>
Parameters:
coll - the collection to check against
Returns:
true if the Bag contains all elements in the collection

containsAllCardinally

boolean containsAllCardinally(java.util.Collection<?> coll)
Returns true if the bag contains all elements in the given collection, respecting cardinality. More specifically, this method returns true only if the following is true.

Parameters:
coll - the collection to check against
Returns:
true if the contents of the collection is a subset of the contents of this bag.

removeAll

boolean removeAll(java.util.Collection<?> coll)
Remove all occurrences of every element in the given collection.

Specified by:
removeAll in interface java.util.Collection<E>
Parameters:
coll - the elements to remove
Returns:
true if this call changed this collection.

removeAllCardinally

boolean removeAllCardinally(java.util.Collection<?> coll)
Remove all elements represented in the given collection, respecting cardinality. That is, if the given collection coll contains n copies of a given object, the bag will have n fewer copies (or no copies, if the bag had fewer than n copies to begin with).

Parameters:
coll - the collection to remove
Returns:
true if this call changed this collection

retainAll

boolean retainAll(java.util.Collection<?> coll)
Remove all elements of the bag that are not in the given collection.

Specified by:
retainAll in interface java.util.Collection<E>
Parameters:
coll - the collection to retain
Returns:
true if this call changed the collection

retainAllCardinally

boolean retainAllCardinally(java.util.Collection<?> coll)
Remove any members of the bag that are not in the given collection, respecting cardinality. That is, if the given collection coll contains n copies of a given object and the bag has m > n copies, then delete m - n copies from the bag. In addition, if e is an object in the bag but !coll.contains(e), then remove e and any of its copies.

Parameters:
coll - the collection to retain
Returns:
true if this call changed this collection

iterator

java.util.Iterator<E> iterator()
Returns an Iterator over the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.

Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface java.lang.Iterable<E>
Returns:
iterator over all elements in the Bag


Copyright © 2001-2005 SourceForge.net. All Rights Reserved.