Coverage Summary for Class: MutableTypeToInstanceMap (com.google.common.reflect)

Class Method, % Line, %
MutableTypeToInstanceMap 0% (0/11) 0% (0/12)
MutableTypeToInstanceMap$UnmodifiableEntry 0% (0/6) 0% (0/7)
MutableTypeToInstanceMap$UnmodifiableEntry$1 0% (0/5) 0% (0/5)
MutableTypeToInstanceMap$UnmodifiableEntry$2 0% (0/2) 0% (0/2)
Total 0% (0/24) 0% (0/26)


1 /* 2  * Copyright (C) 2012 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.reflect; 16  17 import static com.google.common.base.Preconditions.checkNotNull; 18  19 import com.google.common.annotations.Beta; 20 import com.google.common.base.Function; 21 import com.google.common.collect.ForwardingMap; 22 import com.google.common.collect.ForwardingMapEntry; 23 import com.google.common.collect.ForwardingSet; 24 import com.google.common.collect.Iterators; 25 import com.google.common.collect.Maps; 26 import com.google.errorprone.annotations.CanIgnoreReturnValue; 27 import com.google.errorprone.annotations.DoNotCall; 28 import java.util.Iterator; 29 import java.util.Map; 30 import java.util.Set; 31 import org.checkerframework.checker.nullness.qual.Nullable; 32  33 /** 34  * A mutable type-to-instance map. See also {@link ImmutableTypeToInstanceMap}. 35  * 36  * @author Ben Yu 37  * @since 13.0 38  */ 39 @Beta 40 public final class MutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B> 41  implements TypeToInstanceMap<B> { 42  43  private final Map<TypeToken<? extends B>, B> backingMap = Maps.newHashMap(); 44  45  @Override 46  public <T extends B> @Nullable T getInstance(Class<T> type) { 47  return trustedGet(TypeToken.of(type)); 48  } 49  50  @Override 51  public <T extends B> @Nullable T getInstance(TypeToken<T> type) { 52  return trustedGet(type.rejectTypeVariables()); 53  } 54  55  @Override 56  @CanIgnoreReturnValue 57  public <T extends B> @Nullable T putInstance(Class<T> type, @Nullable T value) { 58  return trustedPut(TypeToken.of(type), value); 59  } 60  61  @Override 62  @CanIgnoreReturnValue 63  public <T extends B> @Nullable T putInstance(TypeToken<T> type, @Nullable T value) { 64  return trustedPut(type.rejectTypeVariables(), value); 65  } 66  67  /** 68  * Not supported. Use {@link #putInstance} instead. 69  * 70  * @deprecated unsupported operation 71  * @throws UnsupportedOperationException always 72  */ 73  @CanIgnoreReturnValue 74  @Deprecated 75  @Override 76  @DoNotCall("Always throws UnsupportedOperationException") 77  public B put(TypeToken<? extends B> key, B value) { 78  throw new UnsupportedOperationException("Please use putInstance() instead."); 79  } 80  81  /** 82  * Not supported. Use {@link #putInstance} instead. 83  * 84  * @deprecated unsupported operation 85  * @throws UnsupportedOperationException always 86  */ 87  @Deprecated 88  @Override 89  @DoNotCall("Always throws UnsupportedOperationException") 90  public void putAll(Map<? extends TypeToken<? extends B>, ? extends B> map) { 91  throw new UnsupportedOperationException("Please use putInstance() instead."); 92  } 93  94  @Override 95  public Set<Entry<TypeToken<? extends B>, B>> entrySet() { 96  return UnmodifiableEntry.transformEntries(super.entrySet()); 97  } 98  99  @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 }