2 * Copyright (c) 2018 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.dom.codec.spi;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ForwardingObject;
13 import java.util.Collection;
14 import org.checkerframework.checker.lock.qual.GuardedBy;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingLazyContainerNode;
18 import org.opendaylight.yangtools.concepts.PrettyTree;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
25 * A {@link ContainerNode} backed by a binding {@link DataObject}, with lazy instantiation of the ContainerNode view.
26 * This class is thread-safe.
28 * @param <T> Binding DataObject type
29 * @param <C> Context type
31 public abstract class AbstractBindingLazyContainerNode<T extends DataObject, C> extends ForwardingObject
32 implements BindingLazyContainerNode<T> {
33 private final @NonNull NodeIdentifier identifier;
34 private final @NonNull T bindingData;
36 private volatile @Nullable ContainerNode delegate;
38 private @Nullable C context;
40 protected AbstractBindingLazyContainerNode(final @NonNull NodeIdentifier identifier, final @NonNull T bindingData,
42 this.identifier = requireNonNull(identifier);
43 this.bindingData = requireNonNull(bindingData);
44 this.context = context;
48 public final T getDataObject() {
53 public final NodeIdentifier name() {
58 public final ContainerNode getDelegate() {
63 public Collection<@NonNull DataContainerChild> body() {
64 return delegate().body();
68 public DataContainerChild childByArg(final NodeIdentifier child) {
69 return delegate().childByArg(child);
73 public PrettyTree prettyTree() {
74 // Do not touch delegate() until we really need to
75 return new PrettyTree() {
77 public void appendTo(final StringBuilder sb, final int depth) {
78 delegate().prettyTree().appendTo(sb, depth);
84 public int hashCode() {
85 return delegate().hashCode();
89 public boolean equals(final Object obj) {
90 return this == obj || obj instanceof ContainerNode other && delegate().equals(other);
94 protected final @NonNull ContainerNode delegate() {
100 delegate = local = requireNonNull(computeContainerNode(context));
109 protected abstract @NonNull ContainerNode computeContainerNode(C context);