001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.reflect; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018 019import com.google.common.collect.ForwardingMap; 020import com.google.common.collect.ForwardingMapEntry; 021import com.google.common.collect.ForwardingSet; 022import com.google.common.collect.Iterators; 023import com.google.common.collect.Maps; 024import com.google.errorprone.annotations.CanIgnoreReturnValue; 025import com.google.errorprone.annotations.DoNotCall; 026import java.util.Iterator; 027import java.util.Map; 028import java.util.Set; 029import javax.annotation.CheckForNull; 030import org.checkerframework.checker.nullness.qual.NonNull; 031import org.checkerframework.checker.nullness.qual.Nullable; 032 033/** 034 * A mutable type-to-instance map. See also {@link ImmutableTypeToInstanceMap}. 035 * 036 * @author Ben Yu 037 * @since 13.0 038 */ 039@ElementTypesAreNonnullByDefault 040public final class MutableTypeToInstanceMap<B extends @Nullable Object> 041 extends ForwardingMap<TypeToken<? extends @NonNull B>, B> implements TypeToInstanceMap<B> { 042 /** Creates a new map. */ 043 public MutableTypeToInstanceMap() {} 044 045 private final Map<TypeToken<? extends @NonNull B>, B> backingMap = Maps.newHashMap(); 046 047 @Override 048 @CheckForNull 049 public <T extends @NonNull B> T getInstance(Class<T> type) { 050 return trustedGet(TypeToken.of(type)); 051 } 052 053 @Override 054 @CheckForNull 055 public <T extends @NonNull B> T getInstance(TypeToken<T> type) { 056 return trustedGet(type.rejectTypeVariables()); 057 } 058 059 @Override 060 @CanIgnoreReturnValue 061 @CheckForNull 062 public <T extends B> T putInstance(Class<@NonNull T> type, @ParametricNullness T value) { 063 return trustedPut(TypeToken.of(type), value); 064 } 065 066 @Override 067 @CanIgnoreReturnValue 068 @CheckForNull 069 public <T extends B> T putInstance(TypeToken<@NonNull T> type, @ParametricNullness T value) { 070 return this.<T>trustedPut(type.rejectTypeVariables(), value); 071 } 072 073 /** 074 * Not supported. Use {@link #putInstance} instead. 075 * 076 * @deprecated unsupported operation 077 * @throws UnsupportedOperationException always 078 */ 079 @CanIgnoreReturnValue 080 @Deprecated 081 @Override 082 @DoNotCall("Always throws UnsupportedOperationException") 083 @CheckForNull 084 public B put(TypeToken<? extends @NonNull B> key, @ParametricNullness B value) { 085 throw new UnsupportedOperationException("Please use putInstance() instead."); 086 } 087 088 /** 089 * Not supported. Use {@link #putInstance} instead. 090 * 091 * @deprecated unsupported operation 092 * @throws UnsupportedOperationException always 093 */ 094 @Deprecated 095 @Override 096 @DoNotCall("Always throws UnsupportedOperationException") 097 public void putAll(Map<? extends TypeToken<? extends @NonNull B>, ? extends B> map) { 098 throw new UnsupportedOperationException("Please use putInstance() instead."); 099 } 100 101 @Override 102 public Set<Entry<TypeToken<? extends @NonNull B>, B>> entrySet() { 103 return UnmodifiableEntry.transformEntries(super.entrySet()); 104 } 105 106 @Override 107 protected Map<TypeToken<? extends @NonNull B>, B> delegate() { 108 return backingMap; 109 } 110 111 @SuppressWarnings("unchecked") // value could not get in if not a T 112 @CheckForNull 113 private <T extends B> T trustedPut(TypeToken<@NonNull T> type, @ParametricNullness T value) { 114 return (T) backingMap.put(type, value); 115 } 116 117 @SuppressWarnings("unchecked") // value could not get in if not a T 118 @CheckForNull 119 private <T extends @NonNull B> T trustedGet(TypeToken<T> type) { 120 return (T) backingMap.get(type); 121 } 122 123 private static final class UnmodifiableEntry<K, V extends @Nullable Object> 124 extends ForwardingMapEntry<K, V> { 125 126 private final Entry<K, V> delegate; 127 128 static <K, V extends @Nullable Object> Set<Entry<K, V>> transformEntries( 129 Set<Entry<K, V>> entries) { 130 return new ForwardingSet<Map.Entry<K, V>>() { 131 @Override 132 protected Set<Entry<K, V>> delegate() { 133 return entries; 134 } 135 136 @Override 137 public Iterator<Entry<K, V>> iterator() { 138 return UnmodifiableEntry.transformEntries(super.iterator()); 139 } 140 141 @Override 142 public Object[] toArray() { 143 /* 144 * standardToArray returns `@Nullable Object[]` rather than `Object[]` but only because it 145 * can be used with collections that may contain null. This collection is a collection of 146 * non-null Entry objects (Entry objects that might contain null values but are not 147 * themselves null), so we can treat it as a plain `Object[]`. 148 */ 149 @SuppressWarnings("nullness") 150 Object[] result = standardToArray(); 151 return result; 152 } 153 154 @Override 155 @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations 156 public <T extends @Nullable Object> T[] toArray(T[] array) { 157 return standardToArray(array); 158 } 159 }; 160 } 161 162 private static <K, V extends @Nullable Object> Iterator<Entry<K, V>> transformEntries( 163 Iterator<Entry<K, V>> entries) { 164 return Iterators.transform(entries, UnmodifiableEntry::new); 165 } 166 167 private UnmodifiableEntry(Entry<K, V> delegate) { 168 this.delegate = checkNotNull(delegate); 169 } 170 171 @Override 172 protected Entry<K, V> delegate() { 173 return delegate; 174 } 175 176 @Override 177 @ParametricNullness 178 public V setValue(@ParametricNullness V value) { 179 throw new UnsupportedOperationException(); 180 } 181 } 182}