Commons Collections学习笔记(二)2011-07-20 博客园 Phinecospublic 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() { return new BagIterator(this); } static class BagIterator implements Iterator { private AbstractMapBag parent; private Iterator entryIterator; private Map.Entry current; private int itemCount; private final int mods; private boolean canRemove; public BagIterator(AbstractMapBag parent) { this.parent = parent; this.entryIterator = parent.map.entrySet().iterator(); this.current = null; this.mods = parent.modCount; this.canRemove = false; } public boolean hasNext() { return (itemCount > 0 || entryIterator.hasNext()); } public Object next() { if (parent.modCount != mods) { throw new ConcurrentModificationException(); } if (itemCount == 0) { current = (Map.Entry) entryIterator.next(); itemCount = ((MutableInteger) current.getValue()).value; } canRemove = true; itemCount--; return current.getKey(); } public void remove() { if (parent.modCount != mods) { throw new ConcurrentModificationException(); } if (canRemove == false) { throw new IllegalStateException(); } MutableInteger mut = (MutableInteger) current.getValue(); if (mut.value > 1) { mut.value--; } else { entryIterator.remove(); } parent.size--; canRemove = false; } } public boolean add(Object object) { return add(object, 1); } public boolean add(Object object, int nCopies) { modCount++; if (nCopies > 0) { MutableInteger mut = (MutableInteger) map.get(object); size += nCopies; if (mut == null) { map.put(object, new MutableInteger(nCopies)); return true; } else { mut.value += nCopies; return false; } } else { return false; } } public boolean addAll(Collection coll) { boolean changed = false; Iterator i = coll.iterator(); while (i.hasNext()) { boolean added = add(i.next()); changed = changed || added; } return changed; } public void clear() { modCount++; map.clear(); size = 0; } public boolean remove(Object object) { MutableInteger mut = (MutableInteger) map.get(object); if (mut == null) { return false; } modCount++; map.remove(object); size -= mut.value; return true; } public boolean remove(Object object, int nCopies) { MutableInteger mut = (MutableInteger) map.get(object); if (mut == null) { return false; } if (nCopies <= 0) { return false; } modCount++; if (nCopies < mut.value) { mut.value -= nCopies; size -= nCopies; } else { map.remove(object); size -= mut.value; } return true; } public boolean removeAll(Collection coll) { boolean result = false; if (coll != null) { Iterator i = coll.iterator(); while (i.hasNext()) { boolean changed = remove(i.next(), 1); result = result || changed; } } return result; } public boolean retainAll(Collection coll) { if (coll instanceof Bag) { return retainAll((Bag) coll); } return retainAll(new HashBag(coll)); } boolean retainAll(Bag other) { boolean result = false; Bag excess = new HashBag(); Iterator i = uniqueSet().iterator(); while (i.hasNext()) { Object current = i.next(); int myCount = getCount(current); int otherCount = other.getCount(current); if (1 <= otherCount && otherCount <= myCount) { excess.add(current, myCount - otherCount); } else { excess.add(current, myCount); } } if (!excess.isEmpty()) { result = removeAll(excess); } return result; } protected static class MutableInteger { /** The value of this mutable. */ protected int value; MutableInteger(int value) { this.value = value; } public boolean equals(Object obj) { if (obj instanceof MutableInteger == false) { return false; } return ((MutableInteger) obj).value == value; } public int hashCode() { return value; } }
public Object[] toArray() { Object[] result = new Object[size()]; int i = 0; Iterator it = map.keySet().iterator(); while (it.hasNext()) { Object current = it.next(); for (int index = getCount(current); index > 0; index--) { result[i++] = current; } } return result; } public Object[] toArray(Object[] array) { int size = size(); if (array.length < size) { array = (Object[]) Array.newInstance(array.getClass().getComponentType(), size); } int i = 0; Iterator it = map.keySet().iterator(); while (it.hasNext()) { Object current = it.next(); for (int index = getCount(current); index > 0; index--) { array[i++] = current; } } if (array.length > size) { array[size] = null;//截断多余的空位 } return array; } public Set uniqueSet() { if (uniqueSet == null) { uniqueSet = UnmodifiableSet.decorate(map.keySet()); } return uniqueSet; } protected void doWriteObject(ObjectOutputStream out) throws IOException { out.writeInt(map.size()); for (Iterator it = map.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); out.writeObject(entry.getKey()); out.writeInt(((MutableInteger) entry.getValue()).value); } } protected void doReadObject(Map map, ObjectInputStream in) throws IOException, ClassNotFoundException { this.map = map; int entrySize = in.readInt(); for (int i = 0; i < entrySize; i++) { Object obj = in.readObject(); int count = in.readInt(); map.put(obj, new MutableInteger(count)); size += count; } } public boolean equals(Object object) { if (object == this) { return true; } if (object instanceof Bag == false) { return false; } Bag other = (Bag) object; if (other.size() != size()) { return false; } for (Iterator it = map.keySet().iterator(); it.hasNext();) { Object element = it.next(); if (other.getCount(element) != getCount(element)) { return false; } } return true; } public int hashCode() { int total = 0; for (Iterator it = map.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); Object element = entry.getKey(); MutableInteger count = (MutableInteger) entry.getValue(); total += (element == null ? 0 : element.hashCode()) ^ count.value; } return total; } public String toString() { if (size() == 0) { return "[]"; } StringBuffer buf = new StringBuffer(); buf.append("["); Iterator it = uniqueSet().iterator(); while (it.hasNext()) { Object current = it.next(); int count = getCount(current); buf.append(count); buf.append(":"); buf.append(current); if (it.hasNext()) { buf.append(","); } } buf.append("]"); return buf.toString(); }