|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Bag<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}
.
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 |
---|
int getCount(java.lang.Object object)
object
- the object to search for
boolean add(E object)
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.
add
in interface java.util.Collection<E>
object
- the object to add
true
, to indicate that the collection changed as a
result of this call.boolean add(E object, int nCopies)
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
.
object
- the object to addnCopies
- the number of copies to add. Cannot be negative.
true
, if the collection changed as a result of this
call (which can only occur if nCopies
> 0).
java.lang.IllegalArgumentException
- if nCopies
is negative.boolean remove(java.lang.Object object)
uniqueSet()
.
remove
in interface java.util.Collection<E>
true
if this call changed the collection.boolean removeAllCopies(java.lang.Object object)
uniqueSet()
.
object
- the object to remove
true
if this call changed the collection.boolean remove(java.lang.Object object, int nCopies)
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.
object
- the object to removenCopies
- the number of copies to remove. Cannot be negative.
true
if this call changed the collection
java.lang.IllegalArgumentException
- if nCopies
is negative.java.util.Set<E> uniqueSet()
Set
of unique elements in the Bag.
Uniqueness constraints are the same as those in Set
.
int size()
size
in interface java.util.Collection<E>
boolean containsAll(java.util.Collection<?> coll)
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
.
containsAll
in interface java.util.Collection<E>
coll
- the collection to check against
true
if the Bag contains all elements in the
collectionboolean containsAllCardinally(java.util.Collection<?> coll)
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. e
, of which there are n
copies in
collection coll
, the value returned by calling
getCount(e)
must be >= n
.e
, in
coll
.
coll
- the collection to check against
true
if the contents of the collection is a subset
of the contents of this bag.boolean removeAll(java.util.Collection<?> coll)
removeAll
in interface java.util.Collection<E>
coll
- the elements to remove
true
if this call changed this collection.boolean removeAllCardinally(java.util.Collection<?> coll)
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).
coll
- the collection to remove
true
if this call changed this collectionboolean retainAll(java.util.Collection<?> coll)
retainAll
in interface java.util.Collection<E>
coll
- the collection to retain
true
if this call changed the collectionboolean retainAllCardinally(java.util.Collection<?> coll)
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.
coll
- the collection to retain
true
if this call changed this collectionjava.util.Iterator<E> iterator()
Iterator
over the entire set of members, including
copies due to cardinality. This iterator is fail-fast and will not
tolerate concurrent modifications.
iterator
in interface java.util.Collection<E>
iterator
in interface java.lang.Iterable<E>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |