jdk/src/share/classes/java/util/Collections.java
changeset 17168 b7d3500f2516
parent 17166 c83a0fa44906
child 18156 edb590d448c5
--- 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<? super E> 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<? super E> action) {
             c.forEach(action);
@@ -1122,6 +1128,11 @@
         public boolean removeIf(Predicate<? super E> filter) {
             throw new UnsupportedOperationException();
         }
+        @Override
+        public Spliterator<E> spliterator() {
+            return (Spliterator<E>)c.spliterator();
+        }
+
     }
 
     /**
@@ -1285,6 +1296,11 @@
                 public void add(E e) {
                     throw new UnsupportedOperationException();
                 }
+
+                @Override
+                public void forEachRemaining(Consumer<? super E> action) {
+                    i.forEachRemaining(action);
+                }
             };
         }
 
@@ -1664,7 +1680,8 @@
      * through the returned collection.<p>
      *
      * 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}:
      * <pre>
      *  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<? super E> action) {
-            synchronized (mutex) {c.forEach(action);}
+        public void forEach(Consumer<? super E> consumer) {
+            synchronized (mutex) {c.forEach(consumer);}
         }
         @Override
         public boolean removeIf(Predicate<? super E> filter) {
             synchronized (mutex) {return c.removeIf(filter);}
         }
+        @Override
+        public Spliterator<E> 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<? super E> action) {
-            c.forEach(action);
-        }
+        public void forEach(Consumer<? super E> action) {c.forEach(action);}
         @Override
         public boolean removeIf(Predicate<? super E> filter) {
             return c.removeIf(filter);
         }
+        @Override
+        public Spliterator<E> spliterator() {return c.spliterator();}
     }
 
     /**
@@ -2796,6 +2818,11 @@
                     typeCheck(e);
                     i.add(e);
                 }
+
+                @Override
+                public void forEachRemaining(Consumer<? super E> 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<? super E> action) {
+            Objects.requireNonNull(action);
+        }
     }
 
     /**
@@ -3474,6 +3505,7 @@
             return a;
         }
 
+        // Override default methods in Collection
         @Override
         public void forEach(Consumer<? super E> action) {
             Objects.requireNonNull(action);
@@ -3483,6 +3515,8 @@
             Objects.requireNonNull(filter);
             return false;
         }
+        @Override
+        public Spliterator<E> 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<? super E> action) {
             Objects.requireNonNull(action);
         }
+
         @Override
         public boolean removeIf(Predicate<? super E> filter) {
             Objects.requireNonNull(filter);
             return false;
         }
+
+        @Override
+        public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); }
     }
 
     /**
@@ -3671,10 +3710,6 @@
         public int hashCode() { return 1; }
 
         @Override
-        public void forEach(Consumer<? super E> action) {
-            Objects.requireNonNull(action);
-        }
-        @Override
         public boolean removeIf(Predicate<? super E> filter) {
             Objects.requireNonNull(filter);
             return false;
@@ -3688,6 +3723,15 @@
             Objects.requireNonNull(c);
         }
 
+        // Override default methods in Collection
+        @Override
+        public void forEach(Consumer<? super E> action) {
+            Objects.requireNonNull(action);
+        }
+
+        @Override
+        public Spliterator<E> 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<? super E> action) {
+                Objects.requireNonNull(action);
+                if (hasNext) {
+                    action.accept(e);
+                    hasNext = false;
+                }
+            }
+        };
+    }
+
+    /**
+     * Creates a {@code Spliterator} with only the specified element
+     *
+     * @param <T> Type of elements
+     * @return A singleton {@code Spliterator}
+     */
+    static <T> Spliterator<T> singletonSpliterator(final T element) {
+        return new Spliterator<T>() {
+            long est = 1;
+
+            @Override
+            public Spliterator<T> trySplit() {
+                return null;
+            }
+
+            @Override
+            public boolean tryAdvance(Consumer<? super T> consumer) {
+                Objects.requireNonNull(consumer);
+                if (est > 0) {
+                    est--;
+                    consumer.accept(element);
+                    return true;
+                }
+                return false;
+            }
+
+            @Override
+            public void forEachRemaining(Consumer<? super T> 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<? super E> action) {
             action.accept(element);
         }
         @Override
+        public Spliterator<E> spliterator() {
+            return singletonSpliterator(element);
+        }
+        @Override
         public boolean removeIf(Predicate<? super E> filter) {
             throw new UnsupportedOperationException();
         }
@@ -3916,6 +4019,7 @@
             return element;
         }
 
+        // Override default methods for Collection
         @Override
         public void forEach(Consumer<? super E> action) {
             action.accept(element);
@@ -3931,6 +4035,10 @@
         @Override
         public void sort(Comparator<? super E> c) {
         }
+        @Override
+        public Spliterator<E> 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<? super E> action) {
             s.forEach(action);
@@ -4538,6 +4647,9 @@
             return s.removeIf(filter);
         }
 
+        @Override
+        public Spliterator<E> 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<? super E> action) {
-            q.forEach(action);
-        }
+        public void forEach(Consumer<? super E> action) {q.forEach(action);}
+        @Override
+        public Spliterator<E> spliterator() {return q.spliterator();}
         @Override
         public boolean removeIf(Predicate<? super E> filter) {
             return q.removeIf(filter);