本文共 5092 字,大约阅读时间需要 16 分钟。
package com.daxin.collections;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;/** * 返回指定 collection 的一个动态类型安全视图。试图插入一个错误类型的元素将导致立即抛出 * ClassCastException。假设在生成动态类型安全视图之前,collection 不包含任何类型不正确的元素,并且所有对该 collection * 的后续访问都通过该视图进行,则可以保证 该 collection 不包含类型不正确的元素。 */public class Main10 { public static void main(String[] args) { ArrayListlist = new ArrayList<>(); for (int i = 1; i <= 5; i++) { list.add(i); } Collection view = Collections.checkedCollection(list, Integer.class); System.out.println(view); view.add(11); view.add(12); view.add(13); System.out.println(view); System.out.println(list); }}
public staticCollection checkedCollection(Collection c, Class type) { return new CheckedCollection<>(c, type); }
/** * @serial include */ static class CheckedCollectionimplements Collection , Serializable { private static final long serialVersionUID = 1578914078182001775L; final Collection c; final Class type; void typeCheck(Object o) { if (o != null && !type.isInstance(o)) throw new ClassCastException(badElementMsg(o)); } private String badElementMsg(Object o) { return "Attempt to insert " + o.getClass() + " element into collection with element type " + type; } CheckedCollection(Collection c, Class type) { if (c==null || type == null) throw new NullPointerException(); this.c = c; this.type = type; } public int size() { return c.size(); } public boolean isEmpty() { return c.isEmpty(); } public boolean contains(Object o) { return c.contains(o); } public Object[] toArray() { return c.toArray(); } public T[] toArray(T[] a) { return c.toArray(a); } public String toString() { return c.toString(); } public boolean remove(Object o) { return c.remove(o); } public void clear() { c.clear(); } public boolean containsAll(Collection coll) { return c.containsAll(coll); } public boolean removeAll(Collection coll) { return c.removeAll(coll); } public boolean retainAll(Collection coll) { return c.retainAll(coll); } public Iterator iterator() { final Iterator it = c.iterator(); return new Iterator () { public boolean hasNext() { return it.hasNext(); } public E next() { return it.next(); } public void remove() { it.remove(); }}; } public boolean add(E e) { typeCheck(e); return c.add(e); } private E[] zeroLengthElementArray = null; // Lazily initialized private E[] zeroLengthElementArray() { return zeroLengthElementArray != null ? zeroLengthElementArray : (zeroLengthElementArray = zeroLengthArray(type)); } @SuppressWarnings("unchecked") Collection checkedCopyOf(Collection coll) { Object[] a = null; try { E[] z = zeroLengthElementArray(); a = coll.toArray(z); // Defend against coll violating the toArray contract if (a.getClass() != z.getClass()) a = Arrays.copyOf(a, a.length, z.getClass()); } catch (ArrayStoreException ignore) { // To get better and consistent diagnostics, // we call typeCheck explicitly on each element. // We call clone() to defend against coll retaining a // reference to the returned array and storing a bad // element into it after it has been type checked. a = coll.toArray().clone(); for (Object o : a) typeCheck(o); } // A slight abuse of the type system, but safe here. return (Collection ) Arrays.asList(a); } public boolean addAll(Collection coll) { // Doing things this way insulates us from concurrent changes // in the contents of coll and provides all-or-nothing // semantics (which we wouldn't get if we type-checked each // element as we added it) return c.addAll(checkedCopyOf(coll)); } }
public boolean add(E e) { typeCheck(e); return c.add(e); }
void typeCheck(Object o) { if (o != null && !type.isInstance(o)) throw new ClassCastException(badElementMsg(o)); }代码很简单,不做解释!!
转载地址:http://hkjlf.baihongyu.com/