001/* 002 * Copyright (C) 2009 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.collect; 018 019import static com.google.common.base.Preconditions.checkNotNull; 020 021import com.google.common.annotations.GwtIncompatible; 022import com.google.common.primitives.Primitives; 023import com.google.errorprone.annotations.CanIgnoreReturnValue; 024import com.google.errorprone.annotations.DoNotCall; 025import com.google.errorprone.annotations.Immutable; 026import java.io.Serializable; 027import java.util.Map; 028import org.jspecify.annotations.NonNull; 029import org.jspecify.annotations.Nullable; 030 031/** 032 * A {@link ClassToInstanceMap} whose contents will never change, with many other important 033 * properties detailed at {@link ImmutableCollection}. 034 * 035 * @author Kevin Bourrillion 036 * @since 2.0 037 */ 038@Immutable(containerOf = "B") 039@GwtIncompatible 040// TODO(b/278589132): Remove the redundant "@NonNull" on B once it's no longer required by J2KT. 041public final class ImmutableClassToInstanceMap<B> 042 extends ForwardingMap<Class<? extends @NonNull B>, B> 043 implements ClassToInstanceMap<B>, Serializable { 044 045 private static final ImmutableClassToInstanceMap<Object> EMPTY = 046 new ImmutableClassToInstanceMap<>(ImmutableMap.<Class<?>, Object>of()); 047 048 /** 049 * Returns an empty {@code ImmutableClassToInstanceMap}. 050 * 051 * <p><b>Performance note:</b> the instance returned is a singleton. 052 * 053 * @since 19.0 054 */ 055 @SuppressWarnings("unchecked") 056 public static <B> ImmutableClassToInstanceMap<B> of() { 057 return (ImmutableClassToInstanceMap<B>) EMPTY; 058 } 059 060 /** 061 * Returns an {@code ImmutableClassToInstanceMap} containing a single entry. 062 * 063 * @since 19.0 064 */ 065 public static <B, T extends B> ImmutableClassToInstanceMap<B> of(Class<T> type, T value) { 066 ImmutableMap<Class<? extends B>, B> map = ImmutableMap.<Class<? extends B>, B>of(type, value); 067 return new ImmutableClassToInstanceMap<>(map); 068 } 069 070 /** 071 * Returns a new builder. The generated builder is equivalent to the builder created by the {@link 072 * Builder} constructor. 073 */ 074 public static <B> Builder<B> builder() { 075 return new Builder<>(); 076 } 077 078 /** 079 * A builder for creating immutable class-to-instance maps. Example: 080 * 081 * <pre>{@code 082 * static final ImmutableClassToInstanceMap<Handler> HANDLERS = 083 * new ImmutableClassToInstanceMap.Builder<Handler>() 084 * .put(FooHandler.class, new FooHandler()) 085 * .put(BarHandler.class, new SubBarHandler()) 086 * .put(Handler.class, new QuuxHandler()) 087 * .build(); 088 * }</pre> 089 * 090 * <p>After invoking {@link #build()} it is still possible to add more entries and build again. 091 * Thus each map generated by this builder will be a superset of any map generated before it. 092 * 093 * @since 2.0 094 */ 095 public static final class Builder<B> { 096 /** Creates a new builder. */ 097 public Builder() {} 098 099 private final ImmutableMap.Builder<Class<? extends B>, B> mapBuilder = ImmutableMap.builder(); 100 101 /** 102 * Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed, 103 * and will cause {@link #build} to fail. 104 */ 105 @CanIgnoreReturnValue 106 public <T extends B> Builder<B> put(Class<T> key, T value) { 107 mapBuilder.put(key, value); 108 return this; 109 } 110 111 /** 112 * Associates all of {@code map's} keys and values in the built map. Duplicate keys are not 113 * allowed, and will cause {@link #build} to fail. 114 * 115 * @throws NullPointerException if any key or value in {@code map} is null 116 * @throws ClassCastException if any value is not an instance of the type specified by its key 117 */ 118 @CanIgnoreReturnValue 119 public <T extends B> Builder<B> putAll(Map<? extends Class<? extends T>, ? extends T> map) { 120 for (Entry<? extends Class<? extends T>, ? extends T> entry : map.entrySet()) { 121 Class<? extends T> type = entry.getKey(); 122 T value = entry.getValue(); 123 mapBuilder.put(type, cast(type, value)); 124 } 125 return this; 126 } 127 128 private static <T> T cast(Class<T> type, Object value) { 129 return Primitives.wrap(type).cast(value); 130 } 131 132 /** 133 * Returns a new immutable class-to-instance map containing the entries provided to this 134 * builder. 135 * 136 * @throws IllegalArgumentException if duplicate keys were added 137 */ 138 public ImmutableClassToInstanceMap<B> build() { 139 ImmutableMap<Class<? extends B>, B> map = mapBuilder.buildOrThrow(); 140 if (map.isEmpty()) { 141 return of(); 142 } else { 143 return new ImmutableClassToInstanceMap<>(map); 144 } 145 } 146 } 147 148 /** 149 * Returns an immutable map containing the same entries as {@code map}. If {@code map} somehow 150 * contains entries with duplicate keys (for example, if it is a {@code SortedMap} whose 151 * comparator is not <i>consistent with equals</i>), the results of this method are undefined. 152 * 153 * <p><b>Note:</b> Despite what the method name suggests, if {@code map} is an {@code 154 * ImmutableClassToInstanceMap}, no copy will actually be performed. 155 * 156 * @throws NullPointerException if any key or value in {@code map} is null 157 * @throws ClassCastException if any value is not an instance of the type specified by its key 158 */ 159 public static <B, S extends B> ImmutableClassToInstanceMap<B> copyOf( 160 Map<? extends Class<? extends S>, ? extends S> map) { 161 if (map instanceof ImmutableClassToInstanceMap) { 162 @SuppressWarnings("rawtypes") // JDT-based J2KT Java frontend does not permit the direct cast 163 Map rawMap = map; 164 @SuppressWarnings("unchecked") // covariant casts safe (unmodifiable) 165 ImmutableClassToInstanceMap<B> cast = (ImmutableClassToInstanceMap<B>) rawMap; 166 return cast; 167 } 168 return new Builder<B>().putAll(map).build(); 169 } 170 171 private final ImmutableMap<Class<? extends B>, B> delegate; 172 173 private ImmutableClassToInstanceMap(ImmutableMap<Class<? extends B>, B> delegate) { 174 this.delegate = delegate; 175 } 176 177 @Override 178 protected Map<Class<? extends B>, B> delegate() { 179 return delegate; 180 } 181 182 @Override 183 @SuppressWarnings("unchecked") // value could not get in if not a T 184 public <T extends B> @Nullable T getInstance(Class<T> type) { 185 return (T) delegate.get(checkNotNull(type)); 186 } 187 188 /** 189 * Guaranteed to throw an exception and leave the map unmodified. 190 * 191 * @throws UnsupportedOperationException always 192 * @deprecated Unsupported operation. 193 */ 194 @CanIgnoreReturnValue 195 @Deprecated 196 @Override 197 @DoNotCall("Always throws UnsupportedOperationException") 198 public <T extends B> @Nullable T putInstance(Class<T> type, T value) { 199 throw new UnsupportedOperationException(); 200 } 201 202 Object readResolve() { 203 return isEmpty() ? of() : this; 204 } 205}