CommonsCollections学习笔记(二)

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() {     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 therCount = other.getCount(current);       if (1 <= otherCount && otherCount 0; index--) {         result[i++] = current;       }     }     return result;   }   public Object[] toArray(Object[] array) {     int size = size();     if (array.length 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 bj = 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 ther = (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();   }}

public class HashBag extends AbstractMapBag implements Bag, Serializable

{   private static final long serialVersionUID = -6561115435802554013L;   public HashBag() {     super(new HashMap());   }   public HashBag(Collection coll) {     this();     addAll(coll);   }   private void writeObject(ObjectOutputStream out) throws IOException {     out.defaultWriteObject();     super.doWriteObject(out);   }   private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {     in.defaultReadObject();     super.doReadObject(new HashMap(), in);   }}

public class TreeBag extends AbstractMapBag implements SortedBag, Serializable

{   private static final long serialVersionUID = -7740146511091606676L;   public TreeBag() {     super(new TreeMap());   }   public TreeBag(ComparaTor comparaTor) {     super(new TreeMap(comparaTor));   }   public TreeBag(Collection coll) {     this();     addAll(coll);   }   public Object first() {     return ((SortedMap) getMap()).firstKey();   }   public Object last() {     return ((SortedMap) getMap()).lastKey();   }   public ComparaTor comparaTor() {     return ((SortedMap) getMap()).comparaTor();   }   private void writeObject(ObjectOutputStream out) throws IOException {     out.defaultWriteObject();     out.writeObject(comparaTor());     super.doWriteObject(out);   }   private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {     in.defaultReadObject();     ComparaTor comp = (ComparaTor) in.readObject();     super.doReadObject(new TreeMap(comp), in);   }}

public class PredicatedBag extends PredicatedCollection implements Bag

{   private static final long serialVersionUID = -2575833140344736876L;   public static Bag decorate(Bag bag, Predicate predicate) {     return new PredicatedBag(bag, predicate);   }   protected PredicatedBag(Bag bag, Predicate predicate) {     super(bag, predicate);   }   protected Bag getBag() {     return (Bag) getCollection();   }   public boolean add(Object object, int count) {     validate(object);     return getBag().add(object, count);   }   public boolean remove(Object object, int count) {     return getBag().remove(object, count);   }   public Set uniqueSet() {     return getBag().uniqueSet();   }   public int getCount(Object object) {     return getBag().getCount(object);   }}

public class PredicatedSortedBag extends PredicatedBag implements SortedBag

{   private static final long serialVersionUID = 3448581314086406616L;   public static SortedBag decorate(SortedBag bag, Predicate predicate) {     return new PredicatedSortedBag(bag, predicate);   }   protected PredicatedSortedBag(SortedBag bag, Predicate predicate) {     super(bag, predicate);   }   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 class TypedBag

{   public static Bag decorate(Bag bag, Class type) {     return new PredicatedBag(bag, InstanceofPredicate.getInstance(type));   }   protected TypedBag() {     super();   }}public class TypedSortedBag{   public static SortedBag decorate(SortedBag bag, Class type) {     return new PredicatedSortedBag(bag, InstanceofPredicate.getInstance(type));   }protected TypedSortedBag(){     super();   }}

旅行要学会随遇而安,淡然一点,

CommonsCollections学习笔记(二)

相关文章:

你感兴趣的文章:

标签云: