package com.google.common.reflect;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ForwardingMapEntry;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;
@Beta
public final class MutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B>
implements TypeToInstanceMap<B> {
private final Map<TypeToken<? extends B>, B> backingMap = Maps.newHashMap();
@Override
public <T extends B> @Nullable T getInstance(Class<T> type) {
return trustedGet(TypeToken.of(type));
}
@Override
public <T extends B> @Nullable T getInstance(TypeToken<T> type) {
return trustedGet(type.rejectTypeVariables());
}
@Override
@CanIgnoreReturnValue
public <T extends B> @Nullable T putInstance(Class<T> type, @Nullable T value) {
return trustedPut(TypeToken.of(type), value);
}
@Override
@CanIgnoreReturnValue
public <T extends B> @Nullable T putInstance(TypeToken<T> type, @Nullable T value) {
return trustedPut(type.rejectTypeVariables(), value);
}
@CanIgnoreReturnValue
@Deprecated
@Override
public B put(TypeToken<? extends B> key, B value) {
throw new UnsupportedOperationException("Please use putInstance() instead.");
}
@Deprecated
@Override
public void putAll(Map<? extends TypeToken<? extends B>, ? extends B> map) {
throw new UnsupportedOperationException("Please use putInstance() instead.");
}
@Override
public Set<Entry<TypeToken<? extends B>, B>> entrySet() {
return UnmodifiableEntry.transformEntries(super.entrySet());
}
@Override
protected Map<TypeToken<? extends B>, B> delegate() {
return backingMap;
}
@SuppressWarnings("unchecked")
private <T extends B> @Nullable T trustedPut(TypeToken<T> type, @Nullable T value) {
return (T) backingMap.put(type, value);
}
@SuppressWarnings("unchecked")
private <T extends B> @Nullable T trustedGet(TypeToken<T> type) {
return (T) backingMap.get(type);
}
private static final class UnmodifiableEntry<K, V> extends ForwardingMapEntry<K, V> {
private final Entry<K, V> delegate;
static <K, V> Set<Entry<K, V>> transformEntries(final Set<Entry<K, V>> entries) {
return new ForwardingSet<Map.Entry<K, V>>() {
@Override
protected Set<Entry<K, V>> delegate() {
return entries;
}
@Override
public Iterator<Entry<K, V>> iterator() {
return UnmodifiableEntry.transformEntries(super.iterator());
}
@Override
public Object[] toArray() {
return standardToArray();
}
@Override
public <T> T[] toArray(T[] array) {
return standardToArray(array);
}
};
}
private static <K, V> Iterator<Entry<K, V>> transformEntries(Iterator<Entry<K, V>> entries) {
return Iterators.transform(
entries,
new Function<Entry<K, V>, Entry<K, V>>() {
@Override
public Entry<K, V> apply(Entry<K, V> entry) {
return new UnmodifiableEntry<>(entry);
}
});
}
private UnmodifiableEntry(java.util.Map.Entry<K, V> delegate) {
this.delegate = checkNotNull(delegate);
}
@Override
protected Entry<K, V> delegate() {
return delegate;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
}
}
}