import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import com.google.common.collect.BiMap;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashBiMap;
private final Multimap<Type, Type> choiceToCases = HashMultimap.create();
private final Map<QName, Type> identities = new HashMap<>();
+ private final LoadingCache<QName, Class<?>> identityClasses = CacheBuilder.newBuilder().weakValues().build(
+ new CacheLoader<QName, Class<?>>() {
+ @Override
+ public Class<?> load(final QName key) {
+ final Type identityType = identities.get(key);
+ Preconditions.checkArgument(identityType != null, "Supplied QName %s is not a valid identity", key);
+ try {
+ return strategy.loadClass(identityType);
+ } catch (ClassNotFoundException e) {
+ throw new IllegalArgumentException("Required class " + identityType + "was not found.", e);
+ }
+ }
+ });
+
private BindingRuntimeContext(final ClassLoadingStrategy strategy, final SchemaContext schema) {
this.strategy = strategy;
this.schemaContext = schema;
}
public Class<?> getIdentityClass(final QName input) {
- Type identityType = identities.get(input);
- Preconditions.checkArgument(identityType != null, "Supplied QName %s is not a valid identity", input);
- try {
- return strategy.loadClass(identityType);
- } catch (ClassNotFoundException e) {
- throw new IllegalArgumentException("Required class " + identityType + "was not found.",e);
- }
+ return identityClasses.getUnchecked(input);
}
-
}