ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractCollection.java
(Generate patch)

Comparing jsr166/src/main/java/util/AbstractCollection.java (file contents):
Revision 1.8 by jsr166, Sun May 28 23:36:29 2006 UTC vs.
Revision 1.15 by jsr166, Sun Sep 5 21:32:18 2010 UTC

# Line 1 | Line 1
1   /*
2 < * %W% %E%
2 > * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
3 > * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4   *
5 < * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
6 < * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
5 > * This code is free software; you can redistribute it and/or modify it
6 > * under the terms of the GNU General Public License version 2 only, as
7 > * published by the Free Software Foundation.  Sun designates this
8 > * particular file as subject to the "Classpath" exception as provided
9 > * by Sun in the LICENSE file that accompanied this code.
10 > *
11 > * This code is distributed in the hope that it will be useful, but WITHOUT
12 > * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 > * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 > * version 2 for more details (a copy is included in the LICENSE file that
15 > * accompanied this code).
16 > *
17 > * You should have received a copy of the GNU General Public License version
18 > * 2 along with this work; if not, write to the Free Software Foundation,
19 > * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 > *
21 > * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 > * or visit www.oracle.com if you need additional information or have any
23 > * questions.
24   */
25  
26   package java.util;
# Line 26 | Line 44 | package java.util;
44   * <tt>Collection</tt> constructor, as per the recommendation in the
45   * <tt>Collection</tt> interface specification.<p>
46   *
47 < * The documentation for each non-abstract methods in this class describes its
47 > * The documentation for each non-abstract method in this class describes its
48   * implementation in detail.  Each of these methods may be overridden if
49   * the collection being implemented admits a more efficient implementation.<p>
50   *
# Line 36 | Line 54 | package java.util;
54   *
55   * @author  Josh Bloch
56   * @author  Neal Gafter
39 * @version %I%, %G%
57   * @see Collection
58   * @since 1.2
59   */
# Line 66 | Line 83 | public abstract class AbstractCollection
83       * <p>This implementation returns <tt>size() == 0</tt>.
84       */
85      public boolean isEmpty() {
86 <        return size() == 0;
86 >        return size() == 0;
87      }
88  
89      /**
# Line 79 | Line 96 | public abstract class AbstractCollection
96       * @throws NullPointerException {@inheritDoc}
97       */
98      public boolean contains(Object o) {
99 <        Iterator<E> e = iterator();
100 <        if (o==null) {
101 <            while (e.hasNext())
102 <                if (e.next()==null)
103 <                    return true;
104 <        } else {
105 <            while (e.hasNext())
106 <                if (o.equals(e.next()))
107 <                    return true;
108 <        }
109 <        return false;
99 >        Iterator<E> it = iterator();
100 >        if (o==null) {
101 >            while (it.hasNext())
102 >                if (it.next()==null)
103 >                    return true;
104 >        } else {
105 >            while (it.hasNext())
106 >                if (o.equals(it.next()))
107 >                    return true;
108 >        }
109 >        return false;
110      }
111  
112      /**
113       * {@inheritDoc}
114       *
115 <     * <p>This implementation allocates the array to be returned, and iterates
116 <     * over the elements in the collection, storing each object reference in
117 <     * the next consecutive element of the array, starting with element 0.
115 >     * <p>This implementation returns an array containing all the elements
116 >     * returned by this collection's iterator, in the same order, stored in
117 >     * consecutive elements of the array, starting with index {@code 0}.
118 >     * The length of the returned array is equal to the number of elements
119 >     * returned by the iterator, even if the size of this collection changes
120 >     * during iteration, as might happen if the collection permits
121 >     * concurrent modification during iteration.  The {@code size} method is
122 >     * called only as an optimization hint; the correct result is returned
123 >     * even if the iterator returns a different number of elements.
124 >     *
125 >     * <p>This method is equivalent to:
126 >     *
127 >     *  <pre> {@code
128 >     * List<E> list = new ArrayList<E>(size());
129 >     * for (E e : this)
130 >     *     list.add(e);
131 >     * return list.toArray();
132 >     * }</pre>
133       */
134      public Object[] toArray() {
135          // Estimate size of array; be prepared to see more or fewer elements
136 <        Object[] r = new Object[size()];
105 <        int i = 0;
136 >        Object[] r = new Object[size()];
137          Iterator<E> it = iterator();
138 <        while (i < r.length && it.hasNext())
139 <            r[i++] = it.next();
140 <        // Trim if overallocated; expand if underallocated
141 <        if (i < r.length || it.hasNext())
142 <            return resizeAndFinishToArray(r, i, it);
143 <        return r;
138 >        for (int i = 0; i < r.length; i++) {
139 >            if (! it.hasNext()) // fewer elements than expected
140 >                return Arrays.copyOf(r, i);
141 >            r[i] = it.next();
142 >        }
143 >        return it.hasNext() ? finishToArray(r, it) : r;
144      }
145  
146      /**
147       * {@inheritDoc}
148       *
149 <     * <p>This implementation checks if the array is large enough to contain the
150 <     * collection; if not, it allocates a new array of the correct size and
151 <     * type (using reflection).  Then, it iterates over the collection,
152 <     * storing each object reference in the next consecutive element of the
153 <     * array, starting with element 0.  If the array is larger than the
154 <     * collection, a <tt>null</tt> is stored in the first location after the
155 <     * end of the collection.
149 >     * <p>This implementation returns an array containing all the elements
150 >     * returned by this collection's iterator in the same order, stored in
151 >     * consecutive elements of the array, starting with index {@code 0}.
152 >     * If the number of elements returned by the iterator is too large to
153 >     * fit into the specified array, then the elements are returned in a
154 >     * newly allocated array with length equal to the number of elements
155 >     * returned by the iterator, even if the size of this collection
156 >     * changes during iteration, as might happen if the collection permits
157 >     * concurrent modification during iteration.  The {@code size} method is
158 >     * called only as an optimization hint; the correct result is returned
159 >     * even if the iterator returns a different number of elements.
160 >     *
161 >     * <p>This method is equivalent to:
162 >     *
163 >     *  <pre> {@code
164 >     * List<E> list = new ArrayList<E>(size());
165 >     * for (E e : this)
166 >     *     list.add(e);
167 >     * return list.toArray(a);
168 >     * }</pre>
169       *
170       * @throws ArrayStoreException  {@inheritDoc}
171       * @throws NullPointerException {@inheritDoc}
# Line 132 | Line 176 | public abstract class AbstractCollection
176          T[] r = a.length >= size ? a :
177                    (T[])java.lang.reflect.Array
178                    .newInstance(a.getClass().getComponentType(), size);
135        int i = 0;
179          Iterator<E> it = iterator();
180 <        while (i < r.length && it.hasNext())
181 <            r[i++] = (T)it.next();
182 <        // Trim if overallocated; expand if underallocated
183 <        if (it.hasNext() || (r != a && i < r.length))
184 <            return resizeAndFinishToArray(r, i, it);
185 <        if (i < r.length)
186 <            r[i] = null; // null-terminate if provided array is too big
187 <        return r;
180 >
181 >        for (int i = 0; i < r.length; i++) {
182 >            if (! it.hasNext()) { // fewer elements than expected
183 >                if (a != r)
184 >                    return Arrays.copyOf(r, i);
185 >                r[i] = null; // null-terminate
186 >                return r;
187 >            }
188 >            r[i] = (T)it.next();
189 >        }
190 >        return it.hasNext() ? finishToArray(r, it) : r;
191      }
192  
193      /**
194 <     * Reallocates the array being used within toArray that has a
195 <     * different number of elements than expected, and finishes
196 <     * filling it from the given iterator, if necessary.
197 <     *
198 <     * @param r the array
199 <     * @param i the next array index to fill
154 <     * @param it the in-progress iterator over the collection
194 >     * Reallocates the array being used within toArray when the iterator
195 >     * returned more elements than expected, and finishes filling it from
196 >     * the iterator.
197 >     *
198 >     * @param r the array, replete with previously stored elements
199 >     * @param it the in-progress iterator over this collection
200       * @return array containing the elements in the given array, plus any
201       *         further elements returned by the iterator, trimmed to size
202       */
203 <    private static <T> T[] resizeAndFinishToArray(T[] r, int i, Iterator<?> it) {
203 >    private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
204 >        int i = r.length;
205          while (it.hasNext()) {
206              int cap = r.length;
207 <            if (i < cap)
162 <                r[i++] = (T)it.next();
163 <            else {
207 >            if (i == cap) {
208                  int newCap = ((cap / 2) + 1) * 3;
209                  if (newCap <= cap) { // integer overflow
210 <                    if (cap == Integer.MAX_VALUE)
211 <                        throw new OutOfMemoryError
212 <                            ("Required array size too large");
210 >                    if (cap == Integer.MAX_VALUE)
211 >                        throw new OutOfMemoryError
212 >                            ("Required array size too large");
213                      newCap = Integer.MAX_VALUE;
214 <                }
214 >                }
215                  r = Arrays.copyOf(r, newCap);
216              }
217 +            r[i++] = (T)it.next();
218          }
219          // trim if overallocated
220 <        return i == r.length ? r : Arrays.copyOf(r, i);
220 >        return (i == r.length) ? r : Arrays.copyOf(r, i);
221      }
222  
223      // Modification Operations
# Line 190 | Line 235 | public abstract class AbstractCollection
235       * @throws IllegalStateException         {@inheritDoc}
236       */
237      public boolean add(E e) {
238 <        throw new UnsupportedOperationException();
238 >        throw new UnsupportedOperationException();
239      }
240  
241      /**
# Line 210 | Line 255 | public abstract class AbstractCollection
255       * @throws NullPointerException          {@inheritDoc}
256       */
257      public boolean remove(Object o) {
258 <        Iterator<E> e = iterator();
259 <        if (o==null) {
260 <            while (e.hasNext()) {
261 <                if (e.next()==null) {
262 <                    e.remove();
263 <                    return true;
264 <                }
265 <            }
266 <        } else {
267 <            while (e.hasNext()) {
268 <                if (o.equals(e.next())) {
269 <                    e.remove();
270 <                    return true;
271 <                }
272 <            }
273 <        }
274 <        return false;
258 >        Iterator<E> it = iterator();
259 >        if (o==null) {
260 >            while (it.hasNext()) {
261 >                if (it.next()==null) {
262 >                    it.remove();
263 >                    return true;
264 >                }
265 >            }
266 >        } else {
267 >            while (it.hasNext()) {
268 >                if (o.equals(it.next())) {
269 >                    it.remove();
270 >                    return true;
271 >                }
272 >            }
273 >        }
274 >        return false;
275      }
276  
277  
# Line 245 | Line 290 | public abstract class AbstractCollection
290       * @see #contains(Object)
291       */
292      public boolean containsAll(Collection<?> c) {
293 <        Iterator<?> e = c.iterator();
294 <        while (e.hasNext())
295 <            if (!contains(e.next()))
296 <                return false;
297 <        return true;
293 >        Iterator<?> it = c.iterator();
294 >        while (it.hasNext())
295 >            if (!contains(it.next()))
296 >                return false;
297 >        return true;
298      }
299  
300      /**
# Line 271 | Line 316 | public abstract class AbstractCollection
316       * @see #add(Object)
317       */
318      public boolean addAll(Collection<? extends E> c) {
319 <        boolean modified = false;
320 <        Iterator<? extends E> e = c.iterator();
321 <        while (e.hasNext()) {
322 <            if (add(e.next()))
323 <                modified = true;
324 <        }
325 <        return modified;
319 >        boolean modified = false;
320 >        Iterator<? extends E> it = c.iterator();
321 >        while (it.hasNext()) {
322 >            if (add(it.next()))
323 >                modified = true;
324 >        }
325 >        return modified;
326      }
327  
328      /**
# Line 302 | Line 347 | public abstract class AbstractCollection
347       * @see #contains(Object)
348       */
349      public boolean removeAll(Collection<?> c) {
350 <        boolean modified = false;
351 <        Iterator<?> e = iterator();
352 <        while (e.hasNext()) {
353 <            if (c.contains(e.next())) {
354 <                e.remove();
355 <                modified = true;
356 <            }
357 <        }
358 <        return modified;
350 >        boolean modified = false;
351 >        Iterator<?> it = iterator();
352 >        while (it.hasNext()) {
353 >            if (c.contains(it.next())) {
354 >                it.remove();
355 >                modified = true;
356 >            }
357 >        }
358 >        return modified;
359      }
360  
361      /**
# Line 335 | Line 380 | public abstract class AbstractCollection
380       * @see #contains(Object)
381       */
382      public boolean retainAll(Collection<?> c) {
383 <        boolean modified = false;
384 <        Iterator<E> e = iterator();
385 <        while (e.hasNext()) {
386 <            if (!c.contains(e.next())) {
387 <                e.remove();
388 <                modified = true;
389 <            }
390 <        }
391 <        return modified;
383 >        boolean modified = false;
384 >        Iterator<E> it = iterator();
385 >        while (it.hasNext()) {
386 >            if (!c.contains(it.next())) {
387 >                it.remove();
388 >                modified = true;
389 >            }
390 >        }
391 >        return modified;
392      }
393  
394      /**
# Line 362 | Line 407 | public abstract class AbstractCollection
407       * @throws UnsupportedOperationException {@inheritDoc}
408       */
409      public void clear() {
410 <        Iterator<E> e = iterator();
411 <        while (e.hasNext()) {
412 <            e.next();
413 <            e.remove();
414 <        }
410 >        Iterator<E> it = iterator();
411 >        while (it.hasNext()) {
412 >            it.next();
413 >            it.remove();
414 >        }
415      }
416  
417  
# Line 383 | Line 428 | public abstract class AbstractCollection
428       * @return a string representation of this collection
429       */
430      public String toString() {
431 <        Iterator<E> i = iterator();
432 <        if (! i.hasNext())
433 <            return "[]";
434 <
435 <        StringBuilder sb = new StringBuilder();
436 <        sb.append('[');
437 <        for (;;) {
438 <            E e = i.next();
439 <            sb.append(e == this ? "(this Collection)" : e);
440 <            if (! i.hasNext())
441 <                return sb.append(']').toString();
442 <            sb.append(", ");
443 <        }
431 >        Iterator<E> it = iterator();
432 >        if (! it.hasNext())
433 >            return "[]";
434 >
435 >        StringBuilder sb = new StringBuilder();
436 >        sb.append('[');
437 >        for (;;) {
438 >            E e = it.next();
439 >            sb.append(e == this ? "(this Collection)" : e);
440 >            if (! it.hasNext())
441 >                return sb.append(']').toString();
442 >            sb.append(", ");
443 >        }
444      }
445  
446   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines