shaded.com.google.common.reflect.ImmutableTypeToInstanceMap Maven / Gradle / Ivy
Show all versions of spring-cloud-contract-shade Show documentation
/*
* Copyright (C) 2012 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package shaded.shaded.com.google.common.reflect;
import shaded.shaded.com.google.common.annotations.Beta;
import shaded.shaded.com.google.common.collect.ForwardingMap;
import shaded.shaded.com.google.common.collect.ImmutableMap;
import shaded.shaded.com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Map;
/**
* A type-to-instance map backed by an {@link ImmutableMap}. See also {@link
* MutableTypeToInstanceMap}.
*
* @author Ben Yu
* @since 13.0
*/
@Beta
public final class ImmutableTypeToInstanceMap extends ForwardingMap, B>
implements TypeToInstanceMap {
/** Returns an empty type to instance map. */
public static ImmutableTypeToInstanceMap of() {
return new ImmutableTypeToInstanceMap(ImmutableMap., B>of());
}
/** Returns a new builder. */
public static Builder builder() {
return new Builder();
}
/**
* A builder for creating immutable type-to-instance maps. Example:
*
* {@code
* static final ImmutableTypeToInstanceMap> HANDLERS =
* ImmutableTypeToInstanceMap.>builder()
* .put(new TypeToken>() {}, new FooHandler())
* .put(new TypeToken>() {}, new SubBarHandler())
* .build();
* }
*
* After invoking {@link #build()} it is still possible to add more entries and build again.
* Thus each map generated by this builder will be a superset of any map generated before it.
*
* @since 13.0
*/
@Beta
public static final class Builder {
private final ImmutableMap.Builder, B> mapBuilder =
ImmutableMap.builder();
private Builder() {}
/**
* Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed,
* and will cause {@link #build} to fail.
*/
@CanIgnoreReturnValue
public Builder put(Class key, T value) {
mapBuilder.put(TypeToken.of(key), value);
return this;
}
/**
* Associates {@code key} with {@code value} in the built map. Duplicate keys are not allowed,
* and will cause {@link #build} to fail.
*/
@CanIgnoreReturnValue
public Builder put(TypeToken key, T value) {
mapBuilder.put(key.rejectTypeVariables(), value);
return this;
}
/**
* Returns a new immutable type-to-instance map containing the entries provided to this builder.
*
* @throws IllegalArgumentException if duplicate keys were added
*/
public ImmutableTypeToInstanceMap build() {
return new ImmutableTypeToInstanceMap(mapBuilder.build());
}
}
private final ImmutableMap, B> delegate;
private ImmutableTypeToInstanceMap(ImmutableMap, B> delegate) {
this.delegate = delegate;
}
@Override
public T getInstance(TypeToken type) {
return trustedGet(type.rejectTypeVariables());
}
@Override
public T getInstance(Class type) {
return trustedGet(TypeToken.of(type));
}
/**
* Guaranteed to throw an exception and leave the map unmodified.
*
* @deprecated unsupported operation
* @throws UnsupportedOperationException always
*/
@CanIgnoreReturnValue
@Deprecated
@Override
public T putInstance(TypeToken type, T value) {
throw new UnsupportedOperationException();
}
/**
* Guaranteed to throw an exception and leave the map unmodified.
*
* @deprecated unsupported operation
* @throws UnsupportedOperationException always
*/
@CanIgnoreReturnValue
@Deprecated
@Override
public T putInstance(Class type, T value) {
throw new UnsupportedOperationException();
}
/**
* Guaranteed to throw an exception and leave the map unmodified.
*
* @deprecated unsupported operation
* @throws UnsupportedOperationException always
*/
@CanIgnoreReturnValue
@Deprecated
@Override
public B put(TypeToken key, B value) {
throw new UnsupportedOperationException();
}
/**
* Guaranteed to throw an exception and leave the map unmodified.
*
* @deprecated unsupported operation
* @throws UnsupportedOperationException always
*/
@Deprecated
@Override
public void putAll(Map, ? extends B> map) {
throw new UnsupportedOperationException();
}
@Override
protected Map, B> delegate() {
return delegate;
}
@SuppressWarnings("unchecked") // value could not get in if not a T
private T trustedGet(TypeToken type) {
return (T) delegate.get(type);
}
}