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