*/
package org.opendaylight.mdsal.binding.dom.codec.impl;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
-import java.util.Set;
-import org.opendaylight.yangtools.yang.binding.DataObject;
+import com.google.common.collect.ImmutableSet;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingObjectCodecTreeNode;
+import org.opendaylight.mdsal.binding.dom.codec.impl.LeafNodeCodecContext.OfTypeObject;
+import org.opendaylight.yangtools.yang.binding.BindingObject;
/**
* Abstract Holder of Binding to Normalized Node caches indexed by {@link DataContainerCodecContext} to which cache is
* associated.
*/
abstract class AbstractBindingNormalizedNodeCacheHolder {
-
- private final Set<Class<? extends DataObject>> cachedValues;
- private final LoadingCache<DataContainerCodecContext<?, ?>, BindingNormalizedNodeCache> caches = CacheBuilder
- .newBuilder().build(new CacheLoader<DataContainerCodecContext<?, ?>, BindingNormalizedNodeCache>() {
+ @SuppressWarnings("rawtypes")
+ private final LoadingCache<NodeCodecContext, AbstractBindingNormalizedNodeCache> caches = CacheBuilder
+ .newBuilder().build(new CacheLoader<NodeCodecContext, AbstractBindingNormalizedNodeCache>() {
@Override
- public BindingNormalizedNodeCache load(final DataContainerCodecContext<?, ?> key) {
- return new BindingNormalizedNodeCache(AbstractBindingNormalizedNodeCacheHolder.this, key);
+ public AbstractBindingNormalizedNodeCache load(final NodeCodecContext key) {
+ if (key instanceof DataContainerCodecContext) {
+ return new DataObjectNormalizedNodeCache(AbstractBindingNormalizedNodeCacheHolder.this,
+ (DataContainerCodecContext<?, ?>) key);
+ } else if (key instanceof OfTypeObject) {
+ return new TypeObjectNormalizedNodeCache<>((OfTypeObject)key);
+ } else {
+ throw new IllegalStateException("Unhandled context " + key);
+ }
}
});
- protected AbstractBindingNormalizedNodeCacheHolder(final Set<Class<? extends DataObject>> cacheSpec) {
- cachedValues = Preconditions.checkNotNull(cacheSpec);
+ private final ImmutableSet<Class<? extends BindingObject>> cacheSpec;
+
+ AbstractBindingNormalizedNodeCacheHolder(final ImmutableSet<Class<? extends BindingObject>> cacheSpec) {
+ this.cacheSpec = requireNonNull(cacheSpec);
}
- BindingNormalizedNodeCache getCachingSerializer(final DataContainerCodecContext<?, ?> childCtx) {
- if (isCached(childCtx.getBindingClass())) {
- return caches.getUnchecked(childCtx);
- }
- return null;
+ @SuppressWarnings("unchecked")
+ <T extends BindingObject, C extends NodeCodecContext & BindingObjectCodecTreeNode<?>>
+ AbstractBindingNormalizedNodeCache<T, C> getCachingSerializer(final C childCtx) {
+ return isCached(childCtx.getBindingClass()) ? caches.getUnchecked(childCtx) : null;
}
- boolean isCached(final Class<?> type) {
- return cachedValues.contains(type);
+ final boolean isCached(final Class<? extends BindingObject> type) {
+ return cacheSpec.contains(type);
}
}