< prev index next >

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

Print this page

        

@@ -21,11 +21,15 @@
  * 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;
+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,11 +60,11 @@
  * @author  Neal Gafter
  * @see Collection
  * @since 1.2
  */
 
-public abstract class AbstractCollection<E> implements Collection<E> {
+public abstract class AbstractCollection<any E> implements Collection<E> {
     /**
      * Sole constructor.  (For invocation by subclass constructors, typically
      * implicit.)
      */
     protected AbstractCollection() {

@@ -96,22 +100,36 @@
      *
      * @throws ClassCastException   {@inheritDoc}
      * @throws NullPointerException {@inheritDoc}
      */
     public boolean contains(Object o) {
+        __WhereVal(E) {
         Iterator<E> it = iterator();
-        if (o==null) {
+            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)
+                    if (it.next() == null)
                     return true;
         } else {
             while (it.hasNext())
                 if (o.equals(it.next()))
                     return true;
         }
         return false;
     }
+    }
 
     /**
      * {@inheritDoc}
      *
      * @implSpec

@@ -136,16 +154,17 @@
      */
     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] = it.next();
+            r[i] = box.apply(it.next()); // boxing
         }
-        return it.hasNext() ? finishToArray(r, it) : r;
+        return it.hasNext() ? finishToArray(r, it, box) : r;
     }
 
     /**
      * {@inheritDoc}
      *

@@ -173,36 +192,37 @@
      *
      * @throws ArrayStoreException  {@inheritDoc}
      * @throws NullPointerException {@inheritDoc}
      */
     @SuppressWarnings("unchecked")
-    public <T> T[] toArray(T[] a) {
+    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] = 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;
     }
 
     /**
      * The maximum size of array to allocate.
      * Some VMs reserve some header words in an array.

@@ -220,22 +240,22 @@
      * @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) {
+    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++] = (T)it.next();
+            r[i++] = converter.apply(it.next());
         }
         // trim if overallocated
         return (i == r.length) ? r : Arrays.copyOf(r, i);
     }
 

@@ -283,10 +303,25 @@
      * @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,11 +336,11 @@
                 }
             }
         }
         return false;
     }
-
+    }
 
     // Bulk Operations
 
     /**
      * {@inheritDoc}

@@ -319,12 +354,13 @@
      * @throws ClassCastException            {@inheritDoc}
      * @throws NullPointerException          {@inheritDoc}
      * @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;
     }
 
     /**

@@ -346,13 +382,16 @@
      *
      * @see #add(Object)
      */
     public boolean addAll(Collection<? extends E> c) {
         boolean modified = false;
-        for (E e : c)
-            if (add(e))
+        Iterator<? extends E> it = c.iterator();
+        while (it.hasNext()) {
+            if (add(it.next())) {
                 modified = true;
+            }
+        }
         return modified;
     }
 
     /**
      * {@inheritDoc}

@@ -377,13 +416,14 @@
      * @see #contains(Object)
      */
     public boolean removeAll(Collection<?> c) {
         Objects.requireNonNull(c);
         boolean modified = false;
-        Iterator<?> it = iterator();
+        Iterator<E> it = iterator();
+        Function<E, Object> box = Any.converter();
         while (it.hasNext()) {
-            if (c.contains(it.next())) {
+            if (c.contains(box.apply(it.next()))) { // boxing
                 it.remove();
                 modified = true;
             }
         }
         return modified;

@@ -413,12 +453,13 @@
      */
     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(it.next())) {
+            if (!c.contains(box.apply(it.next()))) { // boxing
                 it.remove();
                 modified = true;
             }
         }
         return modified;

@@ -460,21 +501,36 @@
      * 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())
+            if (!it.hasNext())
             return "[]";
 
         StringBuilder sb = new StringBuilder();
         sb.append('[');
-        for (;;) {
+            for (; ; ) {
             E e = it.next();
             sb.append(e == this ? "(this Collection)" : e);
-            if (! it.hasNext())
+                if (!it.hasNext())
                 return sb.append(']').toString();
             sb.append(',').append(' ');
         }
     }
-
+    }
 }
< prev index next >