001/* 002 * Copyright (C) 2012 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.reflect; 018 019import static com.google.common.base.Preconditions.checkNotNull; 020 021import com.google.common.annotations.Beta; 022import com.google.common.base.Function; 023import com.google.common.collect.ForwardingMap; 024import com.google.common.collect.ForwardingMapEntry; 025import com.google.common.collect.ForwardingSet; 026import com.google.common.collect.Iterators; 027import com.google.common.collect.Maps; 028 029import java.util.Iterator; 030import java.util.Map; 031import java.util.Set; 032 033import javax.annotation.Nullable; 034 035/** 036 * A mutable type-to-instance map. 037 * See also {@link ImmutableTypeToInstanceMap}. 038 * 039 * @author Ben Yu 040 * @since 13.0 041 */ 042@Beta 043public final class MutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B> 044 implements TypeToInstanceMap<B> { 045 046 private final Map<TypeToken<? extends B>, B> backingMap = Maps.newHashMap(); 047 048 @Nullable 049 @Override 050 public <T extends B> T getInstance(Class<T> type) { 051 return trustedGet(TypeToken.of(type)); 052 } 053 054 @Nullable 055 @Override 056 public <T extends B> T putInstance(Class<T> type, @Nullable T value) { 057 return trustedPut(TypeToken.of(type), value); 058 } 059 060 @Nullable 061 @Override 062 public <T extends B> T getInstance(TypeToken<T> type) { 063 return trustedGet(type.rejectTypeVariables()); 064 } 065 066 @Nullable 067 @Override 068 public <T extends B> T putInstance(TypeToken<T> type, @Nullable T value) { 069 return trustedPut(type.rejectTypeVariables(), value); 070 } 071 072 /** Not supported. Use {@link #putInstance} instead. */ 073 @Override public B put(TypeToken<? extends B> key, B value) { 074 throw new UnsupportedOperationException("Please use putInstance() instead."); 075 } 076 077 /** Not supported. Use {@link #putInstance} instead. */ 078 @Override public void putAll(Map<? extends TypeToken<? extends B>, ? extends B> map) { 079 throw new UnsupportedOperationException("Please use putInstance() instead."); 080 } 081 082 @Override public Set<Entry<TypeToken<? extends B>, B>> entrySet() { 083 return UnmodifiableEntry.transformEntries(super.entrySet()); 084 } 085 086 @Override protected Map<TypeToken<? extends B>, B> delegate() { 087 return backingMap; 088 } 089 090 @SuppressWarnings("unchecked") // value could not get in if not a T 091 @Nullable 092 private <T extends B> T trustedPut(TypeToken<T> type, @Nullable T value) { 093 return (T) backingMap.put(type, value); 094 } 095 096 @SuppressWarnings("unchecked") // value could not get in if not a T 097 @Nullable 098 private <T extends B> T trustedGet(TypeToken<T> type) { 099 return (T) backingMap.get(type); 100 } 101 102 private static final class UnmodifiableEntry<K, V> extends ForwardingMapEntry<K, V> { 103 104 private final Entry<K, V> delegate; 105 106 static <K, V> Set<Entry<K, V>> transformEntries(final Set<Entry<K, V>> entries) { 107 return new ForwardingSet<Map.Entry<K, V>>() { 108 @Override protected Set<Entry<K, V>> delegate() { 109 return entries; 110 } 111 @Override public Iterator<Entry<K, V>> iterator() { 112 return UnmodifiableEntry.transformEntries(super.iterator()); 113 } 114 @Override public Object[] toArray() { 115 return standardToArray(); 116 } 117 @Override public <T> T[] toArray(T[] array) { 118 return standardToArray(array); 119 } 120 }; 121 } 122 123 private static <K, V> Iterator<Entry<K, V>> transformEntries(Iterator<Entry<K, V>> entries) { 124 return Iterators.transform(entries, new Function<Entry<K, V>, Entry<K, V>>() { 125 @Override public Entry<K, V> apply(Entry<K, V> entry) { 126 return new UnmodifiableEntry<K, V>(entry); 127 } 128 }); 129 } 130 131 private UnmodifiableEntry(java.util.Map.Entry<K, V> delegate) { 132 this.delegate = checkNotNull(delegate); 133 } 134 135 @Override protected Entry<K, V> delegate() { 136 return delegate; 137 } 138 139 @Override public V setValue(V value) { 140 throw new UnsupportedOperationException(); 141 } 142 } 143}