|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Bag<E>
Bags are a specialization of unordered collections that explicitly allow duplicates.
Method Summary | |
---|---|
boolean |
add(E object)
(Violation) 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)
(Violation) 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)
(Violation) Removes all occurrences 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)
(Violation) Remove all elements represented in the given collection, respecting cardinality. |
boolean |
retainAll(java.util.Collection<?> coll)
(Violation) 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)
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,
according to the Collection.add(Object)
contract, it
should always return true
. Since it sometimes returns
false
, this method violates the contract.
add
in interface java.util.Collection<E>
object
- the object to add
true
if the object was not already in the uniqueSet
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
true
if the object was not already in the uniqueSet
boolean remove(java.lang.Object object)
This will also remove the object from the uniqueSet()
.
According to the Collection.remove(Object)
method,
this method should only remove the first occurrence of the
given object, not all occurrences.
remove
in interface java.util.Collection<E>
true
if this call changed the collectionboolean 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
true
if this call changed the collectionjava.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, respecting cardinality. That is, if the
given collection coll
contains n
copies
of a given object, calling getCount(Object)
on that object must
be >= n
for all n
in coll
.
The Collection.containsAll(Collection)
method specifies
that cardinality should not be respected; this method should
return true if the bag contains at least one of every object contained
in the given collection.
containsAll
in interface java.util.Collection<E>
coll
- the collection to check against
true
if the Bag contains all the collectionboolean removeAll(java.util.Collection<?> coll)
coll
contains n
copies of a given object,
the bag will have n
fewer copies, assuming the bag
had at least n
copies to begin with.
The Collection.removeAll(Collection)
method specifies
that cardinality should not be respected; this method should
remove all occurrences of every object contained in the
given collection.
removeAll
in interface java.util.Collection<E>
coll
- the collection to remove
true
if this call changed the collectionboolean retainAll(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.
The Collection.retainAll(Collection)
method specifies
that cardinality should not be respected; this method should
keep all occurrences of every object contained in the
given collection.
retainAll
in interface java.util.Collection<E>
coll
- the collection to retain
true
if this call changed the 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 |