< prev index next >

src/java.base/share/classes/java/util/ImmutableCollections.java

Print this page
rev 57961 : 8238684: Override getOrDefault in immutable Map implementation
Reviewed-by: smarks


 887             return array;
 888         }
 889     }
 890 
 891     // ---------- Map Implementations ----------
 892 
 893     abstract static class AbstractImmutableMap<K,V> extends AbstractMap<K,V> implements Serializable {
 894         @Override public void clear() { throw uoe(); }
 895         @Override public V compute(K key, BiFunction<? super K,? super V,? extends V> rf) { throw uoe(); }
 896         @Override public V computeIfAbsent(K key, Function<? super K,? extends V> mf) { throw uoe(); }
 897         @Override public V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> rf) { throw uoe(); }
 898         @Override public V merge(K key, V value, BiFunction<? super V,? super V,? extends V> rf) { throw uoe(); }
 899         @Override public V put(K key, V value) { throw uoe(); }
 900         @Override public void putAll(Map<? extends K,? extends V> m) { throw uoe(); }
 901         @Override public V putIfAbsent(K key, V value) { throw uoe(); }
 902         @Override public V remove(Object key) { throw uoe(); }
 903         @Override public boolean remove(Object key, Object value) { throw uoe(); }
 904         @Override public V replace(K key, V value) { throw uoe(); }
 905         @Override public boolean replace(K key, V oldValue, V newValue) { throw uoe(); }
 906         @Override public void replaceAll(BiFunction<? super K,? super V,? extends V> f) { throw uoe(); }














 907     }
 908 
 909     static final class Map1<K,V> extends AbstractImmutableMap<K,V> {
 910         @Stable
 911         private final K k0;
 912         @Stable
 913         private final V v0;
 914 
 915         Map1(K k0, V v0) {
 916             this.k0 = Objects.requireNonNull(k0);
 917             this.v0 = Objects.requireNonNull(v0);
 918         }
 919 
 920         @Override
 921         public Set<Map.Entry<K,V>> entrySet() {
 922             return Set.of(new KeyValueHolder<>(k0, v0));
 923         }
 924 
 925         @Override
 926         public V get(Object o) {




 887             return array;
 888         }
 889     }
 890 
 891     // ---------- Map Implementations ----------
 892 
 893     abstract static class AbstractImmutableMap<K,V> extends AbstractMap<K,V> implements Serializable {
 894         @Override public void clear() { throw uoe(); }
 895         @Override public V compute(K key, BiFunction<? super K,? super V,? extends V> rf) { throw uoe(); }
 896         @Override public V computeIfAbsent(K key, Function<? super K,? extends V> mf) { throw uoe(); }
 897         @Override public V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> rf) { throw uoe(); }
 898         @Override public V merge(K key, V value, BiFunction<? super V,? super V,? extends V> rf) { throw uoe(); }
 899         @Override public V put(K key, V value) { throw uoe(); }
 900         @Override public void putAll(Map<? extends K,? extends V> m) { throw uoe(); }
 901         @Override public V putIfAbsent(K key, V value) { throw uoe(); }
 902         @Override public V remove(Object key) { throw uoe(); }
 903         @Override public boolean remove(Object key, Object value) { throw uoe(); }
 904         @Override public V replace(K key, V value) { throw uoe(); }
 905         @Override public boolean replace(K key, V oldValue, V newValue) { throw uoe(); }
 906         @Override public void replaceAll(BiFunction<? super K,? super V,? extends V> f) { throw uoe(); }
 907 
 908         /**
 909          * @implNote {@code null} values are disallowed in these immutable maps,
 910          * so we can improve upon the default implementation since a
 911          * {@code null} return from {@code get(key)} always means the default
 912          * value should be returned.
 913          */
 914         @Override
 915         public V getOrDefault(Object key, V defaultValue) {
 916             V v;
 917             return ((v = get(key)) != null)
 918                     ? v
 919                     : defaultValue;
 920         }
 921     }
 922 
 923     static final class Map1<K,V> extends AbstractImmutableMap<K,V> {
 924         @Stable
 925         private final K k0;
 926         @Stable
 927         private final V v0;
 928 
 929         Map1(K k0, V v0) {
 930             this.k0 = Objects.requireNonNull(k0);
 931             this.v0 = Objects.requireNonNull(v0);
 932         }
 933 
 934         @Override
 935         public Set<Map.Entry<K,V>> entrySet() {
 936             return Set.of(new KeyValueHolder<>(k0, v0));
 937         }
 938 
 939         @Override
 940         public V get(Object o) {


< prev index next >