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 com.google.common.collect.MapConstraints.ConstrainedMap;
020import com.google.common.primitives.Primitives;
021
022import java.util.HashMap;
023import java.util.Map;
024
025/**
026 * A mutable class-to-instance map backed by an arbitrary user-provided map.
027 * See also {@link ImmutableClassToInstanceMap}.
028 *
029 * <p>See the Guava User Guide article on <a href=
030 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#classtoinstancemap">
031 * {@code ClassToInstanceMap}</a>.
032 *
033 * @author Kevin Bourrillion
034 * @since 2.0
035 */
036public final class MutableClassToInstanceMap<B> extends ConstrainedMap<Class<? extends B>, B>
037    implements ClassToInstanceMap<B> {
038
039  /**
040   * Returns a new {@code MutableClassToInstanceMap} instance backed by a {@link
041   * HashMap} using the default initial capacity and load factor.
042   */
043  public static <B> MutableClassToInstanceMap<B> create() {
044    return new MutableClassToInstanceMap<B>(new HashMap<Class<? extends B>, B>());
045  }
046
047  /**
048   * Returns a new {@code MutableClassToInstanceMap} instance backed by a given
049   * empty {@code backingMap}. The caller surrenders control of the backing map,
050   * and thus should not allow any direct references to it to remain accessible.
051   */
052  public static <B> MutableClassToInstanceMap<B> create(Map<Class<? extends B>, B> backingMap) {
053    return new MutableClassToInstanceMap<B>(backingMap);
054  }
055
056  private MutableClassToInstanceMap(Map<Class<? extends B>, B> delegate) {
057    super(delegate, VALUE_CAN_BE_CAST_TO_KEY);
058  }
059
060  private static final MapConstraint<Class<?>, Object> VALUE_CAN_BE_CAST_TO_KEY =
061      new MapConstraint<Class<?>, Object>() {
062        @Override
063        public void checkKeyValue(Class<?> key, Object value) {
064          cast(key, value);
065        }
066      };
067
068  @Override
069  public <T extends B> T putInstance(Class<T> type, T value) {
070    return cast(type, put(type, value));
071  }
072
073  @Override
074  public <T extends B> T getInstance(Class<T> type) {
075    return cast(type, get(type));
076  }
077
078  private static <B, T extends B> T cast(Class<T> type, B value) {
079    return Primitives.wrap(type).cast(value);
080  }
081
082  private static final long serialVersionUID = 0;
083}