Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.bag

public abstract Class AbstractMapBag<E>

extends Object
implements Bag<E>
Class Inheritance
All Implemented Interfaces
org.apache.commons.collections4.Bag, java.util.Collection, java.lang.Iterable
Known Direct Subclasses
org.apache.commons.collections4.bag.HashBag, org.apache.commons.collections4.bag.TreeBag
Type Parameters
<E>
the type of elements in this bag
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, java.lang.reflect.Array, java.util.Collection, .ConcurrentModificationException, .Iterator, .Map, .Map.Entry, .Set, org.apache.commons.collections4.Bag, org.apache.commons.collections4.set.UnmodifiableSet

Abstract implementation of the Bag interface to simplify the creation of subclass implementations.

Subclasses specify a Map implementation to use as the internal storage. The map will be used to map bag elements to a number; the number represents the number of occurrences of that element in the bag.

Since
3.0 (previously DefaultMapBag v2.0)

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
AbstractMapBag.BagIterator<E>

Inner class iterator for the Bag.

protected static class
AbstractMapBag.MutableInteger

Mutable integer class for storing the data.

Field Summary

Modifier and TypeField and Description
private transient Map<E, AbstractMapBag.MutableInteger>
map

The map to use to store the data

private transient int
modCount

The modification count for fail fast iterators

private int
size

The current total size of the bag

private transient Set<E>
uniqueSet

Unique view of the elements

Constructor Summary

AccessConstructor and Description
protected
AbstractMapBag()

Constructor needed for subclass serialisation.

protected
AbstractMapBag(final Map<E, AbstractMapBag.MutableInteger>
the map to assign
map
)

Constructor that assigns the specified Map as the backing store.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the object was not already in the uniqueSet
add
(final E
the object to add
object
)

Implements org.apache.commons.collections4.Bag.add.

Adds a new element to the bag, incrementing its count in the underlying map.
public boolean

Returns:

true if the object was not already in the uniqueSet
add
(final E
the object to search for
object
,
final int
the number of copies to add
nCopies
)

Implements org.apache.commons.collections4.Bag.add.

Adds a new element to the bag, incrementing its count in the map.
public boolean

Returns:

true if this call changed the bag
addAll
(final Collection<? extends E>
the collection to add
coll
)

Implements java.util.Collection.addAll.

Invokes add(Object) for each element in the given collection.
public void
clear()

Implements java.util.Collection.clear.

Clears the bag by clearing the underlying map.
public boolean

Returns:

true if the bag contains the given element
contains
(final Object
the object to search for
object
)

Implements java.util.Collection.contains.

Determines if the bag contains the given element by checking if the underlying map contains the element as a key.
public boolean

Returns:

true if the Bag contains all the collection
containsAll
(final Collection<?>
the collection to check against
coll
)

Implements org.apache.commons.collections4.Bag.containsAll.

Determines if the bag contains the given elements.
pack-priv boolean

Returns:

true if the Bag contains all the collection
containsAll
(final Bag<?>
the bag to check against
other
)

Returns true if the bag contains all elements in the given collection, respecting cardinality.

protected void
doReadObject(final Map<E, AbstractMapBag.MutableInteger>
the map to use
map
,
final ObjectInputStream
the input stream
in
)

Read the map in using a custom routine.

protected void
doWriteObject(final ObjectOutputStream
the output stream
out
)

Write the map out using a custom routine.

public boolean

Returns:

true if equal
equals
(final Object
the Bag to compare to
object
)

Overrides java.lang.Object.equals.

Implements java.util.Collection.equals.

Compares this Bag to another.
public int

Returns:

the number of occurrences of the object, zero if not found
getCount
(final Object
the object to search for
object
)

Implements org.apache.commons.collections4.Bag.getCount.

Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map.
protected Map<E, AbstractMapBag.MutableInteger>

Returns:

the map being used by the Bag
getMap
()

Utility method for implementations to access the map that backs this bag.

public int

Returns:

the hash code of the Bag
hashCode
()

Overrides java.lang.Object.hashCode.

