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

Print this page
rev 9708 : 8035284: Remove redundant null initialization
Reviwed-by: duke


1449 
1450         public int size()                        {return m.size();}
1451         public boolean isEmpty()                 {return m.isEmpty();}
1452         public boolean containsKey(Object key)   {return m.containsKey(key);}
1453         public boolean containsValue(Object val) {return m.containsValue(val);}
1454         public V get(Object key)                 {return m.get(key);}
1455 
1456         public V put(K key, V value) {
1457             throw new UnsupportedOperationException();
1458         }
1459         public V remove(Object key) {
1460             throw new UnsupportedOperationException();
1461         }
1462         public void putAll(Map<? extends K, ? extends V> m) {
1463             throw new UnsupportedOperationException();
1464         }
1465         public void clear() {
1466             throw new UnsupportedOperationException();
1467         }
1468 
1469         private transient Set<K> keySet = null;
1470         private transient Set<Map.Entry<K,V>> entrySet = null;
1471         private transient Collection<V> values = null;
1472 
1473         public Set<K> keySet() {
1474             if (keySet==null)
1475                 keySet = unmodifiableSet(m.keySet());
1476             return keySet;
1477         }
1478 
1479         public Set<Map.Entry<K,V>> entrySet() {
1480             if (entrySet==null)
1481                 entrySet = new UnmodifiableEntrySet<>(m.entrySet());
1482             return entrySet;
1483         }
1484 
1485         public Collection<V> values() {
1486             if (values==null)
1487                 values = unmodifiableCollection(m.values());
1488             return values;
1489         }
1490 
1491         public boolean equals(Object o) {return o == this || m.equals(o);}


