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.primitives.Primitives;
023import com.google.errorprone.annotations.CanIgnoreReturnValue;
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.Iterator;
027import java.util.LinkedHashMap;
028import java.util.Map;
029import java.util.Set;
030import java.util.Spliterator;
031import javax.annotation.CheckForNull;
032import org.checkerframework.checker.nullness.qual.Nullable;
033
034/**
035 * A mutable class-to-instance map backed by an arbitrary user-provided map. See also {@link
036 * ImmutableClassToInstanceMap}.
037 *
038 * <p>See the Guava User Guide article on <a href=
039 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#classtoinstancemap">{@code
040 * ClassToInstanceMap}</a>.
041 *
042 * <p>This implementation <i>does</i> support null values, despite how it is annotated; see
043 * discussion at {@link ClassToInstanceMap}.
044 *
045 * @author Kevin Bourrillion
046 * @since 2.0
047 */
048@GwtIncompatible
049@SuppressWarnings("serial") // using writeReplace instead of standard serialization
050@ElementTypesAreNonnullByDefault
051public final class MutableClassToInstanceMap<B> extends ForwardingMap<Class<? extends B>, B>
052    implements ClassToInstanceMap<B>, Serializable {
053
054  /**
055   * Returns a new {@code MutableClassToInstanceMap} instance backed by a {@link HashMap} using the
056   * default initial capacity and load factor.
057   */
058  public static <B> MutableClassToInstanceMap<B> create() {
059    return new MutableClassToInstanceMap<B>(new HashMap<Class<? extends B>, B>());
060  }
061
062  /**
063   * Returns a new {@code MutableClassToInstanceMap} instance backed by a given empty {@code
064   * backingMap}. The caller surrenders control of the backing map, and thus should not allow any
065   * direct references to it to remain accessible.
066   */
067  public static <B> MutableClassToInstanceMap<B> create(Map<Class<? extends B>, B> backingMap) {
068    return new MutableClassToInstanceMap<B>(backingMap);
069  }
070
071  private final Map<Class<? extends B>, B> delegate;
072
073  private MutableClassToInstanceMap(Map<Class<? extends B>, B> delegate) {
074    this.delegate = checkNotNull(delegate);
075  }
076
077  @Override
078  protected Map<Class<? extends B>, B> delegate() {
079    return delegate;
080  }
081
082  /**
083   * Wraps the {@code setValue} implementation of an {@code Entry} to enforce the class constraint.
084   */
085  private static <B> Entry<Class<? extends B>, B> checkedEntry(
086      final Entry<Class<? extends B>, B> entry) {
087    return new ForwardingMapEntry<Class<? extends B>, B>() {
088      @Override
089      protected Entry<Class<? extends B>, B> delegate() {
090        return entry;
091      }
092
093      @Override
094      public B setValue(B value) {
095        return super.setValue(cast(getKey(), value));
096      }
097    };
098  }
099
100  @Override
101  public Set<Entry<Class<? extends B>, B>> entrySet() {
102    return new ForwardingSet<Entry<Class<? extends B>, B>>() {
103
104      @Override
105      protected Set<Entry<Class<? extends B>, B>> delegate() {
106        return MutableClassToInstanceMap.this.delegate().entrySet();
107      }
108
109      @Override
110      public Spliterator<Entry<Class<? extends B>, B>> spliterator() {
111        return CollectSpliterators.map(
112            delegate().spliterator(), MutableClassToInstanceMap::checkedEntry);
113      }
114
115      @Override
116      public Iterator<Entry<Class<? extends B>, B>> iterator() {
117        return new TransformedIterator<Entry<Class<? extends B>, B>, Entry<Class<? extends B>, B>>(
118            delegate().iterator()) {
119          @Override
120          Entry<Class<? extends B>, B> transform(Entry<Class<? extends B>, B> from) {
121            return checkedEntry(from);
122          }
123        };
124      }
125
126      @Override
127      public Object[] toArray() {
128        /*
129         * standardToArray returns `@Nullable Object[]` rather than `Object[]` but only because it
130         * can be used with collections that may contain null. This collection is a collection of
131         * non-null Entry objects (Entry objects that might contain null values but are not
132         * themselves null), so we can treat it as a plain `Object[]`.
133         */
134        @SuppressWarnings("nullness")
135        Object[] result = standardToArray();
136        return result;
137      }
138
139      @Override
140      @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
141      public <T extends @Nullable Object> T[] toArray(T[] array) {
142        return standardToArray(array);
143      }
144    };
145  }
146
147  @Override
148  @CanIgnoreReturnValue
149  @CheckForNull
150  public B put(Class<? extends B> key, B value) {
151    return super.put(key, cast(key, value));
152  }
153
154  @Override
155  public void putAll(Map<? extends Class<? extends B>, ? extends B> map) {
156    Map<Class<? extends B>, B> copy = new LinkedHashMap<>(map);
157    for (Entry<? extends Class<? extends B>, B> entry : copy.entrySet()) {
158      cast(entry.getKey(), entry.getValue());
159    }
160    super.putAll(copy);
161  }
162
163  @CanIgnoreReturnValue
164  @Override
165  @CheckForNull
166  public <T extends B> T putInstance(Class<T> type, T value) {
167    return cast(type, put(type, value));
168  }
169
170  @Override
171  @CheckForNull
172  public <T extends B> T getInstance(Class<T> type) {
173    return cast(type, get(type));
174  }
175
176  @CanIgnoreReturnValue
177  @CheckForNull
178  private static <B, T extends B> T cast(Class<T> type, @CheckForNull B value) {
179    return Primitives.wrap(type).cast(value);
180  }
181
182  private Object writeReplace() {
183    return new SerializedForm(delegate());
184  }
185
186  /** Serialized form of the map, to avoid serializing the constraint. */
187  private static final class SerializedForm<B> implements Serializable {
188    private final Map<Class<? extends B>, B> backingMap;
189
190    SerializedForm(Map<Class<? extends B>, B> backingMap) {
191      this.backingMap = backingMap;
192    }
193
194    Object readResolve() {
195      return create(backingMap);
196    }
197
198    private static final long serialVersionUID = 0;
199  }
200}