src/share/classes/java/util/Collections.java

Print this page
rev 7932 : 8021591: Additional explicit null checks
Reviewed-by: psandoz, martin, alanb


3862                 for (Object o : c)
3863                     if (!contains(o)) // Invokes safe contains() above
3864                         return false;
3865                 return true;
3866             }
3867 
3868             public boolean remove(Object o) {
3869                 if (!(o instanceof Map.Entry))
3870                     return false;
3871                 return s.remove(new AbstractMap.SimpleImmutableEntry
3872                                 <>((Map.Entry<?,?>)o));
3873             }
3874 
3875             public boolean removeAll(Collection<?> c) {
3876                 return batchRemove(c, false);
3877             }
3878             public boolean retainAll(Collection<?> c) {
3879                 return batchRemove(c, true);
3880             }
3881             private boolean batchRemove(Collection<?> c, boolean complement) {

3882                 boolean modified = false;
3883                 Iterator<Map.Entry<K,V>> it = iterator();
3884                 while (it.hasNext()) {
3885                     if (c.contains(it.next()) != complement) {
3886                         it.remove();
3887                         modified = true;
3888                     }
3889                 }
3890                 return modified;
3891             }
3892 
3893             public boolean equals(Object o) {
3894                 if (o == this)
3895                     return true;
3896                 if (!(o instanceof Set))
3897                     return false;
3898                 Set<?> that = (Set<?>) o;
3899                 return that.size() == s.size()
3900                     && containsAll(that); // Invokes safe containsAll() above
3901             }




3862                 for (Object o : c)
3863                     if (!contains(o)) // Invokes safe contains() above
3864                         return false;
3865                 return true;
3866             }
3867 
3868             public boolean remove(Object o) {
3869                 if (!(o instanceof Map.Entry))
3870                     return false;
3871                 return s.remove(new AbstractMap.SimpleImmutableEntry
3872                                 <>((Map.Entry<?,?>)o));
3873             }
3874 
3875             public boolean removeAll(Collection<?> c) {
3876                 return batchRemove(c, false);
3877             }
3878             public boolean retainAll(Collection<?> c) {
3879                 return batchRemove(c, true);
3880             }
3881             private boolean batchRemove(Collection<?> c, boolean complement) {
3882                 Objects.requireNonNull(c);
3883                 boolean modified = false;
3884                 Iterator<Map.Entry<K,V>> it = iterator();
3885                 while (it.hasNext()) {
3886                     if (c.contains(it.next()) != complement) {
3887                         it.remove();
3888                         modified = true;
3889                     }
3890                 }
3891                 return modified;
3892             }
3893 
3894             public boolean equals(Object o) {
3895                 if (o == this)
3896                     return true;
3897                 if (!(o instanceof Set))
3898                     return false;
3899                 Set<?> that = (Set<?>) o;
3900                 return that.size() == s.size()
3901                     && containsAll(that); // Invokes safe containsAll() above
3902             }