Commons Collections僥楼永芝?屈?
扮寂:2011-07-20 鴬人坩 Phinecos
public abstract class AbstractBagDecorator extends AbstractCollectionDecorator implements Bag {
protected AbstractBagDecorator() {
super();
}
protected AbstractBagDecorator(Bag bag) {
super(bag);
}
protected Bag getBag() {
return (Bag) getCollection();
}
public int getCount(Object object) {
return getBag().getCount(object);
}
public boolean add(Object object, int count) {
return getBag().add(object, count);
}
public boolean remove(Object object, int count) {
return getBag().remove(object, count);
}
public Set uniqueSet() {
return getBag().uniqueSet();
}
}
public abstract class AbstractSortedBagDecorator extends AbstractBagDecorator implements SortedBag
{
protected AbstractSortedBagDecorator() {
super();
}
protected AbstractSortedBagDecorator(SortedBag bag) {
super(bag);
}
protected SortedBag getSortedBag() {
return (SortedBag) getCollection();
}
public Object first() {
return getSortedBag().first();
}
public Object last() {
return getSortedBag().last();
}
public Comparator comparator() {
return getSortedBag().comparator();
}
}
public abstract class AbstractMapBag implements Bag
{
/** The map to use to store the data */
private transient Map map;
/** The current total size of the bag */
private int size;
/** The modification count for fail fast iterators */
private transient int modCount;
/** The modification count for fail fast iterators */
private transient Set uniqueSet;
protected AbstractMapBag() {
super();
}
protected AbstractMapBag(Map map) {
super();
this.map = map;
}
protected Map getMap() {
return map;
}
public int size() {
return size;
}
public boolean isEmpty() {
return map.isEmpty();
}
public int getCount(Object object) {
MutableInteger count = (MutableInteger) map.get(object);
if (count != null) {
return count.value;
}
return 0;
}
public boolean contains(Object object) {
return map.containsKey(object);
}
public boolean containsAll(Collection coll) {
if (coll instanceof Bag) {
return containsAll((Bag) coll);
}
return containsAll(new HashBag(coll));
}
boolean containsAll(Bag other) {
boolean result = true;
Iterator it = other.uniqueSet().iterator();
while (it.hasNext()) {
Object current = it.next();
boolean contains = getCount(current) >= other.getCount(current);
result = result && contains;
}
return result;
}
public Iterator iterator() {
|