diff -r 87067e3340d3 -r b7d3500f2516 jdk/src/share/classes/java/util/Collections.java --- a/jdk/src/share/classes/java/util/Collections.java Wed Apr 17 14:39:04 2013 -0400 +++ b/jdk/src/share/classes/java/util/Collections.java Wed Apr 17 11:34:31 2013 +0200 @@ -1088,6 +1088,11 @@ public void remove() { throw new UnsupportedOperationException(); } + @Override + public void forEachRemaining(Consumer action) { + // Use backing collection version + i.forEachRemaining(action); + } }; } @@ -1114,6 +1119,7 @@ throw new UnsupportedOperationException(); } + // Override default methods in Collection @Override public void forEach(Consumer action) { c.forEach(action); @@ -1122,6 +1128,11 @@ public boolean removeIf(Predicate filter) { throw new UnsupportedOperationException(); } + @Override + public Spliterator spliterator() { + return (Spliterator)c.spliterator(); + } + } /** @@ -1285,6 +1296,11 @@ public void add(E e) { throw new UnsupportedOperationException(); } + + @Override + public void forEachRemaining(Consumer action) { + i.forEachRemaining(action); + } }; } @@ -1664,7 +1680,8 @@ * through the returned collection.

* * It is imperative that the user manually synchronize on the returned - * collection when iterating over it: + * collection when traversing it via {@link Iterator} or + * {@link Spliterator}: *

      *  Collection c = Collections.synchronizedCollection(myCollection);
      *     ...
@@ -1761,18 +1778,22 @@
         public String toString() {
             synchronized (mutex) {return c.toString();}
         }
-        private void writeObject(ObjectOutputStream s) throws IOException {
-            synchronized (mutex) {s.defaultWriteObject();}
-        }
-
+        // Override default methods in Collection
         @Override
-        public void forEach(Consumer action) {
-            synchronized (mutex) {c.forEach(action);}
+        public void forEach(Consumer consumer) {
+            synchronized (mutex) {c.forEach(consumer);}
         }
         @Override
         public boolean removeIf(Predicate filter) {
             synchronized (mutex) {return c.removeIf(filter);}
         }
+        @Override
+        public Spliterator spliterator() {
+            return c.spliterator(); // Must be manually synched by user!
+        }
+        private void writeObject(ObjectOutputStream s) throws IOException {
+            synchronized (mutex) {s.defaultWriteObject();}
+        }
     }
 
     /**
@@ -2533,14 +2554,15 @@
             return c.addAll(checkedCopyOf(coll));
         }
 
+        // Override default methods in Collection
         @Override
-        public void forEach(Consumer action) {
-            c.forEach(action);
-        }
+        public void forEach(Consumer action) {c.forEach(action);}
         @Override
         public boolean removeIf(Predicate filter) {
             return c.removeIf(filter);
         }
+        @Override
+        public Spliterator spliterator() {return c.spliterator();}
     }
 
     /**
@@ -2796,6 +2818,11 @@
                     typeCheck(e);
                     i.add(e);
                 }
+
+                @Override
+                public void forEachRemaining(Consumer action) {
+                    i.forEachRemaining(action);
+                }
             };
         }
 
@@ -3334,6 +3361,10 @@
         public boolean hasNext() { return false; }
         public E next() { throw new NoSuchElementException(); }
         public void remove() { throw new IllegalStateException(); }
+        @Override
+        public void forEachRemaining(Consumer action) {
+            Objects.requireNonNull(action);
+        }
     }
 
     /**
@@ -3474,6 +3505,7 @@
             return a;
         }
 
+        // Override default methods in Collection
         @Override
         public void forEach(Consumer action) {
             Objects.requireNonNull(action);
@@ -3483,6 +3515,8 @@
             Objects.requireNonNull(filter);
             return false;
         }
+        @Override
+        public Spliterator spliterator() { return Spliterators.emptySpliterator(); }
 
         // Preserves singleton property
         private Object readResolve() {
@@ -3592,15 +3626,20 @@
             throw new NoSuchElementException();
         }
 
+        // Override default methods in Collection
         @Override
         public void forEach(Consumer action) {
             Objects.requireNonNull(action);
         }
+
         @Override
         public boolean removeIf(Predicate filter) {
             Objects.requireNonNull(filter);
             return false;
         }
+
+        @Override
+        public Spliterator spliterator() { return Spliterators.emptySpliterator(); }
     }
 
     /**
@@ -3671,10 +3710,6 @@
         public int hashCode() { return 1; }
 
         @Override
-        public void forEach(Consumer action) {
-            Objects.requireNonNull(action);
-        }
-        @Override
         public boolean removeIf(Predicate filter) {
             Objects.requireNonNull(filter);
             return false;
@@ -3688,6 +3723,15 @@
             Objects.requireNonNull(c);
         }
 
+        // Override default methods in Collection
+        @Override
+        public void forEach(Consumer action) {
+            Objects.requireNonNull(action);
+        }
+
+        @Override
+        public Spliterator spliterator() { return Spliterators.emptySpliterator(); }
+
         // Preserves singleton property
         private Object readResolve() {
             return EMPTY_LIST;
@@ -3843,6 +3887,60 @@
             public void remove() {
                 throw new UnsupportedOperationException();
             }
+            @Override
+            public void forEachRemaining(Consumer action) {
+                Objects.requireNonNull(action);
+                if (hasNext) {
+                    action.accept(e);
+                    hasNext = false;
+                }
+            }
+        };
+    }
+
+    /**
+     * Creates a {@code Spliterator} with only the specified element
+     *
+     * @param  Type of elements
+     * @return A singleton {@code Spliterator}
+     */
+    static  Spliterator singletonSpliterator(final T element) {
+        return new Spliterator() {
+            long est = 1;
+
+            @Override
+            public Spliterator trySplit() {
+                return null;
+            }
+
+            @Override
+            public boolean tryAdvance(Consumer consumer) {
+                Objects.requireNonNull(consumer);
+                if (est > 0) {
+                    est--;
+                    consumer.accept(element);
+                    return true;
+                }
+                return false;
+            }
+
+            @Override
+            public void forEachRemaining(Consumer consumer) {
+                tryAdvance(consumer);
+            }
+
+            @Override
+            public long estimateSize() {
+                return est;
+            }
+
+            @Override
+            public int characteristics() {
+                int value = (element != null) ? Spliterator.NONNULL : 0;
+
+                return value | Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE |
+                       Spliterator.DISTINCT | Spliterator.ORDERED;
+            }
         };
     }
 
@@ -3867,11 +3965,16 @@
 
         public boolean contains(Object o) {return eq(o, element);}
 
+        // Override default methods for Collection
         @Override
         public void forEach(Consumer action) {
             action.accept(element);
         }
         @Override
+        public Spliterator spliterator() {
+            return singletonSpliterator(element);
+        }
+        @Override
         public boolean removeIf(Predicate filter) {
             throw new UnsupportedOperationException();
         }
@@ -3916,6 +4019,7 @@
             return element;
         }
 