Implements java.util.Collection.hashCode.

Gets a hash code for the Bag compatible with the definition of equals.
public boolean

Returns:

true if bag is empty
isEmpty
()

Implements java.util.Collection.isEmpty.

Returns true if the underlying map is empty.
public Iterator<E>

Returns:

the iterator
iterator
()

Implements org.apache.commons.collections4.Bag.iterator.

Gets an iterator over the bag elements.
public boolean

Returns:

true if the bag changed
remove
(final Object
the object to remove
object
)

Implements org.apache.commons.collections4.Bag.remove.

Removes all copies of the specified object from the bag.
public boolean

Returns:

true if the bag changed
remove
(final Object
the object to remove
object
,
final int
the number of copies to remove
nCopies
)

Implements org.apache.commons.collections4.Bag.remove.

Removes a specified number of copies of an object from the bag.
public boolean

Returns:

true if the bag changed
removeAll
(final Collection<?>
the collection to use
coll
)

Implements org.apache.commons.collections4.Bag.removeAll.

Removes objects from the bag according to their count in the specified collection.
public boolean

Returns:

true if this call changed the collection
retainAll
(final Collection<?>
the collection to retain
coll
)

Implements org.apache.commons.collections4.Bag.retainAll.

Remove any members of the bag that are not in the given bag, respecting cardinality.
pack-priv boolean

Returns:

true if this call changed the collection
retainAll
(final Bag<?>
the bag to retain
other
)

Remove any members of the bag that are not in the given bag, respecting cardinality.

public int

Returns:

current size of the bag
size
()

Implements org.apache.commons.collections4.Bag.size.

Returns the number of elements in this bag.
public Object[]

Returns:

an array of all of this bag's elements
toArray
()

Implements java.util.Collection.toArray.

Returns an array of all of this bag's elements.
public <
the type of the array elements
T
>
T[]

Returns:

an array of all of this bag's elements
toArray
(T[]
the array to populate
array
)

Implements java.util.Collection.toArray.

Returns an array of all of this bag's elements.
public String

Returns:

a debugging toString
toString
()

Overrides java.lang.Object.toString.

Implement a toString() method suitable for debugging.
public Set<E>

Returns:

the set of unique elements in this bag
uniqueSet
()

Implements org.apache.commons.collections4.Bag.uniqueSet.

Returns an unmodifiable view of the underlying map's key set.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

mapback to summary
private transient Map<E, AbstractMapBag.MutableInteger> map

The map to use to store the data

modCountback to summary
private transient int modCount

The modification count for fail fast iterators

sizeback to summary
private int size

The current total size of the bag

uniqueSetback to summary
private transient Set<E> uniqueSet

Unique view of the elements

Constructor Detail

AbstractMapBagback to summary
protected AbstractMapBag()

Constructor needed for subclass serialisation.

AbstractMapBagback to summary
protected AbstractMapBag(final Map<E, AbstractMapBag.MutableInteger> map)

Constructor that assigns the specified Map as the backing store. The map must be empty and non-null.

Parameters
map:Map<E, AbstractMapBag.MutableInteger>

the map to assign

Method Detail

addback to summary
public boolean add(final E object)

Implements org.apache.commons.collections4.Bag.add.

Adds a new element to the bag, incrementing its count in the underlying map.

Parameters
object:E

the object to add

Returns:boolean

true if the object was not already in the uniqueSet

Annotations
@Override
addback to summary
public boolean add(final E object, final int nCopies)

Implements org.apache.commons.collections4.Bag.add.

Adds a new element to the bag, incrementing its count in the map.

Parameters
object:E

the object to search for

nCopies:int

the number of copies to add

Returns:boolean

true if the object was not already in the uniqueSet

Annotations
@Override
addAllback to summary
public boolean addAll(final Collection<? extends E> coll)

Implements java.util.Collection.addAll.

Invokes add(Object) for each element in the given collection.

Parameters
coll:Collection<? extends E>

the collection to add

Returns:boolean

true if this call changed the bag

