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.Nullable; 031 032/** 033 * A mutable type-to-instance map. See also {@link ImmutableTypeToInstanceMap}. 034 * 035 * <p>This implementation <i>does</i> support null values, despite how it is annotated; see 036 * discussion at {@link TypeToInstanceMap}. 037 * 038 * @author Ben Yu 039 * @since 13.0 040 */ 041@ElementTypesAreNonnullByDefault 042public final class MutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B> 043 implements TypeToInstanceMap<B> { 044 045 private final Map<TypeToken<? extends B>, B> backingMap = Maps.newHashMap(); 046 047 @Override 048 @CheckForNull 049 public <T extends B> T getInstance(Class<T> type) { 050 return trustedGet(TypeToken.of(type)); 051 } 052 053 @Override 054 @CheckForNull 055 public <T extends 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<T> type, 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<T> type, T value) { 070 return 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 B> key, 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 B>, ? extends B> map) { 098 throw new UnsupportedOperationException("Please use putInstance() instead."); 099 } 100 101 @Override 102 public Set<Entry<TypeToken<? extends B>, B>> entrySet() { 103 return UnmodifiableEntry.transformEntries(super.entrySet()); 104 } 105 106 @Override 107 protected Map<TypeToken<? extends 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<T> type, 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 B> T trustedGet(TypeToken<T> type) { 120 return (T) backingMap.get(type); 121 } 122 123 private static final class UnmodifiableEntry<K, V> extends ForwardingMapEntry<K, V> { 124 125 private final Entry<K, V> delegate; 126 127 static <K, V> Set<Entry<K, V>> transformEntries(Set<Entry<K, V>> entries) { 128 return new ForwardingSet<Map.Entry<K, V>>() { 129 @Override 130 protected Set<Entry<K, V>> delegate() { 131 return entries; 132 } 133 134 @Override 135 public Iterator<Entry<K, V>> iterator() { 136 return UnmodifiableEntry.transformEntries(super.iterator()); 137 } 138 139 @Override 140 public Object[] toArray() { 141 /* 142 * standardToArray returns `@Nullable Object[]` rather than `Object[]` but only because it 143 * can be used with collections that may contain null. This collection is a collection of 144 * non-null Entry objects (Entry objects that might contain null values but are not 145 * themselves null), so we can treat it as a plain `Object[]`. 146 */ 147 @SuppressWarnings("nullness") 148 Object[] result = standardToArray(); 149 return result; 150 } 151 152 @Override 153 @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations 154 public <T extends @Nullable Object> T[] toArray(T[] array) { 155 return standardToArray(array); 156 } 157 }; 158 } 159 160 private static <K, V> Iterator<Entry<K, V>> transformEntries(Iterator<Entry<K, V>> entries) { 161 return Iterators.transform(entries, UnmodifiableEntry::new); 162 } 163 164 private UnmodifiableEntry(java.util.Map.Entry<K, V> delegate) { 165 this.delegate = checkNotNull(delegate); 166 } 167 168 @Override 169 protected Entry<K, V> delegate() { 170 return delegate; 171 } 172 173 @Override 174 public V setValue(V value) { 175 throw new UnsupportedOperationException(); 176 } 177 } 178}