--- jsr166/src/main/java/util/AbstractCollection.java 2006/06/25 19:07:16 1.9 +++ jsr166/src/main/java/util/AbstractCollection.java 2010/09/30 01:53:03 1.16 @@ -1,8 +1,26 @@ /* - * %W% %E% + * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * - * Copyright 2006 Sun Microsystems, Inc. All rights reserved. - * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. */ package java.util; @@ -36,7 +54,6 @@ package java.util; * * @author Josh Bloch * @author Neal Gafter - * @version %I%, %G% * @see Collection * @since 1.2 */ @@ -66,7 +83,7 @@ public abstract class AbstractCollection *

This implementation returns size() == 0. */ public boolean isEmpty() { - return size() == 0; + return size() == 0; } /** @@ -79,17 +96,17 @@ public abstract class AbstractCollection * @throws NullPointerException {@inheritDoc} */ public boolean contains(Object o) { - Iterator e = iterator(); - if (o==null) { - while (e.hasNext()) - if (e.next()==null) - return true; - } else { - while (e.hasNext()) - if (o.equals(e.next())) - return true; - } - return false; + Iterator it = iterator(); + if (o==null) { + while (it.hasNext()) + if (it.next()==null) + return true; + } else { + while (it.hasNext()) + if (o.equals(it.next())) + return true; + } + return false; } /** @@ -116,14 +133,14 @@ public abstract class AbstractCollection */ public Object[] toArray() { // Estimate size of array; be prepared to see more or fewer elements - Object[] r = new Object[size()]; + Object[] r = new Object[size()]; Iterator it = iterator(); - for (int i = 0; i < r.length; i++) { - if (! it.hasNext()) // fewer elements than expected - return Arrays.copyOf(r, i); - r[i] = it.next(); - } - return it.hasNext() ? finishToArray(r, it) : r; + for (int i = 0; i < r.length; i++) { + if (! it.hasNext()) // fewer elements than expected + return Arrays.copyOf(r, i); + r[i] = it.next(); + } + return it.hasNext() ? finishToArray(r, it) : r; } /** @@ -161,16 +178,16 @@ public abstract class AbstractCollection .newInstance(a.getClass().getComponentType(), size); Iterator it = iterator(); - for (int i = 0; i < r.length; i++) { - if (! it.hasNext()) { // fewer elements than expected - if (a != r) - return Arrays.copyOf(r, i); - r[i] = null; // null-terminate - return r; - } - r[i] = (T)it.next(); - } - return it.hasNext() ? finishToArray(r, it) : r; + for (int i = 0; i < r.length; i++) { + if (! it.hasNext()) { // fewer elements than expected + if (a != r) + return Arrays.copyOf(r, i); + r[i] = null; // null-terminate + return r; + } + r[i] = (T)it.next(); + } + return it.hasNext() ? finishToArray(r, it) : r; } /** @@ -184,20 +201,20 @@ public abstract class AbstractCollection * further elements returned by the iterator, trimmed to size */ private static T[] finishToArray(T[] r, Iterator it) { - int i = r.length; + int i = r.length; while (it.hasNext()) { int cap = r.length; if (i == cap) { int newCap = ((cap / 2) + 1) * 3; if (newCap <= cap) { // integer overflow - if (cap == Integer.MAX_VALUE) - throw new OutOfMemoryError - ("Required array size too large"); - newCap = Integer.MAX_VALUE; - } - r = Arrays.copyOf(r, newCap); - } - r[i++] = (T)it.next(); + if (cap == Integer.MAX_VALUE) + throw new OutOfMemoryError + ("Required array size too large"); + newCap = Integer.MAX_VALUE; + } + r = Arrays.copyOf(r, newCap); + } + r[i++] = (T)it.next(); } // trim if overallocated return (i == r.length) ? r : Arrays.copyOf(r, i); @@ -218,7 +235,7 @@ public abstract class AbstractCollection * @throws IllegalStateException {@inheritDoc} */ public boolean add(E e) { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException(); } /** @@ -238,23 +255,23 @@ public abstract class AbstractCollection * @throws NullPointerException {@inheritDoc} */ public boolean remove(Object o) { - Iterator e = iterator(); - if (o==null) { - while (e.hasNext()) { - if (e.next()==null) { - e.remove(); - return true; - } - } - } else { - while (e.hasNext()) { - if (o.equals(e.next())) { - e.remove(); - return true; - } - } - } - return false; + Iterator it = iterator(); + if (o==null) { + while (it.hasNext()) { + if (it.next()==null) { + it.remove(); + return true; + } + } + } else { + while (it.hasNext()) { + if (o.equals(it.next())) { + it.remove(); + return true; + } + } + } + return false; } @@ -273,11 +290,11 @@ public abstract class AbstractCollection * @see #contains(Object) */ public boolean containsAll(Collection c) { - Iterator e = c.iterator(); - while (e.hasNext()) - if (!contains(e.next())) - return false; - return true; + Iterator it = c.iterator(); + while (it.hasNext()) + if (!contains(it.next())) + return false; + return true; } /** @@ -299,13 +316,13 @@ public abstract class AbstractCollection * @see #add(Object) */ public boolean addAll(Collection c) { - boolean modified = false; - Iterator e = c.iterator(); - while (e.hasNext()) { - if (add(e.next())) - modified = true; - } - return modified; + boolean modified = false; + Iterator it = c.iterator(); + while (it.hasNext()) { + if (add(it.next())) + modified = true; + } + return modified; } /** @@ -330,15 +347,15 @@ public abstract class AbstractCollection * @see #contains(Object) */ public boolean removeAll(Collection c) { - boolean modified = false; - Iterator e = iterator(); - while (e.hasNext()) { - if (c.contains(e.next())) { - e.remove(); - modified = true; - } - } - return modified; + boolean modified = false; + Iterator it = iterator(); + while (it.hasNext()) { + if (c.contains(it.next())) { + it.remove(); + modified = true; + } + } + return modified; } /** @@ -363,15 +380,15 @@ public abstract class AbstractCollection * @see #contains(Object) */ public boolean retainAll(Collection c) { - boolean modified = false; - Iterator e = iterator(); - while (e.hasNext()) { - if (!c.contains(e.next())) { - e.remove(); - modified = true; - } - } - return modified; + boolean modified = false; + Iterator it = iterator(); + while (it.hasNext()) { + if (!c.contains(it.next())) { + it.remove(); + modified = true; + } + } + return modified; } /** @@ -390,11 +407,11 @@ public abstract class AbstractCollection * @throws UnsupportedOperationException {@inheritDoc} */ public void clear() { - Iterator e = iterator(); - while (e.hasNext()) { - e.next(); - e.remove(); - } + Iterator it = iterator(); + while (it.hasNext()) { + it.next(); + it.remove(); + } } @@ -411,19 +428,19 @@ public abstract class AbstractCollection * @return a string representation of this collection */ public String toString() { - Iterator i = iterator(); - if (! i.hasNext()) - return "[]"; - - StringBuilder sb = new StringBuilder(); - sb.append('['); - for (;;) { - E e = i.next(); - sb.append(e == this ? "(this Collection)" : e); - if (! i.hasNext()) - return sb.append(']').toString(); - sb.append(", "); - } + Iterator it = iterator(); + if (! it.hasNext()) + return "[]"; + + StringBuilder sb = new StringBuilder(); + sb.append('['); + for (;;) { + E e = it.next(); + sb.append(e == this ? "(this Collection)" : e); + if (! it.hasNext()) + return sb.append(']').toString(); + sb.append(',').append(' '); + } } }