Method from java.util.Collections$CheckedCollection Detail: |
public boolean add(E e) {
typeCheck(e);
return c.add(e);
}
|
public boolean addAll(Collection<? extends E> 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));
}
|
Collection<E> checkedCopyOf(Collection<? extends E> 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< E >) Arrays.asList(a);
}
|
public void clear() {
c.clear();
}
|
public boolean contains(Object o) {
return c.contains(o);
}
|
public boolean containsAll(Collection<?> coll) {
return c.containsAll(coll);
}
|
public boolean isEmpty() {
return c.isEmpty();
}
|
public Iterator<E> iterator() {
final Iterator< E > it = c.iterator();
return new Iterator< E >() {
public boolean hasNext() { return it.hasNext(); }
public E next() { return it.next(); }
public void remove() { it.remove(); }};
}
|
public boolean remove(Object o) {
return c.remove(o);
}
|
public boolean removeAll(Collection<?> coll) {
return c.removeAll(coll);
}
|
public boolean retainAll(Collection<?> coll) {
return c.retainAll(coll);
}
|
public int size() {
return c.size();
}
|
public Object[] toArray() {
return c.toArray();
}
|
public T[] toArray(T[] a) {
return c.toArray(a);
}
|
public String toString() {
return c.toString();
}
|
void typeCheck(Object o) {
if (o != null && !type.isInstance(o))
throw new ClassCastException(badElementMsg(o));
}
|