*/
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.yangtools.yang.binding.BindingObject;
/**
- *
- * Abstract Holder of Binding to Normalized Node caches indexed by {@link DataContainerCodecContext}
- * to which cache is associated.
- *
+ * 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>() {
-
- @Override
- public BindingNormalizedNodeCache load(final DataContainerCodecContext<?, ?> key) throws Exception {
- return new BindingNormalizedNodeCache(AbstractBindingNormalizedNodeCacheHolder.this, key);
+ @SuppressWarnings("rawtypes")
+ private final LoadingCache<CodecContext, AbstractBindingNormalizedNodeCache> caches =
+ CacheBuilder.newBuilder().build(new CacheLoader<>() {
+ @Override
+ public AbstractBindingNormalizedNodeCache load(final CodecContext key) {
+ // FIXME: Use a switch expression once we have https://openjdk.org/jeps/441
+ if (key instanceof DataContainerCodecContext<?, ?, ?> dataContainer) {
+ return new DataObjectNormalizedNodeCache(AbstractBindingNormalizedNodeCacheHolder.this,
+ dataContainer);
+ }
+ if (key instanceof LeafNodeCodecContext.OfTypeObject typeObject) {
+ return new TypeObjectNormalizedNodeCache<>(typeObject);
}
+ throw new IllegalStateException("Unhandled context " + key);
+ }
+ });
- });
+ private final ImmutableSet<Class<?>> cacheSpec;
- protected AbstractBindingNormalizedNodeCacheHolder(final Set<Class<? extends DataObject>> cacheSpec) {
- cachedValues = Preconditions.checkNotNull(cacheSpec);
+ AbstractBindingNormalizedNodeCacheHolder(final ImmutableSet<Class<?>> 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 CodecContext & 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<?> type) {
+ return cacheSpec.contains(type);
}
}