org.elasticsearch.util.collect.MutableClassToInstanceMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Open Source, Distributed, RESTful Search Engine
/*
* Copyright (C) 2007 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 org.elasticsearch.util.collect;
import java.util.HashMap;
import java.util.Map;
/**
* A mutable class-to-instance map backed by an arbitrary user-provided map.
* See also {@link ImmutableClassToInstanceMap}.
*
* @author Kevin Bourrillion
*/
public final class MutableClassToInstanceMap
extends ConstrainedMap, B>
implements ClassToInstanceMap {
/**
* Returns a new {@code MutableClassToInstanceMap} instance backed by a {@link
* HashMap} using the default initial capacity and load factor.
*/
public static MutableClassToInstanceMap create() {
return new MutableClassToInstanceMap(
new HashMap, B>());
}
/**
* Returns a new {@code MutableClassToInstanceMap} instance backed by a given
* empty {@code backingMap}. The caller surrenders control of the backing map,
* and thus should not allow any direct references to it to remain accessible.
*/
public static MutableClassToInstanceMap create(
Map, B> backingMap) {
return new MutableClassToInstanceMap(backingMap);
}
private MutableClassToInstanceMap(Map, B> delegate) {
super(delegate, VALUE_CAN_BE_CAST_TO_KEY);
}
private static final MapConstraint, Object> VALUE_CAN_BE_CAST_TO_KEY
= new MapConstraint, Object>() {
public void checkKeyValue(Class key, Object value) {
cast(key, value);
}
};
public T putInstance(Class type, T value) {
return cast(type, put(type, value));
}
public T getInstance(Class type) {
return cast(type, get(type));
}
// Default access so that ImmutableClassToInstanceMap can share it
static T cast(Class type, B value) {
// TODO: this should eventually use common.primitives.Primitives.wrap()
return wrap(type).cast(value);
}
// safe because both Long.class and long.class are of type Class
@SuppressWarnings("unchecked")
private static Class wrap(Class c) {
return c.isPrimitive() ? (Class) PRIMITIVES_TO_WRAPPERS.get(c) : c;
}
private static final Map, Class> PRIMITIVES_TO_WRAPPERS
= new ImmutableMap.Builder, Class>()
.put(boolean.class, Boolean.class)
.put(byte.class, Byte.class)
.put(char.class, Character.class)
.put(double.class, Double.class)
.put(float.class, Float.class)
.put(int.class, Integer.class)
.put(long.class, Long.class)
.put(short.class, Short.class)
.put(void.class, Void.class)
.build();
}