jodd.util.collection
Class HashBag<E>

java.lang.Object
  extended by jodd.util.collection.HashBag<E>
All Implemented Interfaces:
java.lang.Iterable<E>, java.util.Collection<E>, Bag<E>

public class HashBag<E>
extends java.lang.Object
implements Bag<E>

HashBag implementation of a Bag


Field Summary
protected  java.util.Map<E,MutableInteger> map
           
protected  int size
           
 
Constructor Summary
HashBag()
           
HashBag(java.util.Collection<? extends E> coll)
           
 
Method Summary
 boolean add(E object)
          Adds a new element to the bag, incrementing its count in the underlying map.
 boolean add(E object, int nCopies)
          Adds a new element to the bag, incrementing its count in the map.
 boolean addAll(java.util.Collection<? extends E> coll)
          Invokes add(Object) for each element in the given collection.
 void clear()
          Clears the bag by clearing the underlying map.
 boolean contains(java.lang.Object object)
          Determines if the bag contains the given element.
 boolean containsAll(java.util.Collection<?> coll)
          Determines if the bag contains all the collection elements..
 boolean equals(java.lang.Object object)
          Compares bag to another.
 int getCount(java.lang.Object object)
          Returns the number of occurrence of the given element in bag.
 int hashCode()
          Gets a hash code for the Bag compatible with the definition of equals.
 boolean isEmpty()
          Returns true if bag is empty.
 java.util.Iterator<E> iterator()
          Returns an iterator over the bag elements.
 boolean remove(java.lang.Object object)
          Removes all copies of the specified object from the bag.
 boolean remove(java.lang.Object object, int nCopies)
          Removes a specified number of copies of an object from the bag.
 boolean removeAll(java.util.Collection<?> coll)
          Removes objects from the bag according to their count in the specified collection.
 boolean retainAll(java.util.Collection<?> coll)
          Remove any members of the bag that are not in the given bag, respecting cardinality.
 int size()
          Returns the number of elements in the bag.
 java.lang.Object[] toArray()
          Returns an array of all of bag's elements.
<T> T[]
toArray(T[] array)
          Populates an array with all of this bag's elements.
 java.lang.String toString()
          Implement a toString() method suitable for debugging.
 java.util.Set<E> uniqueSet()
          Returns a Set of unique elements in the Bag.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

map

protected java.util.Map<E,MutableInteger> map

size

protected int size
Constructor Detail

HashBag

public HashBag()

HashBag

public HashBag(java.util.Collection<? extends E> coll)
Method Detail

size

public int size()
Returns the number of elements in the bag.

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

isEmpty

public boolean isEmpty()
Returns true if bag is empty.

Specified by:
isEmpty in interface java.util.Collection<E>

getCount

public int getCount(java.lang.Object object)
Returns the number of occurrence of the given element in bag.

Specified by:
getCount in interface Bag<E>
Parameters:
object - the object to search for
Returns:
the number of occurrences of the object, zero if not found

contains

public boolean contains(java.lang.Object object)
Determines if the bag contains the given element.

Specified by:
contains in interface java.util.Collection<E>
Returns:
true if the bag contains the given element

containsAll

public boolean containsAll(java.util.Collection<?> coll)
Determines if the bag contains all the collection elements..

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

iterator

public java.util.Iterator<E> iterator()
Returns an iterator over the bag elements. Elements present in the Bag more than once will be returned repeatedly.

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

add

public boolean add(E object)
Adds a new element to the bag, incrementing its count in the underlying map.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface Bag<E>
Parameters:
object - the object to add
Returns:
true if the object was not already in the bag

add

public boolean add(E object,
                   int nCopies)
Adds a new element to the bag, incrementing its count in the map.

Specified by:
add in interface Bag<E>
Parameters:
object - the object to add
nCopies - the number of copies to add
Returns:
true if the object was not already in the bag

addAll

public boolean addAll(java.util.Collection<? extends E> coll)
Invokes add(Object) for each element in the given collection.

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

clear

public void clear()
Clears the bag by clearing the underlying map.

Specified by:
clear in interface java.util.Collection<E>

remove

public boolean remove(java.lang.Object object)
Removes all copies of the specified object from the bag.

Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface Bag<E>
Returns:
true if the bag changed

remove

public boolean remove(java.lang.Object object,
                      int nCopies)
Removes a specified number of copies of an object from the bag.

Specified by:
remove in interface Bag<E>
Parameters:
object - the object to remove
nCopies - the number of copies to remove
Returns:
true if the bag changed

removeAll

public boolean removeAll(java.util.Collection<?> coll)
Removes objects from the bag according to their count in the specified collection.

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

retainAll

public boolean retainAll(java.util.Collection<?> coll)
Remove any members of the bag that are not in the given bag, respecting cardinality.

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

uniqueSet

public java.util.Set<E> uniqueSet()
Description copied from interface: Bag
Returns a Set of unique elements in the Bag.

Uniqueness constraints are the same as those in Set.

Specified by:
uniqueSet in interface Bag<E>
Returns:
the Set of unique Bag elements

toArray

public java.lang.Object[] toArray()
Returns an array of all of bag's elements.

Specified by:
toArray in interface java.util.Collection<E>

toArray

public <T> T[] toArray(T[] array)
Populates an array with all of this bag's elements.

Specified by:
toArray in interface java.util.Collection<E>

equals

public boolean equals(java.lang.Object object)
Compares bag to another. This Bag equals another Bag if it contains the same number of occurrences of the same elements.

Specified by:
equals in interface java.util.Collection<E>
Overrides:
equals in class java.lang.Object
Returns:
true if equal

hashCode

public int hashCode()
Gets a hash code for the Bag compatible with the definition of equals. The hash code is defined as the sum total of a hash code for each element. The per element hash code is defined as (e==null ? 0 : e.hashCode()) ^ noOccurances). This hash code is compatible with the Set interface.

Specified by:
hashCode in interface java.util.Collection<E>
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Implement a toString() method suitable for debugging.

Overrides:
toString in class java.lang.Object


Copyright © 2003-2012 Jodd Team