public abstract class AbstractMapBag extends Object implements Bag
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.
Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractMapBag.MutableInteger
Mutable integer class for storing the data.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractMapBag()
Constructor needed for subclass serialisation.
|
protected |
AbstractMapBag(Map map)
Constructor that assigns the specified Map as the backing store.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(Object object)
Adds a new element to the bag, incrementing its count in the underlying map.
|
boolean |
add(Object object,
int nCopies)
Adds a new element to the bag, incrementing its count in the map.
|
boolean |
addAll(Collection coll)
Invokes
add(Object) for each element in the given collection. |
void |
clear()
Clears the bag by clearing the underlying map.
|
boolean |
contains(Object object)
Determines if the bag contains the given element by checking if the
underlying map contains the element as a key.
|
boolean |
containsAll(Collection coll)
Determines if the bag contains the given elements.
|
protected void |
doReadObject(Map map,
ObjectInputStream in)
Read the map in using a custom routine.
|
protected void |
doWriteObject(ObjectOutputStream out)
Write the map out using a custom routine.
|
boolean |
equals(Object object)
Compares this Bag to another.
|
int |
getCount(Object object)
Returns the number of occurrence of the given element in this bag
by looking up its count in the underlying map.
|
protected Map |
getMap()
Utility method for implementations to access the map that backs
this bag.
|
int |
hashCode()
Gets a hash code for the Bag compatible with the definition of equals.
|
boolean |
isEmpty()
Returns true if the underlying map is empty.
|
Iterator |
iterator()
Gets an iterator over the bag elements.
|
boolean |
remove(Object object)
Removes all copies of the specified object from the bag.
|
boolean |
remove(Object object,
int nCopies)
Removes a specified number of copies of an object from the bag.
|
boolean |
removeAll(Collection coll)
Removes objects from the bag according to their count in the specified collection.
|
boolean |
retainAll(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 this bag.
|
Object[] |
toArray()
Returns an array of all of this bag's elements.
|
Object[] |
toArray(Object[] array)
Returns an array of all of this bag's elements.
|
String |
toString()
Implement a toString() method suitable for debugging.
|
Set |
uniqueSet()
Returns an unmodifiable view of the underlying map's key set.
|
protected AbstractMapBag()
protected AbstractMapBag(Map map)
map
- the map to assignprotected Map getMap()
public int size()
size
in interface Collection
size
in interface Bag
public boolean isEmpty()
isEmpty
in interface Collection
public int getCount(Object object)
public boolean contains(Object object)
contains
in interface Collection
object
- the object to search forpublic boolean containsAll(Collection coll)
containsAll
in interface Collection
containsAll
in interface Bag
coll
- the collection to check againsttrue
if the Bag contains all the collectionpublic Iterator iterator()
public boolean add(Object object)
add
in interface Collection
add
in interface Bag
object
- the object to addtrue
if the object was not already in the uniqueSet
public boolean add(Object object, int nCopies)
public boolean addAll(Collection coll)
add(Object)
for each element in the given collection.addAll
in interface Collection
coll
- the collection to addtrue
if this call changed the bagpublic void clear()
clear
in interface Collection
public boolean remove(Object object)
remove
in interface Collection
remove
in interface Bag
object
- the object to removepublic boolean remove(Object object, int nCopies)
public boolean removeAll(Collection coll)
removeAll
in interface Collection
removeAll
in interface Bag
coll
- the collection to usepublic boolean retainAll(Collection coll)
retainAll
in interface Collection
retainAll
in interface Bag
coll
- the collection to retainpublic Object[] toArray()
toArray
in interface Collection
public Object[] toArray(Object[] array)
toArray
in interface Collection
array
- the array to populatepublic Set uniqueSet()
protected void doWriteObject(ObjectOutputStream out) throws IOException
out
- the output streamIOException
protected void doReadObject(Map map, ObjectInputStream in) throws IOException, ClassNotFoundException
map
- the map to usein
- the input streamIOException
ClassNotFoundException
public boolean equals(Object object)
equals
in interface Collection
equals
in class Object
object
- the Bag to compare topublic int hashCode()
(e==null ? 0 : e.hashCode()) ^ noOccurances)
.
This hash code is compatible with the Set interface.hashCode
in interface Collection
hashCode
in class Object
Copyright © 2001–2015 The Apache Software Foundation. All rights reserved.