2 * Copyright (c) 2019 PANTHEON.tech, 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.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 org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.binding.BindingObject;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 * An abstract cache mapping BindingObject instances to their NormalizedNode counterparts. Note that this mapping is
21 * not one-to-one, as NormalizedNodes work on instantiated trees while BindingObjects are generally reused across
24 * @param <T> BindingObject subtype
25 * @param <C> Root codec context type
27 abstract class AbstractBindingNormalizedNodeCache<T extends BindingObject, C extends NodeCodecContext>
28 extends CacheLoader<T, NormalizedNode<?, ?>> {
29 private final LoadingCache<T, NormalizedNode<?, ?>> cache = CacheBuilder.newBuilder().weakValues().build(this);
31 private final @NonNull C rootContext;
33 AbstractBindingNormalizedNodeCache(final C rootContext) {
34 this.rootContext = requireNonNull(rootContext);
38 * Returns the root codec context associated with this cache.
40 * @return Root codec context
42 final @NonNull C rootContext() {
47 * Returns cached NormalizedNode representation of DataObject. If the representation is not cached, serializes
48 * DataObject and updates cache with representation.
50 * @param obj Binding object to be deserialized
51 * @return NormalizedNode representation of binding object.
53 final NormalizedNode<?, ?> get(final @NonNull T obj) {
54 return cache.getUnchecked(obj);