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 org.jspecify.annotations.NonNull;
030import org.jspecify.annotations.Nullable;
031
032/**
033 * A mutable type-to-instance map. See also {@link ImmutableTypeToInstanceMap}.
034 *
035 * @author Ben Yu
036 * @since 13.0
037 */
038public final class MutableTypeToInstanceMap<B extends @Nullable Object>
039    extends ForwardingMap<TypeToken<? extends @NonNull B>, B> implements TypeToInstanceMap<B> {
040  /** Creates a new map. */
041  public MutableTypeToInstanceMap() {}
042
043  private final Map<TypeToken<? extends @NonNull B>, B> backingMap = Maps.newHashMap();
044
045  @Override
046  public <T extends @NonNull B> @Nullable T getInstance(Class<T> type) {
047    return trustedGet(TypeToken.of(type));
048  }
049
050  @Override
051  public <T extends @NonNull B> @Nullable T getInstance(TypeToken<T> type) {
052    return trustedGet(type.rejectTypeVariables());
053  }
054
055  @Override
056  @CanIgnoreReturnValue
057  public <T extends B> @Nullable T putInstance(
058      Class<@NonNull T> type, @ParametricNullness T value) {
059    return trustedPut(TypeToken.of(type), value);
060  }
061
062  @Override
063  @CanIgnoreReturnValue
064  public <T extends B> @Nullable T putInstance(
065      TypeToken<@NonNull T> type, @ParametricNullness T value) {
066    return this.<T>trustedPut(type.rejectTypeVariables(), value);
067  }
068
069  /**
070   * Not supported. Use {@link #putInstance} instead.
071   *
072   * @deprecated unsupported operation
073   * @throws UnsupportedOperationException always
074   */
075  @CanIgnoreReturnValue
076  @Deprecated
077  @Override
078  @DoNotCall("Always throws UnsupportedOperationException")
079  public @Nullable B put(TypeToken<? extends @NonNull B> key, @ParametricNullness B value) {
080    throw new UnsupportedOperationException("Please use putInstance() instead.");
081  }
082
083  /**
084   * Not supported. Use {@link #putInstance} instead.
085   *
086   * @deprecated unsupported operation
087   * @throws UnsupportedOperationException always
088   */
089  @Deprecated
090  @Override
091  @DoNotCall("Always throws UnsupportedOperationException")
092  public void putAll(Map<? extends TypeToken<? extends @NonNull B>, ? extends B> map) {
093    throw new UnsupportedOperationException("Please use putInstance() instead.");
094  }
095
096  @Override
097  public Set<Entry<TypeToken<? extends @NonNull B>, B>> entrySet() {
098    return UnmodifiableEntry.transformEntries(super.entrySet());
099  }
100
101  @Override
102  protected Map<TypeToken<? extends @NonNull B>, B> delegate() {
103    return backingMap;
104  }
105
106  @SuppressWarnings("unchecked") // value could not get in if not a T
107  private <T extends B> @Nullable T trustedPut(
108      TypeToken<@NonNull T> type, @ParametricNullness T value) {
109    return (T) backingMap.put(type, value);
110  }
111
112  @SuppressWarnings("unchecked") // value could not get in if not a T
113  private <T extends @NonNull B> @Nullable T trustedGet(TypeToken<T> type) {
114    return (T) backingMap.get(type);
115  }
116
117  private static final class UnmodifiableEntry<K, V extends @Nullable Object>
118      extends ForwardingMapEntry<K, V> {
119
120    private final Entry<K, V> delegate;
121
122    static <K, V extends @Nullable Object> Set<Entry<K, V>> transformEntries(
123        Set<Entry<K, V>> entries) {
124      return new ForwardingSet<Map.Entry<K, V>>() {
125        @Override
126        protected Set<Entry<K, V>> delegate() {
127          return entries;
128        }
129
130        @Override
131        public Iterator<Entry<K, V>> iterator() {
132          return UnmodifiableEntry.transformEntries(super.iterator());
133        }
134
135        @Override
136        public Object[] toArray() {
137          /*
138           * standardToArray returns `@Nullable Object[]` rather than `Object[]` but only because it
139           * can be used with collections that may contain null. This collection is a collection of
140           * non-null Entry objects (Entry objects that might contain null values but are not
141           * themselves null), so we can treat it as a plain `Object[]`.
142           */
143          @SuppressWarnings("nullness")
144          Object[] result = standardToArray();
145          return result;
146        }
147
148        @Override
149        @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
150        public <T extends @Nullable Object> T[] toArray(T[] array) {
151          return standardToArray(array);
152        }
153      };
154    }
155
156    private static <K, V extends @Nullable Object> Iterator<Entry<K, V>> transformEntries(
157        Iterator<Entry<K, V>> entries) {
158      return Iterators.transform(entries, UnmodifiableEntry::new);
159    }
160
161    private UnmodifiableEntry(Entry<K, V> delegate) {
162      this.delegate = checkNotNull(delegate);
163    }
164
165    @Override
166    protected Entry<K, V> delegate() {
167      return delegate;
168    }
169
170    @Override
171    @ParametricNullness
172    public V setValue(@ParametricNullness V value) {
173      throw new UnsupportedOperationException();
174    }
175  }
176}