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 org.jspecify.annotations.NonNull; 035import org.jspecify.annotations.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 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 Spliterator<Entry<Class<? extends @NonNull B>, B>> spliterator() { 115 return CollectSpliterators.map( 116 delegate().spliterator(), 117 Spliterator.DISTINCT | Spliterator.NONNULL, 118 MutableClassToInstanceMap::checkedEntry); 119 } 120 121 @Override 122 public Iterator<Entry<Class<? extends @NonNull B>, B>> iterator() { 123 return new TransformedIterator< 124 Entry<Class<? extends @NonNull B>, B>, Entry<Class<? extends @NonNull B>, B>>( 125 delegate().iterator()) { 126 @Override 127 Entry<Class<? extends @NonNull B>, B> transform( 128 Entry<Class<? extends @NonNull B>, B> from) { 129 return checkedEntry(from); 130 } 131 }; 132 } 133 134 @Override 135 public Object[] toArray() { 136 /* 137 * standardToArray returns `@Nullable Object[]` rather than `Object[]` but only because it 138 * can be used with collections that may contain null. This collection is a collection of 139 * non-null Entry objects (Entry objects that might contain null values but are not 140 * themselves null), so we can treat it as a plain `Object[]`. 141 */ 142 @SuppressWarnings("nullness") 143 Object[] result = standardToArray(); 144 return result; 145 } 146 147 @Override 148 @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations 149 public <T extends @Nullable Object> T[] toArray(T[] array) { 150 return standardToArray(array); 151 } 152 }; 153 } 154 155 @Override 156 @CanIgnoreReturnValue 157 public @Nullable B put(Class<? extends @NonNull B> key, @ParametricNullness B value) { 158 cast(key, value); // If we pass this result to `put` below, it upsets our nullness checker. 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 public <T extends B> @Nullable T putInstance( 174 Class<@NonNull T> type, @ParametricNullness T value) { 175 return cast(type, put(type, value)); 176 } 177 178 @Override 179 public <T extends @NonNull B> @Nullable T getInstance(Class<T> type) { 180 return cast(type, get(type)); 181 } 182 183 @CanIgnoreReturnValue 184 private static <T> @Nullable T cast(Class<T> type, @Nullable Object value) { 185 return Primitives.wrap(type).cast(value); 186 } 187 188 private Object writeReplace() { 189 return new SerializedForm<>(delegate()); 190 } 191 192 private void readObject(ObjectInputStream stream) throws InvalidObjectException { 193 throw new InvalidObjectException("Use SerializedForm"); 194 } 195 196 /** Serialized form of the map, to avoid serializing the constraint. */ 197 private static final class SerializedForm<B extends @Nullable Object> implements Serializable { 198 private final Map<Class<? extends @NonNull B>, B> backingMap; 199 200 SerializedForm(Map<Class<? extends @NonNull B>, B> backingMap) { 201 this.backingMap = backingMap; 202 } 203 204 Object readResolve() { 205 return create(backingMap); 206 } 207 208 private static final long serialVersionUID = 0; 209 } 210}