2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import com.google.common.collect.ImmutableSet;
16 import org.opendaylight.mdsal.binding.dom.codec.api.BindingObjectCodecTreeNode;
17 import org.opendaylight.mdsal.binding.dom.codec.impl.LeafNodeCodecContext.OfTypeObject;
18 import org.opendaylight.yangtools.yang.binding.BindingObject;
21 * Abstract Holder of Binding to Normalized Node caches indexed by {@link DataContainerCodecContext} to which cache is
24 abstract class AbstractBindingNormalizedNodeCacheHolder {
25 @SuppressWarnings("rawtypes")
26 private final LoadingCache<NodeCodecContext, AbstractBindingNormalizedNodeCache> caches = CacheBuilder
27 .newBuilder().build(new CacheLoader<NodeCodecContext, AbstractBindingNormalizedNodeCache>() {
29 public AbstractBindingNormalizedNodeCache load(final NodeCodecContext key) {
30 if (key instanceof DataContainerCodecContext) {
31 return new DataObjectNormalizedNodeCache(AbstractBindingNormalizedNodeCacheHolder.this,
32 (DataContainerCodecContext<?, ?>) key);
33 } else if (key instanceof OfTypeObject) {
34 return new TypeObjectNormalizedNodeCache<>((OfTypeObject)key);
36 throw new IllegalStateException("Unhandled context " + key);
41 private final ImmutableSet<Class<? extends BindingObject>> cacheSpec;
43 AbstractBindingNormalizedNodeCacheHolder(final ImmutableSet<Class<? extends BindingObject>> cacheSpec) {
44 this.cacheSpec = requireNonNull(cacheSpec);
47 @SuppressWarnings("unchecked")
48 <T extends BindingObject, C extends NodeCodecContext & BindingObjectCodecTreeNode<?>>
49 AbstractBindingNormalizedNodeCache<T, C> getCachingSerializer(final C childCtx) {
50 return isCached(childCtx.getBindingClass()) ? caches.getUnchecked(childCtx) : null;
53 final boolean isCached(final Class<? extends BindingObject> type) {
54 return cacheSpec.contains(type);