2580         public boolean containsValue(Object value) {
2581             synchronized (mutex) {return m.containsValue(value);}
2582         }
2583         public V get(Object key) {
2584             synchronized (mutex) {return m.get(key);}
2585         }
2586 
2587         public V put(K key, V value) {
2588             synchronized (mutex) {return m.put(key, value);}
2589         }
2590         public V remove(Object key) {
2591             synchronized (mutex) {return m.remove(key);}
2592         }
2593         public void putAll(Map<? extends K, ? extends V> map) {
2594             synchronized (mutex) {m.putAll(map);}
2595         }
2596         public void clear() {
2597             synchronized (mutex) {m.clear();}
2598         }
2599 
2600         private transient Set<K> keySet = null;
2601         private transient Set<Map.Entry<K,V>> entrySet = null;
2602         private transient Collection<V> values = null;
2603 
2604         public Set<K> keySet() {
2605             synchronized (mutex) {
2606                 if (keySet==null)
2607                     keySet = new SynchronizedSet<>(m.keySet(), mutex);
2608                 return keySet;
2609             }
2610         }
2611 
2612         public Set<Map.Entry<K,V>> entrySet() {
2613             synchronized (mutex) {
2614                 if (entrySet==null)
2615                     entrySet = new SynchronizedSet<>(m.entrySet(), mutex);
2616                 return entrySet;
2617             }
2618         }
2619 
2620         public Collection<V> values() {
2621             synchronized (mutex) {
2622                 if (values==null)


3065         }
3066         public boolean retainAll(Collection<?> coll) {
3067             return c.retainAll(coll);
3068         }
3069 
3070         public Iterator<E> iterator() {
3071             // JDK-6363904 - unwrapped iterator could be typecast to
3072             // ListIterator with unsafe set()
3073             final Iterator<E> it = c.iterator();
3074             return new Iterator<E>() {
3075                 public boolean hasNext() { return it.hasNext(); }
3076                 public E next()          { return it.next(); }
3077                 public void remove()     {        it.remove(); }};
3078         }
3079 
3080         public boolean add(E e) {
3081             typeCheck(e);
3082             return c.add(e);
3083         }
3084 
3085         private E[] zeroLengthElementArray = null; // Lazily initialized
3086 
3087         private E[] zeroLengthElementArray() {
3088             return zeroLengthElementArray != null ? zeroLengthElementArray :
3089                 (zeroLengthElementArray = zeroLengthArray(type));
3090         }
3091 
3092         @SuppressWarnings("unchecked")
3093         Collection<E> checkedCopyOf(Collection<? extends E> coll) {
3094             Object[] a = null;
3095             try {
3096                 E[] z = zeroLengthElementArray();
3097                 a = coll.toArray(z);
3098                 // Defend against coll violating the toArray contract
3099                 if (a.getClass() != z.getClass())
3100                     a = Arrays.copyOf(a, a.length, z.getClass());
3101             } catch (ArrayStoreException ignore) {
3102                 // To get better and consistent diagnostics,
3103                 // we call typeCheck explicitly on each element.
3104                 // We call clone() to defend against coll retaining a
3105                 // reference to the returned array and storing a bad


3626         public void putAll(Map<? extends K, ? extends V> t) {
3627             // Satisfy the following goals:
3628             // - good diagnostics in case of type mismatch
3629             // - all-or-nothing semantics
3630             // - protection from malicious t
3631             // - correct behavior if t is a concurrent map
3632             Object[] entries = t.entrySet().toArray();
3633             List<Map.Entry<K,V>> checked = new ArrayList<>(entries.length);
3634             for (Object o : entries) {
3635                 Map.Entry<?,?> e = (Map.Entry<?,?>) o;
3636                 Object k = e.getKey();
3637                 Object v = e.getValue();
3638                 typeCheck(k, v);
3639                 checked.add(
3640                         new AbstractMap.SimpleImmutableEntry<>((K)k, (V)v));
3641             }
3642             for (Map.Entry<K,V> e : checked)
3643                 m.put(e.getKey(), e.getValue());
3644         }
3645 
3646         private transient Set<Map.Entry<K,V>> entrySet = null;
3647 
3648         public Set<Map.Entry<K,V>> entrySet() {
3649             if (entrySet==null)
3650                 entrySet = new CheckedEntrySet<>(m.entrySet(), valueType);
3651             return entrySet;
3652         }
3653 
3654         // Override default methods in Map
3655         @Override
3656         public void forEach(BiConsumer<? super K, ? super V> action) {
3657             m.forEach(action);
3658         }
3659 
3660         @Override
3661         public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
3662             m.replaceAll(typeCheck(function));
3663         }
3664 
3665         @Override
3666         public V putIfAbsent(K key, V value) {


4860      */
4861     private static class SingletonMap<K,V>
4862           extends AbstractMap<K,V>
4863           implements Serializable {
4864         private static final long serialVersionUID = -6979724477215052911L;
4865 
4866         private final K k;
4867         private final V v;
4868 
4869         SingletonMap(K key, V value) {
4870             k = key;
4871             v = value;
4872         }
4873 
4874         public int size()                                           {return 1;}
4875         public boolean isEmpty()                                {return false;}
4876         public boolean containsKey(Object key)             {return eq(key, k);}
4877         public boolean containsValue(Object value)       {return eq(value, v);}
4878         public V get(Object key)              {return (eq(key, k) ? v : null);}
4879 
4880         private transient Set<K> keySet = null;
4881         private transient Set<Map.Entry<K,V>> entrySet = null;
4882         private transient Collection<V> values = null;
4883 
4884         public Set<K> keySet() {
4885             if (keySet==null)
4886                 keySet = singleton(k);
4887             return keySet;
4888         }
4889 
4890         public Set<Map.Entry<K,V>> entrySet() {
4891             if (entrySet==null)
4892                 entrySet = Collections.<Map.Entry<K,V>>singleton(
4893                     new SimpleImmutableEntry<>(k, v));
4894             return entrySet;
4895         }
4896 
4897         public Collection<V> values() {
4898             if (values==null)
4899                 values = singleton(v);
4900             return values;
4901         }
4902 




1449 
1450         public int size()                        {return m.size();}
1451         public boolean isEmpty()                 {return m.isEmpty();}
1452         public boolean containsKey(Object key)   {return m.containsKey(key);}
1453         public boolean containsValue(Object val) {return m.containsValue(val);}
1454         public V get(Object key)                 {return m.get(key);}
1455 
1456         public V put(K key, V value) {
1457             throw new UnsupportedOperationException();
1458         }
1459         public V remove(Object key) {
1460             throw new UnsupportedOperationException();
1461         }
1462         public void putAll(Map<? extends K, ? extends V> m) {
1463             throw new UnsupportedOperationException();
1464         }
1465         public void clear() {
1466             throw new UnsupportedOperationException();
1467         }
1468 
1469         private transient Set<K> keySet;
1470         private transient Set<Map.Entry<K,V>> entrySet;
1471         private transient Collection<V> values;
1472 
1473         public Set<K> keySet() {
1474             if (keySet==null)
1475                 keySet = unmodifiableSet(m.keySet());
1476             return keySet;
1477         }
1478 
1479         public Set<Map.Entry<K,V>> entrySet() {
1480             if (entrySet==null)
1481                 entrySet = new UnmodifiableEntrySet<>(m.entrySet());
1482             return entrySet;
1483         }
1484 
1485         public Collection<V> values() {
1486             if (values==null)
1487                 values = unmodifiableCollection(m.values());
1488             return values;
1489         }
1490 
1491         public boolean equals(Object o) {return o == this || m.equals(o);}


2580         public boolean containsValue(Object value) {
2581             synchronized (mutex) {return m.containsValue(value);}
2582         }
2583         public V get(Object key) {
2584             synchronized (mutex) {return m.get(key);}
2585         }
2586 
2587         public V put(K key, V value) {
2588             synchronized (mutex) {return m.put(key, value);}
2589         }
2590         public V remove(Object key) {
2591             synchronized (mutex) {return m.remove(key);}
2592         }
2593         public void putAll(Map<? extends K, ? extends V> map) {
2594             synchronized (mutex) {m.putAll(map);}
2595         }
2596         public void clear() {
2597             synchronized (mutex) {m.clear();}
2598         }
2599 
2600         private transient Set<K> keySet;
2601         private transient Set<Map.Entry<K,V>> entrySet;
2602         private transient Collection<V> values;
2603 
2604         public Set<K> keySet() {
2605             synchronized (mutex) {
2606                 if (keySet==null)
2607                     keySet = new SynchronizedSet<>(m.keySet(), mutex);
2608                 return keySet;
2609             }
2610         }
2611 
2612         public Set<Map.Entry<K,V>> entrySet() {
2613             synchronized (mutex) {
2614                 if (entrySet==null)
2615                     entrySet = new SynchronizedSet<>(m.entrySet(), mutex);
2616                 return entrySet;
2617             }
2618         }
2619 
2620         public Collection<V> values() {
2621             synchronized (mutex) {
2622                 if (values==null)


3065         }
3066         public boolean retainAll(Collection<?> coll) {
3067             return c.retainAll(coll);
3068         }
3069 
3070         public Iterator<E> iterator() {
3071             // JDK-6363904 - unwrapped iterator could be typecast to
3072             // ListIterator with unsafe set()
3073             final Iterator<E> it = c.iterator();
3074             return new Iterator<E>() {
3075                 public boolean hasNext() { return it.hasNext(); }
3076                 public E next()          { return it.next(); }
3077                 public void remove()     {        it.remove(); }};
3078         }
3079 
3080         public boolean add(E e) {
3081             typeCheck(e);
3082             return c.add(e);
3083         }
3084 
3085         private E[] zeroLengthElementArray; // Lazily initialized
3086 
3087         private E[] zeroLengthElementArray() {
3088             return zeroLengthElementArray != null ? zeroLengthElementArray :
3089                 (zeroLengthElementArray = zeroLengthArray(type));
3090         }
3091 
3092         @SuppressWarnings("unchecked")
3093         Collection<E> checkedCopyOf(Collection<? extends E> coll) {
3094             Object[] a = null;
3095             try {
3096                 E[] z = zeroLengthElementArray();
3097                 a = coll.toArray(z);
3098                 // Defend against coll violating the toArray contract
3099                 if (a.getClass() != z.getClass())
3100                     a = Arrays.copyOf(a, a.length, z.getClass());
3101             } catch (ArrayStoreException ignore) {
3102                 // To get better and consistent diagnostics,
3103                 // we call typeCheck explicitly on each element.
3104                 // We call clone() to defend against coll retaining a
3105                 // reference to the returned array and storing a bad


3626         public void putAll(Map<? extends K, ? extends V> t) {
3627             // Satisfy the following goals:
3628             // - good diagnostics in case of type mismatch
3629             // - all-or-nothing semantics
3630             // - protection from malicious t
3631             // - correct behavior if t is a concurrent map
3632             Object[] entries = t.entrySet().toArray();
3633             List<Map.Entry<K,V>> checked = new ArrayList<>(entries.length);
3634             for (Object o : entries) {
3635                 Map.Entry<?,?> e = (Map.Entry<?,?>) o;
3636                 Object k = e.getKey();
3637                 Object v = e.getValue();
3638                 typeCheck(k, v);
3639                 checked.add(
3640                         new AbstractMap.SimpleImmutableEntry<>((K)k, (V)v));
3641             }
3642             for (Map.Entry<K,V> e : checked)
3643                 m.put(e.getKey(), e.getValue());
3644         }
3645 
3646         private transient Set<Map.Entry<K,V>> entrySet;
3647 
3648         public Set<Map.Entry<K,V>> entrySet() {
3649             if (entrySet==null)
3650                 entrySet = new CheckedEntrySet<>(m.entrySet(), valueType);
3651             return entrySet;
3652         }
3653 
3654         // Override default methods in Map
3655         @Override
3656         public void forEach(BiConsumer<? super K, ? super V> action) {
3657             m.forEach(action);
3658         }
3659 
3660         @Override
3661         public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
3662             m.replaceAll(typeCheck(function));
3663         }
3664 
3665         @Override
3666         public V putIfAbsent(K key, V value) {


4860      */
4861     private static class SingletonMap<K,V>
4862           extends AbstractMap<K,V>
4863           implements Serializable {
4864         private static final long serialVersionUID = -6979724477215052911L;
4865 
4866         private final K k;
4867         private final V v;
4868 
4869         SingletonMap(K key, V value) {
4870             k = key;
4871             v = value;
4872         }
4873 
4874         public int size()                                           {return 1;}
4875         public boolean isEmpty()                                {return false;}
4876         public boolean containsKey(Object key)             {return eq(key, k);}
4877         public boolean containsValue(Object value)       {return eq(value, v);}
4878         public V get(Object key)              {return (eq(key, k) ? v : null);}
4879 
4880         private transient Set<K> keySet;
4881         private transient Set<Map.Entry<K,V>> entrySet;
4882         private transient Collection<V> values;
4883 
4884         public Set<K> keySet() {
4885             if (keySet==null)
4886                 keySet = singleton(k);
4887             return keySet;
4888         }
4889 
4890         public Set<Map.Entry<K,V>> entrySet() {
4891             if (entrySet==null)
4892                 entrySet = Collections.<Map.Entry<K,V>>singleton(
4893                     new SimpleImmutableEntry<>(k, v));
4894             return entrySet;
4895         }
4896 
4897         public Collection<V> values() {
4898             if (values==null)
4899                 values = singleton(v);
4900             return values;
4901         }
4902