001/*
002 * Copyright (C) 2012 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.reflect;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import com.google.common.collect.ForwardingMap;
020import com.google.common.collect.ForwardingMapEntry;
021import com.google.common.collect.ForwardingSet;
022import com.google.common.collect.Iterators;
023import com.google.common.collect.Maps;
024import com.google.errorprone.annotations.CanIgnoreReturnValue;
025import com.google.errorprone.annotations.DoNotCall;
026import java.util.Iterator;
027import java.util.Map;
028import java.util.Set;
029import javax.annotation.CheckForNull;
030import org.checkerframework.checker.nullness.qual.NonNull;
031import org.checkerframework.checker.nullness.qual.Nullable;
032
033/**
034 * A mutable type-to-instance map. See also {@link ImmutableTypeToInstanceMap}.
035 *
036 * @author Ben Yu
037 * @since 13.0
038 */
039@ElementTypesAreNonnullByDefault
040public final class MutableTypeToInstanceMap<B extends @Nullable Object>
041    extends ForwardingMap<TypeToken<? extends @NonNull B>, B> implements TypeToInstanceMap<B> {
042
043  private final Map<TypeToken<? extends @NonNull B>, B> backingMap = Maps.newHashMap();
044
045  @Override
046  @CheckForNull
047  public <T extends @NonNull B> T getInstance(Class<T> type) {
048    return trustedGet(TypeToken.of(type));
049  }
050
051  @Override
052  @CheckForNull
053  public <T extends @NonNull B> T getInstance(TypeToken<T> type) {
054    return trustedGet(type.rejectTypeVariables());
055  }
056
057  @Override
058  @CanIgnoreReturnValue
059  @CheckForNull
060  public <T extends B> T putInstance(Class<@NonNull T> type, @ParametricNullness T value) {
061    return trustedPut(TypeToken.of(type), value);
062  }
063
064  @Override
065  @CanIgnoreReturnValue
066  @CheckForNull
067  public <T extends B> T putInstance(TypeToken<@NonNull T> type, @ParametricNullness T value) {
068    return this.<T>trustedPut(type.rejectTypeVariables(), value);
069  }
070
071  /**
072   * Not supported. Use {@link #putInstance} instead.
073   *
074   * @deprecated unsupported operation
075   * @throws UnsupportedOperationException always
076   */
077  @CanIgnoreReturnValue
078  @Deprecated
079  @Override
080  @DoNotCall("Always throws UnsupportedOperationException")
081  @CheckForNull
082  public B put(TypeToken<? extends @NonNull B> key, @ParametricNullness B value) {
083    throw new UnsupportedOperationException("Please use putInstance() instead.");
084  }
085
086  /**
087   * Not supported. Use {@link #putInstance} instead.
088   *
089   * @deprecated unsupported operation
090   * @throws UnsupportedOperationException always
091   */
092  @Deprecated
093  @Override
094  @DoNotCall("Always throws UnsupportedOperationException")
095  public void putAll(Map<? extends TypeToken<? extends @NonNull B>, ? extends B> map) {
096    throw new UnsupportedOperationException("Please use putInstance() instead.");
097  }
098
099  @Override
100  public Set<Entry<TypeToken<? extends @NonNull B>, B>> entrySet() {
101    return UnmodifiableEntry.transformEntries(super.entrySet());
102  }
103
104  @Override
105  protected Map<TypeToken<? extends @NonNull B>, B> delegate() {
106    return backingMap;
107  }
108
109  @SuppressWarnings("unchecked") // value could not get in if not a T
110  @CheckForNull
111  private <T extends B> T trustedPut(TypeToken<@NonNull T> type, @ParametricNullness T value) {
112    return (T) backingMap.put(type, value);
113  }
114
115  @SuppressWarnings("unchecked") // value could not get in if not a T
116  @CheckForNull
117  private <T extends @NonNull B> T trustedGet(TypeToken<T> type) {
118    return (T) backingMap.get(type);
119  }
120
121  private static final class UnmodifiableEntry<K, V extends @Nullable Object>
122      extends ForwardingMapEntry<K, V> {
123
124    private final Entry<K, V> delegate;
125
126    static <K, V extends @Nullable Object> Set<Entry<K, V>> transformEntries(
127        Set<Entry<K, V>> entries) {
128      return new ForwardingSet<Map.Entry<K, V>>() {
129        @Override
130        protected Set<Entry<K, V>> delegate() {
131          return entries;
132        }
133
134        @Override
135        public Iterator<Entry<K, V>> iterator() {
136          return UnmodifiableEntry.transformEntries(super.iterator());
137        }
138
139        @Override
140        public Object[] toArray() {
141          /*
142           * standardToArray returns `@Nullable Object[]` rather than `Object[]` but only because it
143           * can be used with collections that may contain null. This collection is a collection of
144           * non-null Entry objects (Entry objects that might contain null values but are not
145           * themselves null), so we can treat it as a plain `Object[]`.
146           */
147          @SuppressWarnings("nullness")
148          Object[] result = standardToArray();
149          return result;
150        }
151
152        @Override
153        @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
154        public <T extends @Nullable Object> T[] toArray(T[] array) {
155          return standardToArray(array);
156        }
157      };
158    }
159
160    private static <K, V extends @Nullable Object> Iterator<Entry<K, V>> transformEntries(
161        Iterator<Entry<K, V>> entries) {
162      return Iterators.transform(entries, UnmodifiableEntry::new);
163    }
164
165    private UnmodifiableEntry(Entry<K, V> delegate) {
166      this.delegate = checkNotNull(delegate);
167    }
168
169    @Override
170    protected Entry<K, V> delegate() {
171      return delegate;
172    }
173
174    @Override
175    @ParametricNullness
176    public V setValue(@ParametricNullness V value) {
177      throw new UnsupportedOperationException();
178    }
179  }
180}