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;
031
032/**
033 * A mutable class-to-instance map backed by an arbitrary user-provided map. See also {@link
034 * ImmutableClassToInstanceMap}.
035 *
036 * <p>See the Guava User Guide article on <a href=
037 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#classtoinstancemap"> {@code
038 * ClassToInstanceMap}</a>.
039 *
040 * @author Kevin Bourrillion
041 * @since 2.0
042 */
043@GwtIncompatible
044@SuppressWarnings("serial") // using writeReplace instead of standard serialization
045public final class MutableClassToInstanceMap<B> extends ForwardingMap<Class<? extends B>, B>
046    implements ClassToInstanceMap<B>, Serializable {
047
048  /**
049   * Returns a new {@code MutableClassToInstanceMap} instance backed by a {@link HashMap} using the
050   * default initial capacity and load factor.
051   */
052  public static <B> MutableClassToInstanceMap<B> create() {
053    return new MutableClassToInstanceMap<B>(new HashMap<Class<? extends B>, B>());
054  }
055
056  /**
057   * Returns a new {@code MutableClassToInstanceMap} instance backed by a given empty {@code
058   * backingMap}. The caller surrenders control of the backing map, and thus should not allow any
059   * direct references to it to remain accessible.
060   */
061  public static <B> MutableClassToInstanceMap<B> create(Map<Class<? extends B>, B> backingMap) {
062    return new MutableClassToInstanceMap<B>(backingMap);
063  }
064
065  private final Map<Class<? extends B>, B> delegate;
066
067  private MutableClassToInstanceMap(Map<Class<? extends B>, B> delegate) {
068    this.delegate = checkNotNull(delegate);
069  }
070
071  @Override
072  protected Map<Class<? extends B>, B> delegate() {
073    return delegate;
074  }
075
076  /**
077   * Wraps the {@code setValue} implementation of an {@code Entry} to enforce the class constraint.
078   */
079  private static <B> Entry<Class<? extends B>, B> checkedEntry(
080      final Entry<Class<? extends B>, B> entry) {
081    return new ForwardingMapEntry<Class<? extends B>, B>() {
082      @Override
083      protected Entry<Class<? extends B>, B> delegate() {
084        return entry;
085      }
086
087      @Override
088      public B setValue(B value) {
089        return super.setValue(cast(getKey(), value));
090      }
091    };
092  }
093
094  @Override
095  public Set<Entry<Class<? extends B>, B>> entrySet() {
096    return new ForwardingSet<Entry<Class<? extends B>, B>>() {
097
098      @Override
099      protected Set<Entry<Class<? extends B>, B>> delegate() {
100        return MutableClassToInstanceMap.this.delegate().entrySet();
101      }
102
103      @Override
104      public Spliterator<Entry<Class<? extends B>, B>> spliterator() {
105        return CollectSpliterators.map(
106            delegate().spliterator(), MutableClassToInstanceMap::checkedEntry);
107      }
108
109      @Override
110      public Iterator<Entry<Class<? extends B>, B>> iterator() {
111        return new TransformedIterator<Entry<Class<? extends B>, B>, Entry<Class<? extends B>, B>>(
112            delegate().iterator()) {
113          @Override
114          Entry<Class<? extends B>, B> transform(Entry<Class<? extends B>, B> from) {
115            return checkedEntry(from);
116          }
117        };
118      }
119
120      @Override
121      public Object[] toArray() {
122        return standardToArray();
123      }
124
125      @Override
126      public <T> T[] toArray(T[] array) {
127        return standardToArray(array);
128      }
129    };
130  }
131
132  @Override
133  @CanIgnoreReturnValue
134  public B put(Class<? extends B> key, B value) {
135    return super.put(key, cast(key, value));
136  }
137
138  @Override
139  public void putAll(Map<? extends Class<? extends B>, ? extends B> map) {
140    Map<Class<? extends B>, B> copy = new LinkedHashMap<>(map);
141    for (Entry<? extends Class<? extends B>, B> entry : copy.entrySet()) {
142      cast(entry.getKey(), entry.getValue());
143    }
144    super.putAll(copy);
145  }
146
147  @CanIgnoreReturnValue
148  @Override
149  public <T extends B> T putInstance(Class<T> type, T value) {
150    return cast(type, put(type, value));
151  }
152
153  @Override
154  public <T extends B> T getInstance(Class<T> type) {
155    return cast(type, get(type));
156  }
157
158  @CanIgnoreReturnValue
159  private static <B, T extends B> T cast(Class<T> type, B value) {
160    return Primitives.wrap(type).cast(value);
161  }
162
163  private Object writeReplace() {
164    return new SerializedForm(delegate());
165  }
166
167  /** Serialized form of the map, to avoid serializing the constraint. */
168  private static final class SerializedForm<B> implements Serializable {
169    private final Map<Class<? extends B>, B> backingMap;
170
171    SerializedForm(Map<Class<? extends B>, B> backingMap) {
172      this.backingMap = backingMap;
173    }
174
175    Object readResolve() {
176      return create(backingMap);
177    }
178
179    private static final long serialVersionUID = 0;
180  }
181}