Annotations
@Override
clearback to summary
public void clear()

Implements java.util.Collection.clear.

Clears the bag by clearing the underlying map.

Annotations
@Override
containsback to summary
public boolean contains(final Object object)

Implements java.util.Collection.contains.

Determines if the bag contains the given element by checking if the underlying map contains the element as a key.

Parameters
object:Object

the object to search for

Returns:boolean

true if the bag contains the given element

Annotations
@Override
containsAllback to summary
public boolean containsAll(final Collection<?> coll)

Implements org.apache.commons.collections4.Bag.containsAll.

Determines if the bag contains the given elements.

Parameters
coll:Collection<?>

the collection to check against

Returns:boolean

true if the Bag contains all the collection

Annotations
@Override
containsAllback to summary
pack-priv boolean containsAll(final Bag<?> other)

Returns true if the bag contains all elements in the given collection, respecting cardinality.

Parameters
other:Bag<?>

the bag to check against

Returns:boolean

true if the Bag contains all the collection

doReadObjectback to summary
protected void doReadObject(final Map<E, AbstractMapBag.MutableInteger> map, final ObjectInputStream in) throws IOException, ClassNotFoundException

Read the map in using a custom routine.

Parameters
map:Map<E, AbstractMapBag.MutableInteger>

the map to use

in:ObjectInputStream

the input stream

Exceptions
IOException:
any of the usual I/O related exceptions
ClassNotFoundException:
if the stream contains an object which class can not be loaded
ClassCastException:
if the stream does not contain the correct objects
doWriteObjectback to summary
protected void doWriteObject(final ObjectOutputStream out) throws IOException

Write the map out using a custom routine.

Parameters
out:ObjectOutputStream

the output stream

Exceptions
IOException:
any of the usual I/O related exceptions
equalsback to summary
public boolean equals(final Object object)

Overrides java.lang.Object.equals.

Implements java.util.Collection.equals.

Compares this Bag to another. This Bag equals another Bag if it contains the same number of occurrences of the same elements.

Parameters
object:Object

the Bag to compare to

Returns:boolean

true if equal

Annotations
@Override
getCountback to summary
public int getCount(final Object object)

Implements org.apache.commons.collections4.Bag.getCount.

Returns the number of occurrence of the given element in this bag by looking up its count in the underlying map.

Parameters
object:Object

the object to search for

Returns:int

the number of occurrences of the object, zero if not found

Annotations
@Override
getMapback to summary
protected Map<E, AbstractMapBag.MutableInteger> getMap()

Utility method for implementations to access the map that backs this bag. Not intended for interactive use outside of subclasses.

Returns:Map<E, AbstractMapBag.MutableInteger>

the map being used by the Bag

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Collection.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.

Returns:int

the hash code of the Bag

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Implements java.util.Collection.isEmpty.

Returns true if the underlying map is empty.

Returns:boolean

true if bag is empty

Annotations
@Override
iteratorback to summary
public Iterator<E> iterator()

Implements org.apache.commons.collections4.Bag.iterator.

Gets an iterator over the bag elements. Elements present in the Bag more than once will be returned repeatedly.

Returns:Iterator<E>

the iterator

Annotations
@Override
removeback to summary
public boolean remove(final Object object)

Implements org.apache.commons.collections4.Bag.remove.

Removes all copies of the specified object from the bag.

Parameters
object:Object

the object to remove

Returns:boolean

true if the bag changed

Annotations
@Override
removeback to summary
public boolean remove(final Object object, final int nCopies)

Implements org.apache.commons.collections4.Bag.remove.

Removes a specified number of copies of an object from the bag.

Parameters
object:Object

the object to remove

nCopies:int

the number of copies to remove

Returns:boolean

true if the bag changed

Annotations
@Override
removeAllback to summary
public boolean removeAll(final Collection<?> coll)

Implements org.apache.commons.collections4.Bag.removeAll.

Removes objects from the bag according to their count in the specified collection.

Parameters
coll:Collection<?>

the collection to use

Returns:boolean