+        // Override default methods for Collection
         @Override
         public void forEach(Consumer action) {
             action.accept(element);
@@ -3931,6 +4035,10 @@
         @Override
         public void sort(Comparator c) {
         }
+        @Override
+        public Spliterator spliterator() {
+            return singletonSpliterator(element);
+        }
     }
 
     /**
@@ -4529,6 +4637,7 @@
         public boolean retainAll(Collection c)   {return s.retainAll(c);}
         // addAll is the only inherited implementation
 
+        // Override default methods in Collection
         @Override
         public void forEach(Consumer action) {
             s.forEach(action);
@@ -4538,6 +4647,9 @@
             return s.removeIf(filter);
         }
 
+        @Override
+        public Spliterator spliterator() {return s.spliterator();}
+
         private static final long serialVersionUID = 2454657854757543876L;
 
         private void readObject(java.io.ObjectInputStream stream)
@@ -4597,10 +4709,11 @@
         public boolean retainAll(Collection c)   {return q.retainAll(c);}
         // We use inherited addAll; forwarding addAll would be wrong
 
+        // Override default methods in Collection
         @Override
-        public void forEach(Consumer action) {
-            q.forEach(action);
-        }
+        public void forEach(Consumer action) {q.forEach(action);}
+        @Override
+        public Spliterator spliterator() {return q.spliterator();}
         @Override
         public boolean removeIf(Predicate filter) {
             return q.removeIf(filter);