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.yangtools.binding.data.codec.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
19 * Abstract Holder of Binding to Normalized Node caches indexed by {@link DataContainerCodecContext}
20 * to which cache is associated.
23 abstract class AbstractBindingNormalizedNodeCacheHolder {
25 private final Set<Class<? extends DataObject>> cachedValues;
26 private final LoadingCache<DataContainerCodecContext<?, ?>, BindingNormalizedNodeCache> caches = CacheBuilder
27 .newBuilder().build(new CacheLoader<DataContainerCodecContext<?, ?>, BindingNormalizedNodeCache>() {
30 public BindingNormalizedNodeCache load(final DataContainerCodecContext<?, ?> key) throws Exception {
31 return new BindingNormalizedNodeCache(AbstractBindingNormalizedNodeCacheHolder.this, key);
36 protected AbstractBindingNormalizedNodeCacheHolder(final Set<Class<? extends DataObject>> cacheSpec) {
37 cachedValues = Preconditions.checkNotNull(cacheSpec);
40 BindingNormalizedNodeCache getCachingSerializer(final DataContainerCodecContext<?, ?> childCtx) {
41 if (isCached(childCtx.getBindingClass())) {
42 return caches.getUnchecked(childCtx);
47 boolean isCached(final Class<?> type) {
48 return cachedValues.contains(type);