001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import com.google.common.annotations.GwtIncompatible;
022import com.google.common.annotations.J2ktIncompatible;
023import com.google.common.primitives.Primitives;
024import com.google.errorprone.annotations.CanIgnoreReturnValue;
025import java.io.InvalidObjectException;
026import java.io.ObjectInputStream;
027import java.io.Serializable;
028import java.util.HashMap;
029import java.util.Iterator;
030import java.util.LinkedHashMap;
031import java.util.Map;
032import java.util.Set;
033import java.util.Spliterator;
034import org.jspecify.annotations.NonNull;
035import org.jspecify.annotations.Nullable;
036
037/**
038 * A mutable class-to-instance map backed by an arbitrary user-provided map. See also {@link
039 * ImmutableClassToInstanceMap}.
040 *
041 * <p>See the Guava User Guide article on <a href=
042 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#classtoinstancemap">{@code
043 * ClassToInstanceMap}</a>.
044 *
045 * @author Kevin Bourrillion
046 * @since 2.0
047 */
048@J2ktIncompatible
049@GwtIncompatible
050@SuppressWarnings("serial") // using writeReplace instead of standard serialization
051public final class MutableClassToInstanceMap<B extends @Nullable Object>
052    extends ForwardingMap<Class<? extends @NonNull B>, B>
053    implements ClassToInstanceMap<B>, Serializable {
054
055  /**
056   * Returns a new {@code MutableClassToInstanceMap} instance backed by a {@link HashMap} using the
057   * default initial capacity and load factor.
058   */
059  public static <B extends @Nullable Object> MutableClassToInstanceMap<B> create() {
060    return new MutableClassToInstanceMap<>(new HashMap<Class<? extends @NonNull B>, B>());
061  }
062
063  /**
064   * Returns a new {@code MutableClassToInstanceMap} instance backed by a given empty {@code
065   * backingMap}. The caller surrenders control of the backing map, and thus should not allow any
066   * direct references to it to remain accessible.
067   */
068  public static <B extends @Nullable Object> MutableClassToInstanceMap<B> create(
069      Map<Class<? extends @NonNull B>, B> backingMap) {
070    return new MutableClassToInstanceMap<>(backingMap);
071  }
072
073  private final Map<Class<? extends @NonNull B>, B> delegate;
074
075  private MutableClassToInstanceMap(Map<Class<? extends @NonNull B>, B> delegate) {
076    this.delegate = checkNotNull(delegate);
077  }
078
079  @Override
080  protected Map<Class<? extends @NonNull B>, B> delegate() {
081    return delegate;
082  }
083
084  /**
085   * Wraps the {@code setValue} implementation of an {@code Entry} to enforce the class constraint.
086   */
087  private static <B extends @Nullable Object> Entry<Class<? extends @NonNull B>, B> checkedEntry(
088      final Entry<Class<? extends @NonNull B>, B> entry) {
089    return new ForwardingMapEntry<Class<? extends @NonNull B>, B>() {
090      @Override
091      protected Entry<Class<? extends @NonNull B>, B> delegate() {
092        return entry;
093      }
094
095      @Override
096      @ParametricNullness
097      public B setValue(@ParametricNullness B value) {
098        cast(getKey(), value);
099        return super.setValue(value);
100      }
101    };
102  }
103
104  @Override
105  public Set<Entry<Class<? extends @NonNull B>, B>> entrySet() {
106    return new ForwardingSet<Entry<Class<? extends @NonNull B>, B>>() {
107
108      @Override
109      protected Set<Entry<Class<? extends @NonNull B>, B>> delegate() {
110        return MutableClassToInstanceMap.this.delegate().entrySet();
111      }
112
113      @Override
114      public Spliterator<Entry<Class<? extends @NonNull B>, B>> spliterator() {
115        return CollectSpliterators.map(
116            delegate().spliterator(), MutableClassToInstanceMap::checkedEntry);
117      }
118
119      @Override
120      public Iterator<Entry<Class<? extends @NonNull B>, B>> iterator() {
121        return new TransformedIterator<
122            Entry<Class<? extends @NonNull B>, B>, Entry<Class<? extends @NonNull B>, B>>(
123            delegate().iterator()) {
124          @Override
125          Entry<Class<? extends @NonNull B>, B> transform(
126              Entry<Class<? extends @NonNull B>, B> from) {
127            return checkedEntry(from);
128          }
129        };
130      }
131
132      @Override
133      public Object[] toArray() {
134        /*
135         * standardToArray returns `@Nullable Object[]` rather than `Object[]` but only because it
136         * can be used with collections that may contain null. This collection is a collection of
137         * non-null Entry objects (Entry objects that might contain null values but are not
138         * themselves null), so we can treat it as a plain `Object[]`.
139         */
140        @SuppressWarnings("nullness")
141        Object[] result = standardToArray();
142        return result;
143      }
144
145      @Override
146      @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
147      public <T extends @Nullable Object> T[] toArray(T[] array) {
148        return standardToArray(array);
149      }
150    };
151  }
152
153  @Override
154  @CanIgnoreReturnValue
155  public @Nullable B put(Class<? extends @NonNull B> key, @ParametricNullness B value) {
156    cast(key, value);
157    return super.put(key, value);
158  }
159
160  @Override
161  public void putAll(Map<? extends Class<? extends @NonNull B>, ? extends B> map) {
162    Map<Class<? extends @NonNull B>, B> copy = new LinkedHashMap<>(map);
163    for (Entry<? extends Class<? extends @NonNull B>, B> entry : copy.entrySet()) {
164      cast(entry.getKey(), entry.getValue());
165    }
166    super.putAll(copy);
167  }
168
169  @CanIgnoreReturnValue
170  @Override
171  public <T extends B> @Nullable T putInstance(
172      Class<@NonNull T> type, @ParametricNullness T value) {
173    return cast(type, put(type, value));
174  }
175
176  @Override
177  public <T extends @NonNull B> @Nullable T getInstance(Class<T> type) {
178    return cast(type, get(type));
179  }
180
181  @CanIgnoreReturnValue
182  private static <T> @Nullable T cast(Class<T> type, @Nullable Object value) {
183    return Primitives.wrap(type).cast(value);
184  }
185
186  private Object writeReplace() {
187    return new SerializedForm<>(delegate());
188  }
189
190  private void readObject(ObjectInputStream stream) throws InvalidObjectException {
191    throw new InvalidObjectException("Use SerializedForm");
192  }
193
194  /** Serialized form of the map, to avoid serializing the constraint. */
195  private static final class SerializedForm<B extends @Nullable Object> implements Serializable {
196    private final Map<Class<? extends @NonNull B>, B> backingMap;
197
198    SerializedForm(Map<Class<? extends @NonNull B>, B> backingMap) {
199      this.backingMap = backingMap;
200    }
201
202    Object readResolve() {
203      return create(backingMap);
204    }
205
206    private static final long serialVersionUID = 0;
207  }
208}