changeset 7727:ec9ba77a90bc

Ensure NPE as specification requires
author henryjen
date Fri, 22 Mar 2013 16:01:01 -0700
parents 7ec12f4990c0
children 9592b7987cac
files src/share/classes/java/util/Collections.java src/share/classes/java/util/Spliterators.java
diffstat 2 files changed, 37 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Collections.java	Fri Mar 22 22:25:54 2013 +0100
+++ b/src/share/classes/java/util/Collections.java	Fri Mar 22 16:01:01 2013 -0700
@@ -2915,6 +2915,7 @@
         private BiFunction<? super K, ? super V, ? extends V> typeCheck(
                     BiFunction<? super K, ? super V, ? extends V> func)
         {
+            Objects.requireNonNull(func);
             return (k, v) -> {
                 V newValue = func.apply(k, v);
                 typeCheck(k, newValue);
@@ -3023,6 +3024,7 @@
 
         @Override
         public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
+            Objects.requireNonNull(mappingFunction);
             return m.computeIfAbsent(key, k -> {
                 V value = mappingFunction.apply(k);
                 typeCheck(k, value);
@@ -3042,6 +3044,7 @@
 
         @Override
         public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
+            Objects.requireNonNull(remappingFunction);
             return m.merge(key, value, (v1, v2) -> {
                 V newValue = remappingFunction.apply(v1, v2);
                 typeCheck(null, newValue);
@@ -3349,7 +3352,9 @@
         public E next() { throw new NoSuchElementException(); }
         public void remove() { throw new IllegalStateException(); }
         @Override
-        public void forEach(Consumer<? super E> action) {}
+        public void forEach(Consumer<? super E> action) {
+            Objects.requireNonNull(action);
+        }
     }
 
     /**
@@ -3492,7 +3497,9 @@
 
         // Override default methods in Collection
         @Override
-        public void forEach(Consumer<? super E> action) {}
+        public void forEach(Consumer<? super E> action) {
+            Objects.requireNonNull(action);
+        }
         @Override
         public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); }
 
@@ -3606,7 +3613,9 @@
 
         // Override default methods in Collection
         @Override
-        public void forEach(Consumer<? super E> action) {}
+        public void forEach(Consumer<? super E> action) {
+            Objects.requireNonNull(action);
+        }
         @Override
         public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); }
     }
@@ -3680,7 +3689,9 @@
 
         // Override default methods in Collection
         @Override
-        public void forEach(Consumer<? super E> action) {}
+        public void forEach(Consumer<? super E> action) {
+            Objects.requireNonNull(action);
+        }
         @Override
         public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); }
 
@@ -3751,7 +3762,9 @@
         }
 
         @Override
-        public void forEach(BiConsumer<? super K, ? super V> action) {}
+        public void forEach(BiConsumer<? super K, ? super V> action) {
+            Objects.requireNonNull(action);
+        }
 
         @Override
         public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
@@ -3838,6 +3851,7 @@
             }
             @Override
             public void forEach(Consumer<? super E> action) {
+                Objects.requireNonNull(action);
                 if (hasNext) {
                     action.accept(e);
                     hasNext = false;
--- a/src/share/classes/java/util/Spliterators.java	Fri Mar 22 22:25:54 2013 +0100
+++ b/src/share/classes/java/util/Spliterators.java	Fri Mar 22 16:01:01 2013 -0700
@@ -574,11 +574,14 @@
 
         @Override
         public boolean tryAdvance(Consumer<? super T> consumer) {
+            Objects.requireNonNull(consumer);
             return false;
         }
 
         @Override
-        public void forEach(Consumer<? super T> consumer) { }
+        public void forEach(Consumer<? super T> consumer) {
+            Objects.requireNonNull(consumer);
+        }
 
         @Override
         public long estimateSize() {
@@ -600,11 +603,13 @@
 
             @Override
             public boolean tryAdvance(IntConsumer consumer) {
+                Objects.requireNonNull(consumer);
                 return false;
             }
 
             @Override
             public void forEach(IntConsumer consumer) {
+                Objects.requireNonNull(consumer);
             }
         }
 
@@ -618,11 +623,13 @@
 
             @Override
             public boolean tryAdvance(LongConsumer consumer) {
+                Objects.requireNonNull(consumer);
                 return false;
             }
 
             @Override
             public void forEach(LongConsumer consumer) {
+                Objects.requireNonNull(consumer);
             }
         }
 
@@ -636,11 +643,13 @@
 
             @Override
             public boolean tryAdvance(DoubleConsumer consumer) {
+                Objects.requireNonNull(consumer);
                 return false;
             }
 
             @Override
             public void forEach(DoubleConsumer consumer) {
+                Objects.requireNonNull(consumer);
             }
         }
     }
@@ -658,6 +667,7 @@
 
         @Override
         public boolean tryAdvance(Consumer<? super T> consumer) {
+            Objects.requireNonNull(consumer);
             if (!consumed) {
                 consumed = true;
                 consumer.accept(element);
@@ -678,7 +688,10 @@
 
         @Override
         public int characteristics() {
-            return Spliterator.SIZED | Spliterator.SUBSIZED;
+            int value = (element != null) ? Spliterator.NONNULL : 0;
+
+            return value | Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE |
+                Spliterator.DISTINCT | Spliterator.ORDERED;
         }
 
         private static final class OfInt extends SingletonSpliterator<Integer> implements Spliterator.OfInt {
@@ -693,6 +706,7 @@
 
             @Override
             public boolean tryAdvance(IntConsumer consumer) {
+                Objects.requireNonNull(consumer);
                 if (!consumed) {
                     consumed = true;
                     consumer.accept(value);
@@ -719,6 +733,7 @@
 
             @Override
             public boolean tryAdvance(LongConsumer consumer) {
+                Objects.requireNonNull(consumer);
                 if (!consumed) {
                     consumed = true;
                     consumer.accept(value);
@@ -745,6 +760,7 @@
 
             @Override
             public boolean tryAdvance(DoubleConsumer consumer) {
+                Objects.requireNonNull(consumer);
                 if (!consumed) {
                     consumed = true;
                     consumer.accept(value);