Coverage Summary for Class: JdkBackedImmutableMap (com.google.common.collect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| JdkBackedImmutableMap | 0% (0/1) | 0% (0/10) | 0% (0/18) |
1 /* 2 * Copyright (C) 2018 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 import static com.google.common.collect.RegularImmutableMap.makeImmutable; 21 22 import com.google.common.annotations.GwtCompatible; 23 import java.util.Map; 24 import java.util.function.BiConsumer; 25 import org.checkerframework.checker.nullness.qual.Nullable; 26 27 /** 28 * Implementation of ImmutableMap backed by a JDK HashMap, which has smartness protecting against 29 * hash flooding. 30 */ 31 @GwtCompatible(emulated = true) 32 final class JdkBackedImmutableMap<K, V> extends ImmutableMap<K, V> { 33 /** 34 * Creates an {@code ImmutableMap} backed by a JDK HashMap. Used when probable hash flooding is 35 * detected. This implementation may replace the entries in entryArray with its own entry objects 36 * (though they will have the same key/value contents), and will take ownership of entryArray. 37 */ 38 static <K, V> ImmutableMap<K, V> create(int n, Entry<K, V>[] entryArray) { 39 Map<K, V> delegateMap = Maps.newHashMapWithExpectedSize(n); 40 for (int i = 0; i < n; i++) { 41 entryArray[i] = makeImmutable(entryArray[i]); 42 V oldValue = delegateMap.putIfAbsent(entryArray[i].getKey(), entryArray[i].getValue()); 43 if (oldValue != null) { 44 throw conflictException("key", entryArray[i], entryArray[i].getKey() + "=" + oldValue); 45 } 46 } 47 return new JdkBackedImmutableMap<>(delegateMap, ImmutableList.asImmutableList(entryArray, n)); 48 } 49 50 private final transient Map<K, V> delegateMap; 51 private final transient ImmutableList<Entry<K, V>> entries; 52 53 JdkBackedImmutableMap(Map<K, V> delegateMap, ImmutableList<Entry<K, V>> entries) { 54 this.delegateMap = delegateMap; 55 this.entries = entries; 56 } 57 58 @Override 59 public int size() { 60 return entries.size(); 61 } 62 63 @Override 64 public V get(@Nullable Object key) { 65 return delegateMap.get(key); 66 } 67 68 @Override 69 ImmutableSet<Entry<K, V>> createEntrySet() { 70 return new ImmutableMapEntrySet.RegularEntrySet<K, V>(this, entries); 71 } 72 73 @Override 74 public void forEach(BiConsumer<? super K, ? super V> action) { 75 checkNotNull(action); 76 entries.forEach(e -> action.accept(e.getKey(), e.getValue())); 77 } 78 79 @Override 80 ImmutableSet<K> createKeySet() { 81 return new ImmutableMapKeySet<K, V>(this); 82 } 83 84 @Override 85 ImmutableCollection<V> createValues() { 86 return new ImmutableMapValues<K, V>(this); 87 } 88 89 @Override 90 boolean isPartialView() { 91 return false; 92 } 93 }