Commons Collections学习笔记(二)

简介:
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 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();
    }
    
}
复制代码
 

复制代码
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();
    }
}
复制代码


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/12/17/1357122.html,如需转载请自行联系原作者
目录
相关文章
|
6月前
|
Java Maven
JAVA反序列化学习笔记4.Commons Collections2分析
JAVA反序列化学习笔记4.Commons Collections2分析
|
6月前
|
安全 Java
JAVA反序列化学习笔记3.Commons Collections5分析
JAVA反序列化学习笔记3.Commons Collections5分析
|
6月前
|
安全 Java
JAVA反序列化学习笔记2.Commons Collections1分析
JAVA反序列化学习笔记2.Commons Collections1分析
|
8月前
|
Java
Java 中Collections工具类的使用
Java 中Collections工具类的使用
36 0
|
11月前
|
安全 Java
Java安全之Commons Collections3分析
在学习完成前面的CC1链和CC2链后,其实再来看CC3链会比较轻松。
62 0
|
11月前
|
Java fastjson Shell
Commons-collections3 利用链分析笔记
Commons-collections3 利用链分析
|
11月前
|
安全 Java Maven
Commons Collections1 利用链分析笔记
Commons Collections1 利用链分析
|
12月前
commons-collections常用工具类
commons-collections常用工具类
74 0
|
Java 开发者
Collections 工具类|学习笔记
快速学习 Collections 工具类
Collections 工具类|学习笔记
Java:org.apache.commons.collections4.MapUtils获取Map数据
Java:org.apache.commons.collections4.MapUtils获取Map数据
186 0