com.google.common.collect.ImmutableClassToInstanceMap Maven / Gradle / Ivy
Show all versions of google-collections Show documentation
/*
* Copyright (C) 2009 Google Inc.
*
* 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 com.google.common.collect;
import static com.google.common.collect.MutableClassToInstanceMap.cast;
import java.util.Map;
/**
* A class-to-instance map backed by an {@link ImmutableMap}. See also {@link
* MutableClassToInstanceMap}.
*
* @author Kevin Bourrillion
*/
public final class ImmutableClassToInstanceMap extends
ForwardingMap, B> implements ClassToInstanceMap {
/**
* Returns a new builder. The generated builder is equivalent to the builder
* created by the {@link Builder} constructor.
*/
public static Builder builder() {
return new Builder();
}
/**
* A builder for creating immutable class-to-instance maps. Example:
* {@code
*
* static final ImmutableClassToInstanceMap HANDLERS =
* new ImmutableClassToInstanceMap.Builder()
* .put(FooHandler.class, new FooHandler())
* .put(BarHandler.class, new SubBarHandler())
* .put(Handler.class, new QuuxHandler())
* .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.
*/
public static final class Builder {
private final ImmutableMap.Builder, B> mapBuilder
= ImmutableMap.builder();
/**
* Associates {@code key} with {@code value} in the built map. Duplicate
* keys are not allowed, and will cause {@link #build} to fail.
*/
public Builder put(Class type, T value) {
mapBuilder.put(type, value);
return this;
}
/**
* Associates all of {@code map's} keys and values in the built map.
* Duplicate keys are not allowed, and will cause {@link #build} to fail.
*
* @throws NullPointerException if any key or value in {@code map} is null
* @throws ClassCastException if any value is not an instance of the type
* specified by its key
*/
public Builder putAll(
Map extends Class extends T>, ? extends T> map) {
for (Entry extends Class extends T>, ? extends T> entry
: map.entrySet()) {
Class extends T> type = entry.getKey();
T value = entry.getValue();
mapBuilder.put(type, cast(type, value));
}
return this;
}
/**
* Returns a new immutable class-to-instance map containing the entries
* provided to this builder.
*
* @throws IllegalArgumentException if duplicate keys were added
*/
public ImmutableClassToInstanceMap build() {
return new ImmutableClassToInstanceMap(mapBuilder.build());
}
}
/**
* Returns an immutable map containing the same entries as {@code map}. If
* {@code map} somehow contains entries with duplicate keys (for example, if
* it is a {@code SortedMap} whose comparator is not consistent with
* equals), the results of this method are undefined.
*
* Note: Despite what the method name suggests, if {@code map} is
* an {@code ImmutableClassToInstanceMap}, no copy will actually be performed.
*
* @throws NullPointerException if any key or value in {@code map} is null
* @throws ClassCastException if any value is not an instance of the type
* specified by its key
*/
@SuppressWarnings("unchecked") // covariant casts safe (unmodifiable)
public static ImmutableClassToInstanceMap copyOf(
Map extends Class extends S>, ? extends S> map) {
if (map instanceof ImmutableClassToInstanceMap) {
return (ImmutableClassToInstanceMap) (Map) map;
}
return new Builder().putAll(map).build();
}
private final ImmutableMap, B> delegate;
private ImmutableClassToInstanceMap(
ImmutableMap, B> delegate) {
this.delegate = delegate;
}
@Override protected Map, B> delegate() {
return delegate;
}
@SuppressWarnings("unchecked") // value could not get in if not a T
public T getInstance(Class type) {
return (T) delegate.get(type);
}
/**
* Guaranteed to throw an exception and leave the map unmodified.
*
* @throws UnsupportedOperationException always
*/
public T putInstance(Class type, T value) {
throw new UnsupportedOperationException();
}
}