1 /* 2 * Copyright (C) 2007 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import java.util.HashMap; 20 import java.util.Map; 21 22 /** 23 * A mutable class-to-instance map backed by an arbitrary user-provided map. 24 * See also {@link ImmutableClassToInstanceMap}. 25 * 26 * @author Kevin Bourrillion 27 * @since 2010.01.04 <b>stable</b> (imported from Google Collections Library) 28 */ 29 public final class MutableClassToInstanceMap<B> 30 extends ConstrainedMap<Class<? extends B>, B> 31 implements ClassToInstanceMap<B> { 32 33 /** 34 * Returns a new {@code MutableClassToInstanceMap} instance backed by a {@link 35 * HashMap} using the default initial capacity and load factor. 36 */ 37 public static <B> MutableClassToInstanceMap<B> create() { 38 return new MutableClassToInstanceMap<B>( 39 new HashMap<Class<? extends B>, B>()); 40 } 41 42 /** 43 * Returns a new {@code MutableClassToInstanceMap} instance backed by a given 44 * empty {@code backingMap}. The caller surrenders control of the backing map, 45 * and thus should not allow any direct references to it to remain accessible. 46 */ 47 public static <B> MutableClassToInstanceMap<B> create( 48 Map<Class<? extends B>, B> backingMap) { 49 return new MutableClassToInstanceMap<B>(backingMap); 50 } 51 52 private MutableClassToInstanceMap(Map<Class<? extends B>, B> delegate) { 53 super(delegate, VALUE_CAN_BE_CAST_TO_KEY); 54 } 55 56 private static final MapConstraint<Class<?>, Object> VALUE_CAN_BE_CAST_TO_KEY 57 = new MapConstraint<Class<?>, Object>() { 58 public void checkKeyValue(Class<?> key, Object value) { 59 cast(key, value); 60 } 61 }; 62 63 public <T extends B> T putInstance(Class<T> type, T value) { 64 return cast(type, put(type, value)); 65 } 66 67 public <T extends B> T getInstance(Class<T> type) { 68 return cast(type, get(type)); 69 } 70 71 // Default access so that ImmutableClassToInstanceMap can share it 72 static <B, T extends B> T cast(Class<T> type, B value) { 73 // TODO: this should eventually use common.primitives.Primitives.wrap() 74 return wrap(type).cast(value); 75 } 76 77 // safe because both Long.class and long.class are of type Class<Long> 78 @SuppressWarnings("unchecked") 79 private static <T> Class<T> wrap(Class<T> c) { 80 return c.isPrimitive() ? (Class<T>) PRIMITIVES_TO_WRAPPERS.get(c) : c; 81 } 82 83 private static final Map<Class<?>, Class<?>> PRIMITIVES_TO_WRAPPERS 84 = new ImmutableMap.Builder<Class<?>, Class<?>>() 85 .put(boolean.class, Boolean.class) 86 .put(byte.class, Byte.class) 87 .put(char.class, Character.class) 88 .put(double.class, Double.class) 89 .put(float.class, Float.class) 90 .put(int.class, Integer.class) 91 .put(long.class, Long.class) 92 .put(short.class, Short.class) 93 .put(void.class, Void.class) 94 .build(); 95 96 private static final long serialVersionUID = 0; 97 } 98