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.annotations.J2ktIncompatible; 023import com.google.common.primitives.Primitives; 024import com.google.errorprone.annotations.CanIgnoreReturnValue; 025import java.io.InvalidObjectException; 026import java.io.ObjectInputStream; 027import java.io.Serializable; 028import java.util.HashMap; 029import java.util.Iterator; 030import java.util.LinkedHashMap; 031import java.util.Map; 032import java.util.Set; 033import javax.annotation.CheckForNull; 034import org.checkerframework.checker.nullness.qual.NonNull; 035import org.checkerframework.checker.nullness.qual.Nullable; 036 037/** 038 * A mutable class-to-instance map backed by an arbitrary user-provided map. See also {@link 039 * ImmutableClassToInstanceMap}. 040 * 041 * <p>See the Guava User Guide article on <a href= 042 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#classtoinstancemap">{@code 043 * ClassToInstanceMap}</a>. 044 * 045 * @author Kevin Bourrillion 046 * @since 2.0 047 */ 048@J2ktIncompatible 049@GwtIncompatible 050@SuppressWarnings("serial") // using writeReplace instead of standard serialization 051public final class MutableClassToInstanceMap<B extends @Nullable Object> 052 extends ForwardingMap<Class<? extends @NonNull B>, B> 053 implements ClassToInstanceMap<B>, Serializable { 054 055 /** 056 * Returns a new {@code MutableClassToInstanceMap} instance backed by a {@link HashMap} using the 057 * default initial capacity and load factor. 058 */ 059 public static <B extends @Nullable Object> MutableClassToInstanceMap<B> create() { 060 return new MutableClassToInstanceMap<>(new HashMap<Class<? extends @NonNull B>, B>()); 061 } 062 063 /** 064 * Returns a new {@code MutableClassToInstanceMap} instance backed by a given empty {@code 065 * backingMap}. The caller surrenders control of the backing map, and thus should not allow any 066 * direct references to it to remain accessible. 067 */ 068 public static <B extends @Nullable Object> MutableClassToInstanceMap<B> create( 069 Map<Class<? extends @NonNull B>, B> backingMap) { 070 return new MutableClassToInstanceMap<>(backingMap); 071 } 072 073 private final Map<Class<? extends @NonNull B>, B> delegate; 074 075 private MutableClassToInstanceMap(Map<Class<? extends @NonNull B>, B> delegate) { 076 this.delegate = checkNotNull(delegate); 077 } 078 079 @Override 080 protected Map<Class<? extends @NonNull B>, B> delegate() { 081 return delegate; 082 } 083 084 /** 085 * Wraps the {@code setValue} implementation of an {@code Entry} to enforce the class constraint. 086 */ 087 private static <B extends @Nullable Object> Entry<Class<? extends @NonNull B>, B> checkedEntry( 088 final Entry<Class<? extends @NonNull B>, B> entry) { 089 return new ForwardingMapEntry<Class<? extends @NonNull B>, B>() { 090 @Override 091 protected Entry<Class<? extends @NonNull B>, B> delegate() { 092 return entry; 093 } 094 095 @Override 096 @ParametricNullness 097 public B setValue(@ParametricNullness B value) { 098 cast(getKey(), value); 099 return super.setValue(value); 100 } 101 }; 102 } 103 104 @Override 105 public Set<Entry<Class<? extends @NonNull B>, B>> entrySet() { 106 return new ForwardingSet<Entry<Class<? extends @NonNull B>, B>>() { 107 108 @Override 109 protected Set<Entry<Class<? extends @NonNull B>, B>> delegate() { 110 return MutableClassToInstanceMap.this.delegate().entrySet(); 111 } 112 113 @Override 114 public Iterator<Entry<Class<? extends @NonNull B>, B>> iterator() { 115 return new TransformedIterator< 116 Entry<Class<? extends @NonNull B>, B>, Entry<Class<? extends @NonNull B>, B>>( 117 delegate().iterator()) { 118 @Override 119 Entry<Class<? extends @NonNull B>, B> transform( 120 Entry<Class<? extends @NonNull B>, B> from) { 121 return checkedEntry(from); 122 } 123 }; 124 } 125 126 @Override 127 public Object[] toArray() { 128 /* 129 * standardToArray returns `@Nullable Object[]` rather than `Object[]` but only because it 130 * can be used with collections that may contain null. This collection is a collection of 131 * non-null Entry objects (Entry objects that might contain null values but are not 132 * themselves null), so we can treat it as a plain `Object[]`. 133 */ 134 @SuppressWarnings("nullness") 135 Object[] result = standardToArray(); 136 return result; 137 } 138 139 @Override 140 @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations 141 public <T extends @Nullable Object> T[] toArray(T[] array) { 142 return standardToArray(array); 143 } 144 }; 145 } 146 147 @Override 148 @CanIgnoreReturnValue 149 @CheckForNull 150 public B put(Class<? extends @NonNull B> key, @ParametricNullness B value) { 151 cast(key, value); 152 return super.put(key, value); 153 } 154 155 @Override 156 public void putAll(Map<? extends Class<? extends @NonNull B>, ? extends B> map) { 157 Map<Class<? extends @NonNull B>, B> copy = new LinkedHashMap<>(map); 158 for (Entry<? extends Class<? extends @NonNull B>, B> entry : copy.entrySet()) { 159 cast(entry.getKey(), entry.getValue()); 160 } 161 super.putAll(copy); 162 } 163 164 @CanIgnoreReturnValue 165 @Override 166 @CheckForNull 167 public <T extends B> T putInstance(Class<@NonNull T> type, @ParametricNullness T value) { 168 return cast(type, put(type, value)); 169 } 170 171 @Override 172 @CheckForNull 173 public <T extends @NonNull B> T getInstance(Class<T> type) { 174 return cast(type, get(type)); 175 } 176 177 @CanIgnoreReturnValue 178 @CheckForNull 179 private static <T> T cast(Class<T> type, @CheckForNull Object value) { 180 return Primitives.wrap(type).cast(value); 181 } 182 183 private Object writeReplace() { 184 return new SerializedForm<>(delegate()); 185 } 186 187 private void readObject(ObjectInputStream stream) throws InvalidObjectException { 188 throw new InvalidObjectException("Use SerializedForm"); 189 } 190 191 /** Serialized form of the map, to avoid serializing the constraint. */ 192 private static final class SerializedForm<B extends @Nullable Object> implements Serializable { 193 private final Map<Class<? extends @NonNull B>, B> backingMap; 194 195 SerializedForm(Map<Class<? extends @NonNull B>, B> backingMap) { 196 this.backingMap = backingMap; 197 } 198 199 Object readResolve() { 200 return create(backingMap); 201 } 202 203 private static final long serialVersionUID = 0; 204 } 205}