11/*
2- * Copyright (c) 1997, 2013 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1997, 2022 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2525
2626package java .util ;
2727
28+ import jdk .internal .util .ArraysSupport ;
29+
2830/**
29- * This class provides a skeletal implementation of the <tt> Collection</tt>
31+ * This class provides a skeletal implementation of the {@code Collection}
3032 * interface, to minimize the effort required to implement this interface. <p>
3133 *
3234 * To implement an unmodifiable collection, the programmer needs only to
33- * extend this class and provide implementations for the <tt> iterator</tt> and
34- * <tt> size</tt> methods. (The iterator returned by the <tt> iterator</tt>
35- * method must implement <tt> hasNext</tt> and <tt> next</tt> .)<p>
35+ * extend this class and provide implementations for the {@code iterator} and
36+ * {@code size} methods. (The iterator returned by the {@code iterator}
37+ * method must implement {@code hasNext} and {@code next} .)<p>
3638 *
3739 * To implement a modifiable collection, the programmer must additionally
38- * override this class's <tt> add</tt> method (which otherwise throws an
39- * <tt> UnsupportedOperationException</tt> ), and the iterator returned by the
40- * <tt> iterator</tt> method must additionally implement its <tt> remove</tt>
40+ * override this class's {@code add} method (which otherwise throws an
41+ * {@code UnsupportedOperationException} ), and the iterator returned by the
42+ * {@code iterator} method must additionally implement its {@code remove}
4143 * method.<p>
4244 *
4345 * The programmer should generally provide a void (no argument) and
44- * <tt> Collection</tt> constructor, as per the recommendation in the
45- * <tt> Collection</tt> interface specification.<p>
46+ * {@code Collection} constructor, as per the recommendation in the
47+ * {@code Collection} interface specification.<p>
4648 *
4749 * The documentation for each non-abstract method in this class describes its
4850 * implementation in detail. Each of these methods may be overridden if
4951 * the collection being implemented admits a more efficient implementation.<p>
5052 *
5153 * This class is a member of the
52- * <a href="{@docRoot}/../technotes/guides/collections/index .html">
54+ * <a href="{@docRoot}/java.base/java/util/package-summary .html#CollectionsFramework ">
5355 * Java Collections Framework</a>.
5456 *
57+ * @param <E> the type of elements in this collection
58+ *
5559 * @author Josh Bloch
5660 * @author Neal Gafter
5761 * @see Collection
@@ -80,7 +84,8 @@ protected AbstractCollection() {
8084 /**
8185 * {@inheritDoc}
8286 *
83- * <p>This implementation returns <tt>size() == 0</tt>.
87+ * @implSpec
88+ * This implementation returns {@code size() == 0}.
8489 */
8590 public boolean isEmpty () {
8691 return size () == 0 ;
@@ -89,7 +94,8 @@ public boolean isEmpty() {
8994 /**
9095 * {@inheritDoc}
9196 *
92- * <p>This implementation iterates over the elements in the collection,
97+ * @implSpec
98+ * This implementation iterates over the elements in the collection,
9399 * checking each element in turn for equality with the specified element.
94100 *
95101 * @throws ClassCastException {@inheritDoc}
@@ -112,7 +118,8 @@ public boolean contains(Object o) {
112118 /**
113119 * {@inheritDoc}
114120 *
115- * <p>This implementation returns an array containing all the elements
121+ * @implSpec
122+ * This implementation returns an array containing all the elements
116123 * returned by this collection's iterator, in the same order, stored in
117124 * consecutive elements of the array, starting with index {@code 0}.
118125 * The length of the returned array is equal to the number of elements
@@ -146,7 +153,8 @@ public Object[] toArray() {
146153 /**
147154 * {@inheritDoc}
148155 *
149- * <p>This implementation returns an array containing all the elements
156+ * @implSpec
157+ * This implementation returns an array containing all the elements
150158 * returned by this collection's iterator in the same order, stored in
151159 * consecutive elements of the array, starting with index {@code 0}.
152160 * If the number of elements returned by the iterator is too large to
@@ -199,14 +207,6 @@ public <T> T[] toArray(T[] a) {
199207 return it .hasNext () ? finishToArray (r , it ) : r ;
200208 }
201209
202- /**
203- * The maximum size of array to allocate.
204- * Some VMs reserve some header words in an array.
205- * Attempts to allocate larger arrays may result in
206- * OutOfMemoryError: Requested array size exceeds VM limit
207- */
208- private static final int MAX_ARRAY_SIZE = Integer .MAX_VALUE - 8 ;
209-
210210 /**
211211 * Reallocates the array being used within toArray when the iterator
212212 * returned more elements than expected, and finishes filling it from
@@ -219,38 +219,29 @@ public <T> T[] toArray(T[] a) {
219219 */
220220 @ SuppressWarnings ("unchecked" )
221221 private static <T > T [] finishToArray (T [] r , Iterator <?> it ) {
222- int i = r .length ;
222+ int len = r .length ;
223+ int i = len ;
223224 while (it .hasNext ()) {
224- int cap = r .length ;
225- if (i == cap ) {
226- int newCap = cap + (cap >> 1 ) + 1 ;
227- // overflow-conscious code
228- if (newCap - MAX_ARRAY_SIZE > 0 )
229- newCap = hugeCapacity (cap + 1 );
230- r = Arrays .copyOf (r , newCap );
225+ if (i == len ) {
226+ len = ArraysSupport .newLength (len ,
227+ 1 , /* minimum growth */
228+ (len >> 1 ) + 1 /* preferred growth */ );
229+ r = Arrays .copyOf (r , len );
231230 }
232231 r [i ++] = (T )it .next ();
233232 }
234233 // trim if overallocated
235- return (i == r .length ) ? r : Arrays .copyOf (r , i );
236- }
237-
238- private static int hugeCapacity (int minCapacity ) {
239- if (minCapacity < 0 ) // overflow
240- throw new OutOfMemoryError
241- ("Required array size too large" );
242- return (minCapacity > MAX_ARRAY_SIZE ) ?
243- Integer .MAX_VALUE :
244- MAX_ARRAY_SIZE ;
234+ return (i == len ) ? r : Arrays .copyOf (r , i );
245235 }
246236
247237 // Modification Operations
248238
249239 /**
250240 * {@inheritDoc}
251241 *
252- * <p>This implementation always throws an
253- * <tt>UnsupportedOperationException</tt>.
242+ * @implSpec
243+ * This implementation always throws an
244+ * {@code UnsupportedOperationException}.
254245 *
255246 * @throws UnsupportedOperationException {@inheritDoc}
256247 * @throws ClassCastException {@inheritDoc}
@@ -265,13 +256,14 @@ public boolean add(E e) {
265256 /**
266257 * {@inheritDoc}
267258 *
268- * <p>This implementation iterates over the collection looking for the
259+ * @implSpec
260+ * This implementation iterates over the collection looking for the
269261 * specified element. If it finds the element, it removes the element
270262 * from the collection using the iterator's remove method.
271263 *
272264 * <p>Note that this implementation throws an
273- * <tt> UnsupportedOperationException</tt> if the iterator returned by this
274- * collection's iterator method does not implement the <tt> remove</tt>
265+ * {@code UnsupportedOperationException} if the iterator returned by this
266+ * collection's iterator method does not implement the {@code remove}
275267 * method and this collection contains the specified object.
276268 *
277269 * @throws UnsupportedOperationException {@inheritDoc}
@@ -304,10 +296,11 @@ public boolean remove(Object o) {
304296 /**
305297 * {@inheritDoc}
306298 *
307- * <p>This implementation iterates over the specified collection,
299+ * @implSpec
300+ * This implementation iterates over the specified collection,
308301 * checking each element returned by the iterator in turn to see
309302 * if it's contained in this collection. If all elements are so
310- * contained <tt> true</tt> is returned, otherwise <tt> false</tt> .
303+ * contained {@code true} is returned, otherwise {@code false} .
311304 *
312305 * @throws ClassCastException {@inheritDoc}
313306 * @throws NullPointerException {@inheritDoc}
@@ -323,11 +316,12 @@ public boolean containsAll(Collection<?> c) {
323316 /**
324317 * {@inheritDoc}
325318 *
326- * <p>This implementation iterates over the specified collection, and adds
319+ * @implSpec
320+ * This implementation iterates over the specified collection, and adds
327321 * each object returned by the iterator to this collection, in turn.
328322 *
329323 * <p>Note that this implementation will throw an
330- * <tt> UnsupportedOperationException</tt> unless <tt> add</tt> is
324+ * {@code UnsupportedOperationException} unless {@code add} is
331325 * overridden (assuming the specified collection is non-empty).
332326 *
333327 * @throws UnsupportedOperationException {@inheritDoc}
@@ -349,14 +343,15 @@ public boolean addAll(Collection<? extends E> c) {
349343 /**
350344 * {@inheritDoc}
351345 *
352- * <p>This implementation iterates over this collection, checking each
346+ * @implSpec
347+ * This implementation iterates over this collection, checking each
353348 * element returned by the iterator in turn to see if it's contained
354349 * in the specified collection. If it's so contained, it's removed from
355- * this collection with the iterator's <tt> remove</tt> method.
350+ * this collection with the iterator's {@code remove} method.
356351 *
357352 * <p>Note that this implementation will throw an
358- * <tt> UnsupportedOperationException</tt> if the iterator returned by the
359- * <tt> iterator</tt> method does not implement the <tt> remove</tt> method
353+ * {@code UnsupportedOperationException} if the iterator returned by the
354+ * {@code iterator} method does not implement the {@code remove} method
360355 * and this collection contains one or more elements in common with the
361356 * specified collection.
362357 *
@@ -383,14 +378,15 @@ public boolean removeAll(Collection<?> c) {
383378 /**
384379 * {@inheritDoc}
385380 *
386- * <p>This implementation iterates over this collection, checking each
381+ * @implSpec
382+ * This implementation iterates over this collection, checking each
387383 * element returned by the iterator in turn to see if it's contained
388384 * in the specified collection. If it's not so contained, it's removed
389- * from this collection with the iterator's <tt> remove</tt> method.
385+ * from this collection with the iterator's {@code remove} method.
390386 *
391387 * <p>Note that this implementation will throw an
392- * <tt> UnsupportedOperationException</tt> if the iterator returned by the
393- * <tt> iterator</tt> method does not implement the <tt> remove</tt> method
388+ * {@code UnsupportedOperationException} if the iterator returned by the
389+ * {@code iterator} method does not implement the {@code remove} method
394390 * and this collection contains one or more elements not present in the
395391 * specified collection.
396392 *
@@ -417,15 +413,16 @@ public boolean retainAll(Collection<?> c) {
417413 /**
418414 * {@inheritDoc}
419415 *
420- * <p>This implementation iterates over this collection, removing each
421- * element using the <tt>Iterator.remove</tt> operation. Most
416+ * @implSpec
417+ * This implementation iterates over this collection, removing each
418+ * element using the {@code Iterator.remove} operation. Most
422419 * implementations will probably choose to override this method for
423420 * efficiency.
424421 *
425422 * <p>Note that this implementation will throw an
426- * <tt> UnsupportedOperationException</tt> if the iterator returned by this
427- * collection's <tt> iterator</tt> method does not implement the
428- * <tt> remove</tt> method and this collection is non-empty.
423+ * {@code UnsupportedOperationException} if the iterator returned by this
424+ * collection's {@code iterator} method does not implement the
425+ * {@code remove} method and this collection is non-empty.
429426 *
430427 * @throws UnsupportedOperationException {@inheritDoc}
431428 */
@@ -444,8 +441,8 @@ public void clear() {
444441 * Returns a string representation of this collection. The string
445442 * representation consists of a list of the collection's elements in the
446443 * order they are returned by its iterator, enclosed in square brackets
447- * (<tt> "[]"</tt> ). Adjacent elements are separated by the characters
448- * <tt> ", "</tt> (comma and space). Elements are converted to strings as
444+ * ({@code "[]"} ). Adjacent elements are separated by the characters
445+ * {@code ", "} (comma and space). Elements are converted to strings as
449446 * by {@link String#valueOf(Object)}.
450447 *
451448 * @return a string representation of this collection
0 commit comments