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