mathCollection
Class AbstractMultiset

java.lang.Object
  extended byjava.util.AbstractCollection
      extended bymathCollection.AbstractMultiset
All Implemented Interfaces:
java.util.Collection, Multiset
Direct Known Subclasses:
HashMultiset

public abstract class AbstractMultiset
extends java.util.AbstractCollection
implements Multiset

This class provides a skeletal implementation of the Multiset interface to minimize the effort required to implement this interface.

See Also:
Collection, AbstractCollection, HashMultiset

Constructor Summary
AbstractMultiset()
           
 
Method Summary
 boolean equals(java.lang.Object o)
          Compares the specified object with this multiset for equality.
 int hashCode()
          Returns the hash code value for this multiset.
 boolean isDisjoint(java.util.Collection c)
          Returns true if this multiset has no common element with the specified set.
 boolean isSubset(java.util.Collection c)
          Returns true if this multiset is a subset of the specified collection.
 boolean isSuperset(java.util.Collection c)
          Returns true if this multiset is a superset of the specified collection.
 java.lang.String toString()
          Returns a string representation of this multiset.
 
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface mathCollection.Multiset
add, difference, getQuantity, intersection, remove, setQuantity, setSize, sum, symmetricDifference, toSet, union
 
Methods inherited from interface java.util.Collection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 

Constructor Detail

AbstractMultiset

public AbstractMultiset()
Method Detail

hashCode

public int hashCode()
Returns the hash code value for this multiset. To get the hash code of this multiset, new hash code values for every element of this multiset are calculated from a polynomial of 3rd order and finally summed up. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two multisets s1 and s2, as required by the general contract of Object.hashCode().

Specified by:
hashCode in interface java.util.Collection
Returns:
the hash code value for this multiset.

toString

public java.lang.String toString()
Returns a string representation of this multiset. The string representation consists of a list of the set's elements in the order they are returned by its iterator, enclosed in curly brackets ("{}"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by Object.toString().

Returns:
a string representation of this multiset.

isSuperset

public boolean isSuperset(java.util.Collection c)
Returns true if this multiset is a superset of the specified collection. That is, if all elements of the specified collection are also present in this multiset at least the same number of times.

This implementation first compares the sizes of this multiset and the specified collection by invoking the size method on each. If this multiset is bigger than the specified collection then each element of the specified collection is checked for presence in this multiset (for multiple equal elements, the quantity in this multiset has to be greater or equal). Otherwise, false is returned.

Specified by:
isSuperset in interface Multiset
Parameters:
c - collection to be checked for being a subset.
Returns:
true if this multiset is a superset of the specifed collection, false otherwise.
See Also:
isSubset(Collection)

isSubset

public boolean isSubset(java.util.Collection c)
Returns true if this multiset is a subset of the specified collection. That is, if all elements of this multiset are also present in the specified collection at least the same number of times.

This implementation first compares the sizes of this multiset and the specified collection by invoking the size method on each. If the specified collection is bigger than this multiset then each element of this multiset is checked for presence in the specified collection (for multiple equal elements, the quantity in the specified collection has to be greater or equal). Otherwise, false is returned.

Specified by:
isSubset in interface Multiset
Parameters:
c - collection to be checked for being a superset.
Returns:
true if this multiset is a subset of the specifed collection, false otherwise.
See Also:
isSuperset(Collection)

isDisjoint

public boolean isDisjoint(java.util.Collection c)
Returns true if this multiset has no common element with the specified set.

This implementation checks whether the specified collection is an instance of Multiset or not. If so, it iterates over the multiset that has fewer different elements. During iteration, only different elements are taken into account. If the specified collection is not an instance of Multiset, it iterates over all elements of the specified collection. If a common element is found, that is, if an element is contained both in this multiset and in the specified collection, false is returned.

Specified by:
isDisjoint in interface Multiset
Parameters:
c - collection to be checked for common elements.
Returns:
true if this multiset has no common elements with the specifed collection, false otherwise.

equals

public boolean equals(java.lang.Object o)
Compares the specified object with this multiset for equality. Returns true if the specified object is also a collection, the two sets have the same size, and every element of the specified set is contained in this set the same number of times.

If the specified object is not this multiset itself but another collection, this implementation first compares the sizes of this multiset and the specified collection by invoking the size method on each. If the sizes match, the sets are compared on a per-element basis.

Specified by:
equals in interface java.util.Collection
Parameters:
o - object to be compared for equality with this multiset.
Returns:
true if the specified object is equal to this multiset, false otherwise.