Coverage Summary for Class: ImmutableTypeToInstanceMap (com.google.common.reflect)
| Class | Method, % | Line, % |
|---|---|---|
| ImmutableTypeToInstanceMap | 0% (0/12) | 0% (0/13) |
| ImmutableTypeToInstanceMap$Builder | 0% (0/5) | 0% (0/8) |
| Total | 0% (0/17) | 0% (0/21) |
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 com.google.common.annotations.Beta; 18 import com.google.common.collect.ForwardingMap; 19 import com.google.common.collect.ImmutableMap; 20 import com.google.errorprone.annotations.CanIgnoreReturnValue; 21 import com.google.errorprone.annotations.DoNotCall; 22 import java.util.Map; 23 24 /** 25 * A type-to-instance map backed by an {@link ImmutableMap}. See also {@link 26 * MutableTypeToInstanceMap}. 27 * 28 * @author Ben Yu 29 * @since 13.0 30 */ 31 @Beta 32 public final class ImmutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B> 33 implements TypeToInstanceMap<B> { 34 35 /** Returns an empty type to instance map. */ 36 public static <B> ImmutableTypeToInstanceMap<B> of() { 37 return new ImmutableTypeToInstanceMap<B>(ImmutableMap.<TypeToken<? extends B>, B>of()); 38 } 39 40 /** Returns a new builder. */ 41 public static <B> Builder<B> builder() { 42 return new Builder<B>(); 43 } 44 45 /** 46 * A builder for creating immutable type-to-instance maps. Example: 47 * 48 * <pre>{@code 49 * static final ImmutableTypeToInstanceMap<Handler<?>> HANDLERS = 50 * ImmutableTypeToInstanceMap.<Handler<?>>builder() 51 * .put(new TypeToken<Handler<Foo>>() {}, new FooHandler()) 52 * .put(new TypeToken<Handler<Bar>>() {}, new SubBarHandler()) 53 * .build(); 54 * }</pre> 55 * 56 * <p>After invoking {@link #build()} it is still possible to add more entries and build again. 57 * Thus each map generated by this builder will be a superset of any map generated before it. 58 * 59 * @since 13.0 60 */ 61 @Beta 62 public static final class Builder<B> { 63 private final ImmutableMap.Builder<TypeToken<? extends B>, B> mapBuilder = 64 ImmutableMap.builder(); 65 66 private Builder() {} 67 68 /** 69 * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed, 70 * and will cause {@link #build} to fail. 71 */ 72 @CanIgnoreReturnValue 73 public <T extends B> Builder<B> put(Class<T> key, T value) { 74 mapBuilder.put(TypeToken.of(key), value); 75 return this; 76 } 77 78 /** 79 * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed, 80 * and will cause {@link #build} to fail. 81 */ 82 @CanIgnoreReturnValue 83 public <T extends B> Builder<B> put(TypeToken<T> key, T value) { 84 mapBuilder.put(key.rejectTypeVariables(), value); 85 return this; 86 } 87 88 /** 89 * Returns a new immutable type-to-instance map containing the entries provided to this builder. 90 * 91 * @throws IllegalArgumentException if duplicate keys were added 92 */ 93 public ImmutableTypeToInstanceMap<B> build() { 94 return new ImmutableTypeToInstanceMap<B>(mapBuilder.build()); 95 } 96 } 97 98 private final ImmutableMap<TypeToken<? extends B>, B> delegate; 99 100 private ImmutableTypeToInstanceMap(ImmutableMap<TypeToken<? extends B>, B> delegate) { 101 this.delegate = delegate; 102 } 103 104 @Override 105 public <T extends B> T getInstance(TypeToken<T> type) { 106 return trustedGet(type.rejectTypeVariables()); 107 } 108 109 @Override 110 public <T extends B> T getInstance(Class<T> type) { 111 return trustedGet(TypeToken.of(type)); 112 } 113 114 /** 115 * Guaranteed to throw an exception and leave the map unmodified. 116 * 117 * @deprecated unsupported operation 118 * @throws UnsupportedOperationException always 119 */ 120 @CanIgnoreReturnValue 121 @Deprecated 122 @Override 123 @DoNotCall("Always throws UnsupportedOperationException") 124 public <T extends B> T putInstance(TypeToken<T> type, T value) { 125 throw new UnsupportedOperationException(); 126 } 127 128 /** 129 * Guaranteed to throw an exception and leave the map unmodified. 130 * 131 * @deprecated unsupported operation 132 * @throws UnsupportedOperationException always 133 */ 134 @CanIgnoreReturnValue 135 @Deprecated 136 @Override 137 @DoNotCall("Always throws UnsupportedOperationException") 138 public <T extends B> T putInstance(Class<T> type, T value) { 139 throw new UnsupportedOperationException(); 140 } 141 142 /** 143 * Guaranteed to throw an exception and leave the map unmodified. 144 * 145 * @deprecated unsupported operation 146 * @throws UnsupportedOperationException always 147 */ 148 @CanIgnoreReturnValue 149 @Deprecated 150 @Override 151 @DoNotCall("Always throws UnsupportedOperationException") 152 public B put(TypeToken<? extends B> key, B value) { 153 throw new UnsupportedOperationException(); 154 } 155 156 /** 157 * Guaranteed to throw an exception and leave the map unmodified. 158 * 159 * @deprecated unsupported operation 160 * @throws UnsupportedOperationException always 161 */ 162 @Deprecated 163 @Override 164 @DoNotCall("Always throws UnsupportedOperationException") 165 public void putAll(Map<? extends TypeToken<? extends B>, ? extends B> map) { 166 throw new UnsupportedOperationException(); 167 } 168 169 @Override 170 protected Map<TypeToken<? extends B>, B> delegate() { 171 return delegate; 172 } 173 174 @SuppressWarnings("unchecked") // value could not get in if not a T 175 private <T extends B> T trustedGet(TypeToken<T> type) { 176 return (T) delegate.get(type); 177 } 178 }