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 com.google.common.annotations.Beta; 018import com.google.common.collect.ForwardingMap; 019import com.google.common.collect.ImmutableMap; 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import java.util.Map; 022 023/** 024 * A type-to-instance map backed by an {@link ImmutableMap}. See also 025 * {@link MutableTypeToInstanceMap}. 026 * 027 * @author Ben Yu 028 * @since 13.0 029 */ 030@Beta 031public final class ImmutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B> 032 implements TypeToInstanceMap<B> { 033 034 /** Returns an empty type to instance map. */ 035 public static <B> ImmutableTypeToInstanceMap<B> of() { 036 return new ImmutableTypeToInstanceMap<B>(ImmutableMap.<TypeToken<? extends B>, B>of()); 037 } 038 039 /** Returns a new builder. */ 040 public static <B> Builder<B> builder() { 041 return new Builder<B>(); 042 } 043 044 /** 045 * A builder for creating immutable type-to-instance maps. Example: 046 * <pre> {@code 047 * 048 * static final ImmutableTypeToInstanceMap<Handler<?>> HANDLERS = 049 * ImmutableTypeToInstanceMap.<Handler<?>>builder() 050 * .put(new TypeToken<Handler<Foo>>() {}, new FooHandler()) 051 * .put(new TypeToken<Handler<Bar>>() {}, new SubBarHandler()) 052 * .build();}</pre> 053 * 054 * <p>After invoking {@link #build()} it is still possible to add more entries and build again. 055 * Thus each map generated by this builder will be a superset of any map generated before it. 056 * 057 * @since 13.0 058 */ 059 @Beta 060 public static final class Builder<B> { 061 private final ImmutableMap.Builder<TypeToken<? extends B>, B> mapBuilder = 062 ImmutableMap.builder(); 063 064 private Builder() {} 065 066 /** 067 * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed, 068 * and will cause {@link #build} to fail. 069 */ 070 @CanIgnoreReturnValue 071 public <T extends B> Builder<B> put(Class<T> key, T value) { 072 mapBuilder.put(TypeToken.of(key), value); 073 return this; 074 } 075 076 /** 077 * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed, 078 * and will cause {@link #build} to fail. 079 */ 080 @CanIgnoreReturnValue 081 public <T extends B> Builder<B> put(TypeToken<T> key, T value) { 082 mapBuilder.put(key.rejectTypeVariables(), value); 083 return this; 084 } 085 086 /** 087 * Returns a new immutable type-to-instance map containing the entries provided to this builder. 088 * 089 * @throws IllegalArgumentException if duplicate keys were added 090 */ 091 public ImmutableTypeToInstanceMap<B> build() { 092 return new ImmutableTypeToInstanceMap<B>(mapBuilder.build()); 093 } 094 } 095 096 private final ImmutableMap<TypeToken<? extends B>, B> delegate; 097 098 private ImmutableTypeToInstanceMap(ImmutableMap<TypeToken<? extends B>, B> delegate) { 099 this.delegate = delegate; 100 } 101 102 @Override 103 public <T extends B> T getInstance(TypeToken<T> type) { 104 return trustedGet(type.rejectTypeVariables()); 105 } 106 107 /** 108 * Guaranteed to throw an exception and leave the map unmodified. 109 * 110 * @deprecated unsupported operation 111 * @throws UnsupportedOperationException always 112 */ 113 @CanIgnoreReturnValue 114 @Deprecated 115 @Override 116 public <T extends B> T putInstance(TypeToken<T> type, T value) { 117 throw new UnsupportedOperationException(); 118 } 119 120 @Override 121 public <T extends B> T getInstance(Class<T> type) { 122 return trustedGet(TypeToken.of(type)); 123 } 124 125 /** 126 * Guaranteed to throw an exception and leave the map unmodified. 127 * 128 * @deprecated unsupported operation 129 * @throws UnsupportedOperationException always 130 */ 131 @CanIgnoreReturnValue 132 @Deprecated 133 @Override 134 public <T extends B> T putInstance(Class<T> type, T value) { 135 throw new UnsupportedOperationException(); 136 } 137 138 /** 139 * Guaranteed to throw an exception and leave the map unmodified. 140 * 141 * @deprecated unsupported operation 142 * @throws UnsupportedOperationException always 143 */ 144 @CanIgnoreReturnValue 145 @Deprecated 146 @Override 147 public B put(TypeToken<? extends B> key, B value) { 148 throw new UnsupportedOperationException(); 149 } 150 151 /** 152 * Guaranteed to throw an exception and leave the map unmodified. 153 * 154 * @deprecated unsupported operation 155 * @throws UnsupportedOperationException always 156 */ 157 @Deprecated 158 @Override 159 public void putAll(Map<? extends TypeToken<? extends B>, ? extends B> map) { 160 throw new UnsupportedOperationException(); 161 } 162 163 @Override 164 protected Map<TypeToken<? extends B>, B> delegate() { 165 return delegate; 166 } 167 168 @SuppressWarnings("unchecked") // value could not get in if not a T 169 private <T extends B> T trustedGet(TypeToken<T> type) { 170 return (T) delegate.get(type); 171 } 172}