001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.reflect; 018 019import com.google.common.annotations.Beta; 020import com.google.common.collect.ForwardingMap; 021import com.google.common.collect.ImmutableMap; 022 023import java.util.Map; 024 025/** 026 * A type-to-instance map backed by an {@link ImmutableMap}. See also {@link 027 * MutableTypeToInstanceMap}. 028 * 029 * @author Ben Yu 030 * @since 13.0 031 */ 032@Beta 033public final class ImmutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B> 034 implements TypeToInstanceMap<B> { 035 036 /** Returns an empty type to instance map. */ 037 public static <B> ImmutableTypeToInstanceMap<B> of() { 038 return new ImmutableTypeToInstanceMap<B>(ImmutableMap.<TypeToken<? extends B>, B>of()); 039 } 040 041 /** Returns a new builder. */ 042 public static <B> Builder<B> builder() { 043 return new Builder<B>(); 044 } 045 046 /** 047 * A builder for creating immutable type-to-instance maps. Example: 048 * <pre> {@code 049 * 050 * static final ImmutableTypeToInstanceMap<Handler<?>> HANDLERS = 051 * ImmutableTypeToInstanceMap.<Handler<?>>builder() 052 * .put(new TypeToken<Handler<Foo>>() {}, new FooHandler()) 053 * .put(new TypeToken<Handler<Bar>>() {}, new SubBarHandler()) 054 * .build();}</pre> 055 * 056 * <p>After invoking {@link #build()} it is still possible to add more entries 057 * and build again. Thus each map generated by this builder will be a superset 058 * of any map generated before it. 059 * 060 * @since 13.0 061 */ 062 @Beta 063 public static final class Builder<B> { 064 private final ImmutableMap.Builder<TypeToken<? extends B>, B> mapBuilder 065 = ImmutableMap.builder(); 066 067 private Builder() {} 068 069 /** 070 * Associates {@code key} with {@code value} in the built map. Duplicate 071 * keys are not allowed, and will cause {@link #build} to fail. 072 */ 073 public <T extends B> Builder<B> put(Class<T> key, T value) { 074 mapBuilder.put(TypeToken.of(key), value); 075 return this; 076 } 077 078 /** 079 * Associates {@code key} with {@code value} in the built map. Duplicate 080 * keys are not allowed, and will cause {@link #build} to fail. 081 */ 082 public <T extends B> Builder<B> put(TypeToken<T> key, T value) { 083 mapBuilder.put(key.rejectTypeVariables(), value); 084 return this; 085 } 086 087 /** 088 * Returns a new immutable type-to-instance map containing the entries 089 * provided to this builder. 090 * 091 * @throws IllegalArgumentException if duplicate keys were added 092 */ 093 public ImmutableTypeToInstanceMap<B> build() { 094 return new ImmutableTypeToInstanceMap<B>(mapBuilder.build()); 095 } 096 } 097 098 private final ImmutableMap<TypeToken<? extends B>, B> delegate; 099 100 private ImmutableTypeToInstanceMap(ImmutableMap<TypeToken<? extends B>, B> delegate) { 101 this.delegate = delegate; 102 } 103 104 @Override public <T extends B> T getInstance(TypeToken<T> type) { 105 return trustedGet(type.rejectTypeVariables()); 106 } 107 108 /** 109 * Guaranteed to throw an exception and leave the map unmodified. 110 * 111 * @throws UnsupportedOperationException always 112 */ 113 @Override public <T extends B> T putInstance(TypeToken<T> type, T value) { 114 throw new UnsupportedOperationException(); 115 } 116 117 @Override public <T extends B> T getInstance(Class<T> type) { 118 return trustedGet(TypeToken.of(type)); 119 } 120 121 /** 122 * Guaranteed to throw an exception and leave the map unmodified. 123 * 124 * @throws UnsupportedOperationException always 125 */ 126 @Override public <T extends B> T putInstance(Class<T> type, T value) { 127 throw new UnsupportedOperationException(); 128 } 129 130 @Override protected Map<TypeToken<? extends B>, B> delegate() { 131 return delegate; 132 } 133 134 @SuppressWarnings("unchecked") // value could not get in if not a T 135 private <T extends B> T trustedGet(TypeToken<T> type) { 136 return (T) delegate.get(type); 137 } 138}