3060 public boolean containsAll(Collection<?> coll) {
3061 return c.containsAll(coll);
3062 }
3063 public boolean removeAll(Collection<?> coll) {
3064 return c.removeAll(coll);
3065 }
3066 public boolean retainAll(Collection<?> coll) {
3067 return c.retainAll(coll);
3068 }
3069
3070 public Iterator<E> iterator() {
3071 // JDK-6363904 - unwrapped iterator could be typecast to
3072 // ListIterator with unsafe set()
3073 final Iterator<E> it = c.iterator();
3074 return new Iterator<E>() {
3075 public boolean hasNext() { return it.hasNext(); }
3076 public E next() { return it.next(); }
3077 public void remove() { it.remove(); }};
3078 }
3079
3080 public boolean add(E e) {
3081 typeCheck(e);
3082 return c.add(e);
3083 }
3084
3085 private E[] zeroLengthElementArray; // Lazily initialized
3086
3087 private E[] zeroLengthElementArray() {
3088 return zeroLengthElementArray != null ? zeroLengthElementArray :
3089 (zeroLengthElementArray = zeroLengthArray(type));
3090 }
3091
3092 @SuppressWarnings("unchecked")
3093 Collection<E> checkedCopyOf(Collection<? extends E> coll) {
3094 Object[] a;
3095 try {
3096 E[] z = zeroLengthElementArray();
3097 a = coll.toArray(z);
3098 // Defend against coll violating the toArray contract
3099 if (a.getClass() != z.getClass())
3100 a = Arrays.copyOf(a, a.length, z.getClass());
3101 } catch (ArrayStoreException ignore) {
3102 // To get better and consistent diagnostics,
3103 // we call typeCheck explicitly on each element.
3170 * @serial include
3171 */
3172 static class CheckedQueue<E>
3173 extends CheckedCollection<E>
3174 implements Queue<E>, Serializable
3175 {
3176 private static final long serialVersionUID = 1433151992604707767L;
3177 final Queue<E> queue;
3178
3179 CheckedQueue(Queue<E> queue, Class<E> elementType) {
3180 super(queue, elementType);
3181 this.queue = queue;
3182 }
3183
3184 public E element() {return queue.element();}
3185 public boolean equals(Object o) {return o == this || c.equals(o);}
3186 public int hashCode() {return c.hashCode();}
3187 public E peek() {return queue.peek();}
3188 public E poll() {return queue.poll();}
3189 public E remove() {return queue.remove();}
3190
3191 public boolean offer(E e) {
3192 typeCheck(e);
3193 return add(e);
3194 }
3195 }
3196
3197 /**
3198 * Returns a dynamically typesafe view of the specified set.
3199 * Any attempt to insert an element of the wrong type will result in
3200 * an immediate {@link ClassCastException}. Assuming a set contains
3201 * no incorrectly typed elements prior to the time a dynamically typesafe
3202 * view is generated, and that all subsequent access to the set
3203 * takes place through the view, it is <i>guaranteed</i> that the
3204 * set cannot contain an incorrectly typed element.
3205 *
3206 * <p>A discussion of the use of dynamically typesafe views may be
3207 * found in the documentation for the {@link #checkedCollection
3208 * checkedCollection} method.
3209 *
3210 * <p>The returned set will be serializable if the specified set is
3211 * serializable.
3212 *
3213 * <p>Since {@code null} is considered to be a value of any reference
3214 * type, the returned set permits insertion of null elements whenever
3423 static class CheckedList<E>
3424 extends CheckedCollection<E>
3425 implements List<E>
3426 {
3427 private static final long serialVersionUID = 65247728283967356L;
3428 final List<E> list;
3429
3430 CheckedList(List<E> list, Class<E> type) {
3431 super(list, type);
3432 this.list = list;
3433 }
3434
3435 public boolean equals(Object o) { return o == this || list.equals(o); }
3436 public int hashCode() { return list.hashCode(); }
3437 public E get(int index) { return list.get(index); }
3438 public E remove(int index) { return list.remove(index); }
3439 public int indexOf(Object o) { return list.indexOf(o); }
3440 public int lastIndexOf(Object o) { return list.lastIndexOf(o); }
3441
3442 public E set(int index, E element) {
3443 typeCheck(element);
3444 return list.set(index, element);
3445 }
3446
3447 public void add(int index, E element) {
3448 typeCheck(element);
3449 list.add(index, element);
3450 }
3451
3452 public boolean addAll(int index, Collection<? extends E> c) {
3453 return list.addAll(index, checkedCopyOf(c));
3454 }
3455 public ListIterator<E> listIterator() { return listIterator(0); }
3456
3457 public ListIterator<E> listIterator(final int index) {
3458 final ListIterator<E> i = list.listIterator(index);
3459
3460 return new ListIterator<E>() {
3461 public boolean hasNext() { return i.hasNext(); }
3462 public E next() { return i.next(); }
3463 public boolean hasPrevious() { return i.hasPrevious(); }
3464 public E previous() { return i.previous(); }
3465 public int nextIndex() { return i.nextIndex(); }
3466 public int previousIndex() { return i.previousIndex(); }
3467 public void remove() { i.remove(); }
3468
3469 public void set(E e) {
3470 typeCheck(e);
3471 i.set(e);
3472 }
3473
3474 public void add(E e) {
3475 typeCheck(e);
3476 i.add(e);
3477 }
3478
3479 @Override
3480 public void forEachRemaining(Consumer<? super E> action) {
3481 i.forEachRemaining(action);
3482 }
3483 };
3484 }
3485
3486 public List<E> subList(int fromIndex, int toIndex) {
3487 return new CheckedList<>(list.subList(fromIndex, toIndex), type);
3488 }
3489
3490 /**
3491 * {@inheritDoc}
3492 *
3493 * @throws ClassCastException if the class of an element returned by the
3494 * operator prevents it from being added to this collection. The
3495 * exception may be thrown after some elements of the list have
3496 * already been replaced.
|
3060 public boolean containsAll(Collection<?> coll) {
3061 return c.containsAll(coll);
3062 }
3063 public boolean removeAll(Collection<?> coll) {
3064 return c.removeAll(coll);
3065 }
3066 public boolean retainAll(Collection<?> coll) {
3067 return c.retainAll(coll);
3068 }
3069
3070 public Iterator<E> iterator() {
3071 // JDK-6363904 - unwrapped iterator could be typecast to
3072 // ListIterator with unsafe set()
3073 final Iterator<E> it = c.iterator();
3074 return new Iterator<E>() {
3075 public boolean hasNext() { return it.hasNext(); }
3076 public E next() { return it.next(); }
3077 public void remove() { it.remove(); }};
3078 }
3079
3080 public boolean add(E e) { return c.add(typeCheck(e)); }
3081
3082 private E[] zeroLengthElementArray; // Lazily initialized
3083
3084 private E[] zeroLengthElementArray() {
3085 return zeroLengthElementArray != null ? zeroLengthElementArray :
3086 (zeroLengthElementArray = zeroLengthArray(type));
3087 }
3088
3089 @SuppressWarnings("unchecked")
3090 Collection<E> checkedCopyOf(Collection<? extends E> coll) {
3091 Object[] a;
3092 try {
3093 E[] z = zeroLengthElementArray();
3094 a = coll.toArray(z);
3095 // Defend against coll violating the toArray contract
3096 if (a.getClass() != z.getClass())
3097 a = Arrays.copyOf(a, a.length, z.getClass());
3098 } catch (ArrayStoreException ignore) {
3099 // To get better and consistent diagnostics,
3100 // we call typeCheck explicitly on each element.
3167 * @serial include
3168 */
3169 static class CheckedQueue<E>
3170 extends CheckedCollection<E>
3171 implements Queue<E>, Serializable
3172 {
3173 private static final long serialVersionUID = 1433151992604707767L;
3174 final Queue<E> queue;
3175
3176 CheckedQueue(Queue<E> queue, Class<E> elementType) {
3177 super(queue, elementType);
3178 this.queue = queue;
3179 }
3180
3181 public E element() {return queue.element();}
3182 public boolean equals(Object o) {return o == this || c.equals(o);}
3183 public int hashCode() {return c.hashCode();}
3184 public E peek() {return queue.peek();}
3185 public E poll() {return queue.poll();}
3186 public E remove() {return queue.remove();}
3187 public boolean offer(E e) {return queue.offer(typeCheck(e));}
3188 }
3189
3190 /**
3191 * Returns a dynamically typesafe view of the specified set.
3192 * Any attempt to insert an element of the wrong type will result in
3193 * an immediate {@link ClassCastException}. Assuming a set contains
3194 * no incorrectly typed elements prior to the time a dynamically typesafe
3195 * view is generated, and that all subsequent access to the set
3196 * takes place through the view, it is <i>guaranteed</i> that the
3197 * set cannot contain an incorrectly typed element.
3198 *
3199 * <p>A discussion of the use of dynamically typesafe views may be
3200 * found in the documentation for the {@link #checkedCollection
3201 * checkedCollection} method.
3202 *
3203 * <p>The returned set will be serializable if the specified set is
3204 * serializable.
3205 *
3206 * <p>Since {@code null} is considered to be a value of any reference
3207 * type, the returned set permits insertion of null elements whenever
3416 static class CheckedList<E>
3417 extends CheckedCollection<E>
3418 implements List<E>
3419 {
3420 private static final long serialVersionUID = 65247728283967356L;
3421 final List<E> list;
3422
3423 CheckedList(List<E> list, Class<E> type) {
3424 super(list, type);
3425 this.list = list;
3426 }
3427
3428 public boolean equals(Object o) { return o == this || list.equals(o); }
3429 public int hashCode() { return list.hashCode(); }
3430 public E get(int index) { return list.get(index); }
3431 public E remove(int index) { return list.remove(index); }
3432 public int indexOf(Object o) { return list.indexOf(o); }
3433 public int lastIndexOf(Object o) { return list.lastIndexOf(o); }
3434
3435 public E set(int index, E element) {
3436 return list.set(index, typeCheck(element));
3437 }
3438
3439 public void add(int index, E element) {
3440 list.add(index, typeCheck(element));
3441 }
3442
3443 public boolean addAll(int index, Collection<? extends E> c) {
3444 return list.addAll(index, checkedCopyOf(c));
3445 }
3446 public ListIterator<E> listIterator() { return listIterator(0); }
3447
3448 public ListIterator<E> listIterator(final int index) {
3449 final ListIterator<E> i = list.listIterator(index);
3450
3451 return new ListIterator<E>() {
3452 public boolean hasNext() { return i.hasNext(); }
3453 public E next() { return i.next(); }
3454 public boolean hasPrevious() { return i.hasPrevious(); }
3455 public E previous() { return i.previous(); }
3456 public int nextIndex() { return i.nextIndex(); }
3457 public int previousIndex() { return i.previousIndex(); }
3458 public void remove() { i.remove(); }
3459
3460 public void set(E e) {
3461 i.set(typeCheck(e));
3462 }
3463
3464 public void add(E e) {
3465 i.add(typeCheck(e));
3466 }
3467
3468 @Override
3469 public void forEachRemaining(Consumer<? super E> action) {
3470 i.forEachRemaining(action);
3471 }
3472 };
3473 }
3474
3475 public List<E> subList(int fromIndex, int toIndex) {
3476 return new CheckedList<>(list.subList(fromIndex, toIndex), type);
3477 }
3478
3479 /**
3480 * {@inheritDoc}
3481 *
3482 * @throws ClassCastException if the class of an element returned by the
3483 * operator prevents it from being added to this collection. The
3484 * exception may be thrown after some elements of the list have
3485 * already been replaced.
|