package com.google.common.collect;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.RegularImmutableMap.makeImmutable;
import com.google.common.annotations.GwtCompatible;
import java.util.Map;
import java.util.function.BiConsumer;
import org.checkerframework.checker.nullness.qual.Nullable;
@GwtCompatible(emulated = true)
final class JdkBackedImmutableMap<K, V> extends ImmutableMap<K, V> {
static <K, V> ImmutableMap<K, V> create(int n, Entry<K, V>[] entryArray) {
Map<K, V> delegateMap = Maps.newHashMapWithExpectedSize(n);
for (int i = 0; i < n; i++) {
entryArray[i] = makeImmutable(entryArray[i]);
V oldValue = delegateMap.putIfAbsent(entryArray[i].getKey(), entryArray[i].getValue());
if (oldValue != null) {
throw conflictException("key", entryArray[i], entryArray[i].getKey() + "=" + oldValue);
}
}
return new JdkBackedImmutableMap<>(delegateMap, ImmutableList.asImmutableList(entryArray, n));
}
private final transient Map<K, V> delegateMap;
private final transient ImmutableList<Entry<K, V>> entries;
JdkBackedImmutableMap(Map<K, V> delegateMap, ImmutableList<Entry<K, V>> entries) {
this.delegateMap = delegateMap;
this.entries = entries;
}
@Override
public int size() {
return entries.size();
}
@Override
public V get(@Nullable Object key) {
return delegateMap.get(key);
}
@Override
ImmutableSet<Entry<K, V>> createEntrySet() {
return new ImmutableMapEntrySet.RegularEntrySet<K, V>(this, entries);
}
@Override
public void forEach(BiConsumer<? super K, ? super V> action) {
checkNotNull(action);
entries.forEach(e -> action.accept(e.getKey(), e.getValue()));
}
@Override
ImmutableSet<K> createKeySet() {
return new ImmutableMapKeySet<K, V>(this);
}
@Override
ImmutableCollection<V> createValues() {
return new ImmutableMapValues<K, V>(this);
}
@Override
boolean isPartialView() {
return false;
}
}