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