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 static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.cache.CacheLoader;
15 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.dom.codec.impl.serializer.CachingNormalizedNodeSerializer;
19 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 * Cached NormalizedNode representation of TreeNode.
26 public class BindingNormalizedNodeCache extends CacheLoader<TreeNode, NormalizedNode<?, ?>> {
28 @SuppressWarnings("rawtypes")
29 private final DataContainerCodecContext subtreeRoot;
30 private final AbstractBindingNormalizedNodeCacheHolder cacheHolder;
31 private final LoadingCache<TreeNode, NormalizedNode<?, ?>> cache =
32 CacheBuilder.newBuilder().weakValues().build(this);
34 public BindingNormalizedNodeCache(@Nonnull final AbstractBindingNormalizedNodeCacheHolder cacheHolder,
35 @Nonnull final DataContainerCodecContext<?, ?> subtreeRoot) {
36 this.cacheHolder = requireNonNull(cacheHolder, "cacheHolder");
37 this.subtreeRoot = requireNonNull(subtreeRoot, "subtreeRoot");
41 public NormalizedNode<?, ?> load(@Nonnull final TreeNode key) throws Exception {
42 return CachingNormalizedNodeSerializer.serializeUsingStreamWriter(cacheHolder, subtreeRoot, key);
46 * Returns cached NormalizedNode representation of TreeNode.
49 * If representation is not cached, serializes TreeNode and updates cache
50 * with representation.
53 * - binding object to be deserialized
54 * @return NormalizedNode representation of binding object
56 public NormalizedNode<?, ?> get(final TreeNode obj) {
57 return cache.getUnchecked(obj);