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.annotations.Beta;
020import com.google.common.base.Function;
021import com.google.common.collect.ForwardingMap;
022import com.google.common.collect.ForwardingMapEntry;
023import com.google.common.collect.ForwardingSet;
024import com.google.common.collect.Iterators;
025import com.google.common.collect.Maps;
026import com.google.errorprone.annotations.CanIgnoreReturnValue;
027import com.google.errorprone.annotations.DoNotCall;
028import java.util.Iterator;
029import java.util.Map;
030import java.util.Set;
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@Beta
040public final class MutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B>
041    implements TypeToInstanceMap<B> {
042
043  private final Map<TypeToken<? extends B>, B> backingMap = Maps.newHashMap();
044
045  @Override
046  public <T extends B> @Nullable T getInstance(Class<T> type) {
047    return trustedGet(TypeToken.of(type));
048  }
049
050  @Override
051  public <T extends B> @Nullable T getInstance(TypeToken<T> type) {
052    return trustedGet(type.rejectTypeVariables());
053  }
054
055  @Override
056  @CanIgnoreReturnValue
057  public <T extends B> @Nullable T putInstance(Class<T> type, @Nullable T value) {
058    return trustedPut(TypeToken.of(type), value);
059  }
060
061  @Override
062  @CanIgnoreReturnValue
063  public <T extends B> @Nullable T putInstance(TypeToken<T> type, @Nullable T value) {
064    return trustedPut(type.rejectTypeVariables(), value);
065  }
066
067  /**
068   * Not supported. Use {@link #putInstance} instead.
069   *
070   * @deprecated unsupported operation
071   * @throws UnsupportedOperationException always
072   */
073  @CanIgnoreReturnValue
074  @Deprecated
075  @Override
076  @DoNotCall("Always throws UnsupportedOperationException")
077  public B put(TypeToken<? extends B> key, B value) {
078    throw new UnsupportedOperationException("Please use putInstance() instead.");
079  }
080
081  /**
082   * Not supported. Use {@link #putInstance} instead.
083   *
084   * @deprecated unsupported operation
085   * @throws UnsupportedOperationException always
086   */
087  @Deprecated
088  @Override
089  @DoNotCall("Always throws UnsupportedOperationException")
090  public void putAll(Map<? extends TypeToken<? extends B>, ? extends B> map) {
091    throw new UnsupportedOperationException("Please use putInstance() instead.");
092  }
093
094  @Override
095  public Set<Entry<TypeToken<? extends B>, B>> entrySet() {
096    return UnmodifiableEntry.transformEntries(super.entrySet());
097  }
098
099  @Override
100  protected Map<TypeToken<? extends B>, B> delegate() {
101    return backingMap;
102  }
103
104  @SuppressWarnings("unchecked") // value could not get in if not a T
105  private <T extends B> @Nullable T trustedPut(TypeToken<T> type, @Nullable T value) {
106    return (T) backingMap.put(type, value);
107  }
108
109  @SuppressWarnings("unchecked") // value could not get in if not a T
110  private <T extends B> @Nullable T trustedGet(TypeToken<T> type) {
111    return (T) backingMap.get(type);
112  }
113
114  private static final class UnmodifiableEntry<K, V> extends ForwardingMapEntry<K, V> {
115
116    private final Entry<K, V> delegate;
117
118    static <K, V> Set<Entry<K, V>> transformEntries(final Set<Entry<K, V>> entries) {
119      return new ForwardingSet<Map.Entry<K, V>>() {
120        @Override
121        protected Set<Entry<K, V>> delegate() {
122          return entries;
123        }
124
125        @Override
126        public Iterator<Entry<K, V>> iterator() {
127          return UnmodifiableEntry.transformEntries(super.iterator());
128        }
129
130        @Override
131        public Object[] toArray() {
132          return standardToArray();
133        }
134
135        @Override
136        public <T> T[] toArray(T[] array) {
137          return standardToArray(array);
138        }
139      };
140    }
141
142    private static <K, V> Iterator<Entry<K, V>> transformEntries(Iterator<Entry<K, V>> entries) {
143      return Iterators.transform(
144          entries,
145          new Function<Entry<K, V>, Entry<K, V>>() {
146            @Override
147            public Entry<K, V> apply(Entry<K, V> entry) {
148              return new UnmodifiableEntry<>(entry);
149            }
150          });
151    }
152
153    private UnmodifiableEntry(java.util.Map.Entry<K, V> delegate) {
154      this.delegate = checkNotNull(delegate);
155    }
156
157    @Override
158    protected Entry<K, V> delegate() {
159      return delegate;
160    }
161
162    @Override
163    public V setValue(V value) {
164      throw new UnsupportedOperationException();
165    }
166  }
167}