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.11 by jsr166, Sun May 20 07:54:01 2007 UTC vs.
Revision 1.17 by jsr166, Thu Nov 11 06:19:54 2010 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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   * This code is free software; you can redistribute it and/or modify it
# Line 18 | Line 18
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 < * CA 95054 USA or visit www.sun.com if you need additional information or
23 < * have any questions.
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 54 | Line 54 | package java.util;
54   *
55   * @author  Josh Bloch
56   * @author  Neal Gafter
57 * @version %I%, %G%
57   * @see Collection
58   * @since 1.2
59   */
# Line 84 | 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 97 | 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      /**
# Line 134 | Line 133 | public abstract class AbstractCollection
133       */
134      public Object[] toArray() {
135          // Estimate size of array; be prepared to see more or fewer elements
136 <        Object[] r = new Object[size()];
136 >        Object[] r = new Object[size()];
137          Iterator<E> it = iterator();
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;
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      /**
# Line 179 | Line 178 | public abstract class AbstractCollection
178                    .newInstance(a.getClass().getComponentType(), size);
179          Iterator<E> it = iterator();
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;
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      /**
# Line 202 | Line 201 | public abstract class AbstractCollection
201       *         further elements returned by the iterator, trimmed to size
202       */
203      private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
204 <        int i = r.length;
204 >        int i = r.length;
205          while (it.hasNext()) {
206              int cap = r.length;
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");
213 <                    newCap = Integer.MAX_VALUE;
214 <                }
215 <                r = Arrays.copyOf(r, newCap);
216 <            }
217 <            r[i++] = (T)it.next();
210 >                    if (cap == Integer.MAX_VALUE)
211 >                        throw new OutOfMemoryError
212 >                            ("Required array size too large");
213 >                    newCap = Integer.MAX_VALUE;
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);
# Line 236 | 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 256 | 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 291 | 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;
298 <        return true;
293 >        for (Object e : c)
294 >            if (!contains(e))
295 >                return false;
296 >        return true;
297      }
298  
299      /**
# Line 317 | Line 315 | public abstract class AbstractCollection
315       * @see #add(Object)
316       */
317      public boolean addAll(Collection<? extends E> c) {
318 <        boolean modified = false;
319 <        Iterator<? extends E> e = c.iterator();
320 <        while (e.hasNext()) {
321 <            if (add(e.next()))
322 <                modified = true;
325 <        }
326 <        return modified;
318 >        boolean modified = false;
319 >        for (E e : c)
320 >            if (add(e))
321 >                modified = true;
322 >        return modified;
323      }
324  
325      /**
# Line 348 | Line 344 | public abstract class AbstractCollection
344       * @see #contains(Object)
345       */
346      public boolean removeAll(Collection<?> c) {
347 <        boolean modified = false;
348 <        Iterator<?> e = iterator();
349 <        while (e.hasNext()) {
350 <            if (c.contains(e.next())) {
351 <                e.remove();
352 <                modified = true;
353 <            }
354 <        }
355 <        return modified;
347 >        boolean modified = false;
348 >        Iterator<?> it = iterator();
349 >        while (it.hasNext()) {
350 >            if (c.contains(it.next())) {
351 >                it.remove();
352 >                modified = true;
353 >            }
354 >        }
355 >        return modified;
356      }
357  
358      /**
# Line 381 | Line 377 | public abstract class AbstractCollection
377       * @see #contains(Object)
378       */
379      public boolean retainAll(Collection<?> c) {
380 <        boolean modified = false;
381 <        Iterator<E> e = iterator();
382 <        while (e.hasNext()) {
383 <            if (!c.contains(e.next())) {
384 <                e.remove();
385 <                modified = true;
386 <            }
387 <        }
388 <        return modified;
380 >        boolean modified = false;
381 >        Iterator<E> it = iterator();
382 >        while (it.hasNext()) {
383 >            if (!c.contains(it.next())) {
384 >                it.remove();
385 >                modified = true;
386 >            }
387 >        }
388 >        return modified;
389      }
390  
391      /**
# Line 408 | Line 404 | public abstract class AbstractCollection
404       * @throws UnsupportedOperationException {@inheritDoc}
405       */
406      public void clear() {
407 <        Iterator<E> e = iterator();
408 <        while (e.hasNext()) {
409 <            e.next();
410 <            e.remove();
411 <        }
407 >        Iterator<E> it = iterator();
408 >        while (it.hasNext()) {
409 >            it.next();
410 >            it.remove();
411 >        }
412      }
413  
414  
# Line 429 | Line 425 | public abstract class AbstractCollection
425       * @return a string representation of this collection
426       */
427      public String toString() {
428 <        Iterator<E> i = iterator();
429 <        if (! i.hasNext())
430 <            return "[]";
431 <
432 <        StringBuilder sb = new StringBuilder();
433 <        sb.append('[');
434 <        for (;;) {
435 <            E e = i.next();
436 <            sb.append(e == this ? "(this Collection)" : e);
437 <            if (! i.hasNext())
438 <                return sb.append(']').toString();
439 <            sb.append(", ");
440 <        }
428 >        Iterator<E> it = iterator();
429 >        if (! it.hasNext())
430 >            return "[]";
431 >
432 >        StringBuilder sb = new StringBuilder();
433 >        sb.append('[');
434 >        for (;;) {
435 >            E e = it.next();
436 >            sb.append(e == this ? "(this Collection)" : e);
437 >            if (! it.hasNext())
438 >                return sb.append(']').toString();
439 >            sb.append(',').append(' ');
440 >        }
441      }
442  
443   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines