2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.javav2.dom.codec.impl.cache;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.DataContainerCodecContext;
18 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
21 * Abstract Holder of Binding to Normalized Node caches indexed by
22 * {@link DataContainerCodecContext} to which cache is associated.
26 public abstract class AbstractBindingNormalizedNodeCacheHolder {
28 private final Set<Class<? extends TreeNode>> cachedValues;
29 private final LoadingCache<DataContainerCodecContext<?, ?>, BindingNormalizedNodeCache> caches = CacheBuilder
30 .newBuilder().build(new CacheLoader<DataContainerCodecContext<?, ?>, BindingNormalizedNodeCache>() {
33 public BindingNormalizedNodeCache load(@Nonnull final DataContainerCodecContext<?, ?> key)
35 return new BindingNormalizedNodeCache(AbstractBindingNormalizedNodeCacheHolder.this, key);
40 protected AbstractBindingNormalizedNodeCacheHolder(@Nonnull final Set<Class<? extends TreeNode>> cacheSpec) {
41 cachedValues = Preconditions.checkNotNull(cacheSpec);
44 public BindingNormalizedNodeCache getCachingSerializer(final DataContainerCodecContext<?, ?> childCtx) {
45 if (isCached(childCtx.getBindingClass())) {
46 return caches.getUnchecked(childCtx);
52 * Check if specific type is cached.
56 * @return true if type is cached, false otherwise
58 public boolean isCached(final Class<? extends TreeNode> type) {
59 return cachedValues.contains(type);