ratpack.registry.internal.TypeCaching Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ratpack-core Show documentation
Show all versions of ratpack-core Show documentation
The core HTTP and composition mechanisms of Ratpack
/*
* Copyright 2015 the original author or 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 ratpack.registry.internal;
import com.google.common.reflect.TypeToken;
import ratpack.server.internal.ServerEnvironment;
import java.lang.reflect.Type;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
public abstract class TypeCaching {
private static class Impl {
ConcurrentMap, Boolean> cache(TypeToken left) {
return null;
}
boolean isAssignableFrom(ConcurrentMap, Boolean> cache, TypeToken left, TypeToken right) {
return isAssignableFrom(left, right);
}
private static boolean isAssignableFrom(TypeToken left, TypeToken right) {
return left.equals(right) || left.isSupertypeOf(right);
}
@SuppressWarnings("unchecked")
TypeToken typeToken(Type type) {
return (TypeToken) TypeToken.of(type);
}
}
private static class CachingImpl extends Impl {
private final ConcurrentMap, ConcurrentMap, Boolean>> assignabilityCache = new ConcurrentHashMap<>();
private final Function, ConcurrentMap, Boolean>> assignabilityIndexProducer = t -> new ConcurrentHashMap<>();
private final ConcurrentMap> typeTokensCache = new ConcurrentHashMap<>();
private final Function> typeTokenProducer = TypeToken::of;
@Override
ConcurrentMap, Boolean> cache(TypeToken left) {
ConcurrentMap, Boolean> forLeft = assignabilityCache.get(left);
if (forLeft == null) {
forLeft = assignabilityCache.computeIfAbsent(left, assignabilityIndexProducer);
}
return forLeft;
}
@Override
boolean isAssignableFrom(ConcurrentMap, Boolean> forLeft, TypeToken left, TypeToken right) {
Boolean value = forLeft.get(right);
if (value == null) {
value = Impl.isAssignableFrom(left, right);
forLeft.put(right, value);
}
return value;
}
@SuppressWarnings("unchecked")
@Override
TypeToken typeToken(Type type) {
TypeToken typeToken = typeTokensCache.get(type);
if (typeToken == null) {
return (TypeToken) typeTokensCache.computeIfAbsent(type, typeTokenProducer);
} else {
return (TypeToken) typeToken;
}
}
}
private static final Impl IMPL = ServerEnvironment.INSTANCE.isDevelopment() ? new Impl() : new CachingImpl();
public static ConcurrentMap, Boolean> cache(TypeToken left) {
return IMPL.cache(left);
}
public static boolean isAssignableFrom(ConcurrentMap, Boolean> cache, TypeToken left, TypeToken right) {
return IMPL.isAssignableFrom(cache, left, right);
}
public static TypeToken typeToken(Type type) {
return IMPL.typeToken(type);
}
public static TypeToken typeToken(TypeToken type) {
return IMPL.typeToken(type.getType());
}
}