CommonsCollections学习笔记(二)
来源:优易学  2011-1-3 12:26:00   【优易学:中国教育考试门户网】   资料下载   IT书店

  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;
  }

[1] [2] [3] [4] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训