--- old/src/java.base/share/classes/java/util/AbstractCollection.java 2015-02-04 10:44:09.062986054 +0100 +++ new/src/java.base/share/classes/java/util/AbstractCollection.java 2015-02-04 10:44:08.962987908 +0100 @@ -23,7 +23,11 @@ * questions. */ -package java.util; +package javany.util; + +import java.util.Objects; + +import javany.util.function.*; /** * This class provides a skeletal implementation of the Collection @@ -58,7 +62,7 @@ * @since 1.2 */ -public abstract class AbstractCollection implements Collection { +public abstract class AbstractCollection implements Collection { /** * Sole constructor. (For invocation by subclass constructors, typically * implicit.) @@ -98,17 +102,31 @@ * @throws NullPointerException {@inheritDoc} */ public boolean contains(Object o) { - 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; + __WhereVal(E) { + Iterator it = iterator(); + if (o == null) { + return false; + } else { + Function box = Any.converter(); + while (it.hasNext()) + if (o.equals(box.apply(it.next()))) + return true; + } + return false; + } + __WhereRef(E) { + 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; } - return false; } /** @@ -138,12 +156,13 @@ // Estimate size of array; be prepared to see more or fewer elements Object[] r = new Object[size()]; Iterator it = iterator(); + Function 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] = it.next(); + r[i] = box.apply(it.next()); // boxing } - return it.hasNext() ? finishToArray(r, it) : r; + return it.hasNext() ? finishToArray(r, it, box) : r; } /** @@ -175,32 +194,33 @@ * @throws NullPointerException {@inheritDoc} */ @SuppressWarnings("unchecked") - public T[] toArray(T[] a) { + public 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 it = iterator(); + Function converter = Any.converter(); for (int i = 0; i < r.length; i++) { if (! it.hasNext()) { // fewer elements than expected if (a == r) { - r[i] = null; // null-terminate + r[i] = Any.defaultValue(); // null/zero-terminate } else if (a.length < i) { return Arrays.copyOf(r, i); } else { - System.arraycopy(r, 0, a, 0, i); + Any.arraycopy(r, 0, a, 0, i); if (a.length > i) { - a[i] = null; + a[i] = Any.defaultValue(); } } return a; } - r[i] = (T)it.next(); + r[i] = converter.apply(it.next()); } // more elements than expected - return it.hasNext() ? finishToArray(r, it) : r; + return it.hasNext() ? finishToArray(r, it, converter) : r; } /** @@ -222,7 +242,7 @@ * further elements returned by the iterator, trimmed to size */ @SuppressWarnings("unchecked") - private static T[] finishToArray(T[] r, Iterator it) { + private static T[] finishToArray(T[] r, Iterator it, Function converter) { int i = r.length; while (it.hasNext()) { int cap = r.length; @@ -233,7 +253,7 @@ newCap = hugeCapacity(cap + 1); r = Arrays.copyOf(r, newCap); } - r[i++] = (T)it.next(); + r[i++] = converter.apply(it.next()); } // trim if overallocated return (i == r.length) ? r : Arrays.copyOf(r, i); @@ -285,26 +305,41 @@ * @throws NullPointerException {@inheritDoc} */ public boolean remove(Object o) { - Iterator it = iterator(); - if (o==null) { + __WhereVal(E) { + if (o == null) { + return false; + } + Iterator it = iterator(); + Function box = Any.converter(); while (it.hasNext()) { - if (it.next()==null) { + if (o.equals(box.apply(it.next()))) { it.remove(); return true; } } - } else { - while (it.hasNext()) { - if (o.equals(it.next())) { - it.remove(); - return true; + return false; + } + __WhereRef(E) { + 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; } - return false; } - // Bulk Operations /** @@ -321,8 +356,9 @@ * @see #contains(Object) */ public boolean containsAll(Collection c) { - for (Object e : c) - if (!contains(e)) + Iterator it =c.iterator(); + while (it.hasNext()) + if (!contains(it.next())) return false; return true; } @@ -348,9 +384,12 @@ */ public boolean addAll(Collection c) { boolean modified = false; - for (E e : c) - if (add(e)) + Iterator it = c.iterator(); + while (it.hasNext()) { + if (add(it.next())) { modified = true; + } + } return modified; } @@ -379,9 +418,10 @@ public boolean removeAll(Collection c) { Objects.requireNonNull(c); boolean modified = false; - Iterator it = iterator(); + Iterator it = iterator(); + Function box = Any.converter(); while (it.hasNext()) { - if (c.contains(it.next())) { + if (c.contains(box.apply(it.next()))) { // boxing it.remove(); modified = true; } @@ -415,8 +455,9 @@ Objects.requireNonNull(c); boolean modified = false; Iterator it = iterator(); + Function box = Any.converter(); while (it.hasNext()) { - if (!c.contains(it.next())) { + if (!c.contains(box.apply(it.next()))) { // boxing it.remove(); modified = true; } @@ -462,19 +503,34 @@ * @return a string representation of this collection */ public String toString() { - 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(' '); + __WhereVal(E) { + Iterator 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 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(' '); + } } } - }