< prev index next >

src/java.base/share/classes/java/util/AbstractCollection.java

Print this page

        

*** 21,31 **** * 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; /** * This class provides a skeletal implementation of the <tt>Collection</tt> * interface, to minimize the effort required to implement this interface. <p> * --- 21,35 ---- * 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 javany.util; ! ! import java.util.Objects; ! ! import javany.util.function.*; /** * This class provides a skeletal implementation of the <tt>Collection</tt> * interface, to minimize the effort required to implement this interface. <p> *
*** 56,66 **** * @author Neal Gafter * @see Collection * @since 1.2 */ ! public abstract class AbstractCollection<E> implements Collection<E> { /** * Sole constructor. (For invocation by subclass constructors, typically * implicit.) */ protected AbstractCollection() { --- 60,70 ---- * @author Neal Gafter * @see Collection * @since 1.2 */ ! public abstract class AbstractCollection<any E> implements Collection<E> { /** * Sole constructor. (For invocation by subclass constructors, typically * implicit.) */ protected AbstractCollection() {
*** 96,117 **** * * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public boolean contains(Object o) { Iterator<E> 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; } /** * {@inheritDoc} * * @implSpec --- 100,135 ---- * * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public boolean contains(Object o) { + __WhereVal(E) { Iterator<E> it = iterator(); ! if (o == null) { ! return false; ! } else { ! Function<E, Object> box = Any.converter(); ! while (it.hasNext()) ! if (o.equals(box.apply(it.next()))) ! return true; ! } ! return false; ! } ! __WhereRef(E) { ! Iterator<E> 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; } + } /** * {@inheritDoc} * * @implSpec
*** 136,151 **** */ public Object[] toArray() { // Estimate size of array; be prepared to see more or fewer elements Object[] r = new Object[size()]; Iterator<E> 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; } /** * {@inheritDoc} * --- 154,170 ---- */ public Object[] toArray() { // Estimate size of array; be prepared to see more or fewer elements Object[] r = new Object[size()]; Iterator<E> it = iterator(); + Function<E, Object> box = Any.converter(); for (int i = 0; i < r.length; i++) { if (! it.hasNext()) // fewer elements than expected return Arrays.copyOf(r, i); ! r[i] = box.apply(it.next()); // boxing } ! return it.hasNext() ? finishToArray(r, it, box) : r; } /** * {@inheritDoc} *
*** 173,208 **** * * @throws ArrayStoreException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ @SuppressWarnings("unchecked") ! public <T> T[] toArray(T[] a) { // Estimate size of array; be prepared to see more or fewer elements int size = size(); T[] r = a.length >= size ? a : (T[])java.lang.reflect.Array .newInstance(a.getClass().getComponentType(), size); Iterator<E> it = iterator(); for (int i = 0; i < r.length; i++) { if (! it.hasNext()) { // fewer elements than expected if (a == r) { ! r[i] = null; // null-terminate } else if (a.length < i) { return Arrays.copyOf(r, i); } else { ! System.arraycopy(r, 0, a, 0, i); if (a.length > i) { ! a[i] = null; } } return a; } ! r[i] = (T)it.next(); } // more elements than expected ! return it.hasNext() ? finishToArray(r, it) : r; } /** * The maximum size of array to allocate. * Some VMs reserve some header words in an array. --- 192,228 ---- * * @throws ArrayStoreException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ @SuppressWarnings("unchecked") ! public <any T> T[] toArray(T[] a) { // Estimate size of array; be prepared to see more or fewer elements int size = size(); T[] r = a.length >= size ? a : (T[])java.lang.reflect.Array .newInstance(a.getClass().getComponentType(), size); Iterator<E> it = iterator(); + Function<E, T> converter = Any.converter(); for (int i = 0; i < r.length; i++) { if (! it.hasNext()) { // fewer elements than expected if (a == r) { ! r[i] = Any.defaultValue(); // null/zero-terminate } else if (a.length < i) { return Arrays.copyOf(r, i); } else { ! Any.arraycopy(r, 0, a, 0, i); if (a.length > i) { ! a[i] = Any.defaultValue(); } } return a; } ! r[i] = converter.apply(it.next()); } // more elements than expected ! return it.hasNext() ? finishToArray(r, it, converter) : r; } /** * The maximum size of array to allocate. * Some VMs reserve some header words in an array.
*** 220,241 **** * @param it the in-progress iterator over this collection * @return array containing the elements in the given array, plus any * further elements returned by the iterator, trimmed to size */ @SuppressWarnings("unchecked") ! private static <T> T[] finishToArray(T[] r, Iterator<?> it) { int i = r.length; while (it.hasNext()) { int cap = r.length; if (i == cap) { int newCap = cap + (cap >> 1) + 1; // overflow-conscious code if (newCap - MAX_ARRAY_SIZE > 0) newCap = hugeCapacity(cap + 1); r = Arrays.copyOf(r, newCap); } ! r[i++] = (T)it.next(); } // trim if overallocated return (i == r.length) ? r : Arrays.copyOf(r, i); } --- 240,261 ---- * @param it the in-progress iterator over this collection * @return array containing the elements in the given array, plus any * further elements returned by the iterator, trimmed to size */ @SuppressWarnings("unchecked") ! private static <any E, any T> T[] finishToArray(T[] r, Iterator<E> it, Function<E, T> converter) { int i = r.length; while (it.hasNext()) { int cap = r.length; if (i == cap) { int newCap = cap + (cap >> 1) + 1; // overflow-conscious code if (newCap - MAX_ARRAY_SIZE > 0) newCap = hugeCapacity(cap + 1); r = Arrays.copyOf(r, newCap); } ! r[i++] = converter.apply(it.next()); } // trim if overallocated return (i == r.length) ? r : Arrays.copyOf(r, i); }
*** 283,292 **** --- 303,327 ---- * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public boolean remove(Object o) { + __WhereVal(E) { + if (o == null) { + return false; + } + Iterator<E> it = iterator(); + Function<E, Object> box = Any.converter(); + while (it.hasNext()) { + if (o.equals(box.apply(it.next()))) { + it.remove(); + return true; + } + } + return false; + } + __WhereRef(E) { Iterator<E> it = iterator(); if (o==null) { while (it.hasNext()) { if (it.next()==null) { it.remove();
*** 301,311 **** } } } return false; } ! // Bulk Operations /** * {@inheritDoc} --- 336,346 ---- } } } return false; } ! } // Bulk Operations /** * {@inheritDoc}
*** 319,330 **** * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @see #contains(Object) */ public boolean containsAll(Collection<?> c) { ! for (Object e : c) ! if (!contains(e)) return false; return true; } /** --- 354,366 ---- * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @see #contains(Object) */ public boolean containsAll(Collection<?> c) { ! Iterator<?> it =c.iterator(); ! while (it.hasNext()) ! if (!contains(it.next())) return false; return true; } /**
*** 346,358 **** * * @see #add(Object) */ public boolean addAll(Collection<? extends E> c) { boolean modified = false; ! for (E e : c) ! if (add(e)) modified = true; return modified; } /** * {@inheritDoc} --- 382,397 ---- * * @see #add(Object) */ public boolean addAll(Collection<? extends E> c) { boolean modified = false; ! Iterator<? extends E> it = c.iterator(); ! while (it.hasNext()) { ! if (add(it.next())) { modified = true; + } + } return modified; } /** * {@inheritDoc}
*** 377,389 **** * @see #contains(Object) */ public boolean removeAll(Collection<?> c) { Objects.requireNonNull(c); boolean modified = false; ! Iterator<?> it = iterator(); while (it.hasNext()) { ! if (c.contains(it.next())) { it.remove(); modified = true; } } return modified; --- 416,429 ---- * @see #contains(Object) */ public boolean removeAll(Collection<?> c) { Objects.requireNonNull(c); boolean modified = false; ! Iterator<E> it = iterator(); ! Function<E, Object> box = Any.converter(); while (it.hasNext()) { ! if (c.contains(box.apply(it.next()))) { // boxing it.remove(); modified = true; } } return modified;
*** 413,424 **** */ public boolean retainAll(Collection<?> c) { Objects.requireNonNull(c); boolean modified = false; Iterator<E> it = iterator(); while (it.hasNext()) { ! if (!c.contains(it.next())) { it.remove(); modified = true; } } return modified; --- 453,465 ---- */ public boolean retainAll(Collection<?> c) { Objects.requireNonNull(c); boolean modified = false; Iterator<E> it = iterator(); + Function<E, Object> box = Any.converter(); while (it.hasNext()) { ! if (!c.contains(box.apply(it.next()))) { // boxing it.remove(); modified = true; } } return modified;
*** 460,480 **** * by {@link String#valueOf(Object)}. * * @return a string representation of this collection */ public String toString() { Iterator<E> 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(' '); } } ! } --- 501,536 ---- * by {@link String#valueOf(Object)}. * * @return a string representation of this collection */ public String toString() { + __WhereVal(E) { + Iterator<E> it = iterator(); + if (!it.hasNext()) + return "[]"; + + StringBuilder sb = new StringBuilder(); + sb.append('['); + for (; ; ) { + sb.append(Any.toString(it.next())); + if (!it.hasNext()) + return sb.append(']').toString(); + sb.append(',').append(' '); + } + } + __WhereRef(E) { Iterator<E> 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(' '); } } ! } }
< prev index next >