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