true if the bag changed

Annotations
@Override
retainAllback to summary
public boolean retainAll(final Collection<?> coll)

Implements org.apache.commons.collections4.Bag.retainAll.

Remove any members of the bag that are not in the given bag, respecting cardinality.

Parameters
coll:Collection<?>

the collection to retain

Returns:boolean

true if this call changed the collection

Annotations
@Override
retainAllback to summary
pack-priv boolean retainAll(final Bag<?> other)

Remove any members of the bag that are not in the given bag, respecting cardinality.

Parameters
other:Bag<?>

the bag to retain

Returns:boolean

true if this call changed the collection

See Also
retainAll(Collection)
sizeback to summary
public int size()

Implements org.apache.commons.collections4.Bag.size.

Returns the number of elements in this bag.

Returns:int

current size of the bag

Annotations
@Override
toArrayback to summary
public Object[] toArray()

Implements java.util.Collection.toArray.

Returns an array of all of this bag's elements.

Returns:Object[]

an array of all of this bag's elements

Annotations
@Override
toArrayback to summary
public <T> T[] toArray(T[] array)

Implements java.util.Collection.toArray.

Returns an array of all of this bag's elements. If the input array has more elements than are in the bag, trailing elements will be set to null.

Parameters
<T>
the type of the array elements
array:T[]

the array to populate

Returns:T[]

an array of all of this bag's elements

Annotations
@Override
Exceptions
ArrayStoreException:
if the runtime type of the specified array is not a supertype of the runtime type of the elements in this list
NullPointerException:
if the specified array is null
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Implement a toString() method suitable for debugging.

Returns:String

a debugging toString

Annotations
@Override
uniqueSetback to summary
public Set<E> uniqueSet()

Implements org.apache.commons.collections4.Bag.uniqueSet.

Returns an unmodifiable view of the underlying map's key set.

Returns:Set<E>

the set of unique elements in this bag

Annotations
@Override
org.apache.commons.collections4.bag back to summary

pack-priv Class AbstractMapBag.BagIterator<E>

extends Object
implements Iterator<E>
Class Inheritance
  • java.lang.Object
  • org.apache.commons.collections4.bag.AbstractMapBag.BagIterator
All Implemented Interfaces
java.util.Iterator

Inner class iterator for the Bag.

Field Summary

Modifier and TypeField and Description
private boolean
private Map.Entry<E, AbstractMapBag.MutableInteger>
private final Iterator<Map.Entry<E, AbstractMapBag.MutableInteger>>
private int
private final int
private final AbstractMapBag<E>

Constructor Summary

AccessConstructor and Description
public
BagIterator(final AbstractMapBag<E>
the parent bag
parent
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public E
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

canRemoveback to summary
private boolean canRemove
currentback to summary
private Map.Entry<E, AbstractMapBag.MutableInteger> current
entryIteratorback to summary
private final Iterator<Map.Entry<E, AbstractMapBag.MutableInteger>> entryIterator
itemCountback to summary
private int itemCount
modsback to summary
private final int mods
parentback to summary
private final AbstractMapBag<E> parent

Constructor Detail

BagIteratorback to summary
public BagIterator(final AbstractMapBag<E> parent)

Constructor.

Parameters
parent:AbstractMapBag<E>

the parent bag

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

Annotations
@Override
nextback to summary
public E next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:E

the next element in the iteration

Annotations
@Override
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

Annotations
@Override
org.apache.commons.collections4.bag back to summary

protected Class AbstractMapBag.MutableInteger

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.commons.collections4.bag.AbstractMapBag.MutableInteger

Mutable integer class for storing the data.

Field Summary

Modifier and TypeField and Description
protected int
value

The value of this mutable.

Constructor Summary

AccessConstructor and Description
pack-priv
MutableInteger(final int
the initial value
value
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

valueback to summary
protected int value

The value of this mutable.

Constructor Detail

MutableIntegerback to summary
pack-priv MutableInteger(final int value)

Constructor.

Parameters
value:int

the initial value

Method Detail

equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override