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.annotations.Beta;
13 import com.google.common.collect.ForwardingObject;
14 import java.util.Collection;
15 import org.checkerframework.checker.lock.qual.GuardedBy;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingLazyContainerNode;
19 import org.opendaylight.yangtools.concepts.PrettyTree;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
26 * A {@link ContainerNode} backed by a binding {@link DataObject}, with lazy instantiation of the ContainerNode view.
27 * This class is thread-safe.
29 * @param <T> Binding DataObject type
30 * @param <C> Context type
31 * @author Robert Varga
34 public abstract class AbstractBindingLazyContainerNode<T extends DataObject, C> extends ForwardingObject
35 implements BindingLazyContainerNode<T> {
36 private final @NonNull NodeIdentifier identifier;
37 private final @NonNull T bindingData;
39 private volatile @Nullable ContainerNode delegate;
41 private @Nullable C context;
43 protected AbstractBindingLazyContainerNode(final @NonNull NodeIdentifier identifier, final @NonNull T bindingData,
45 this.identifier = requireNonNull(identifier);
46 this.bindingData = requireNonNull(bindingData);
47 this.context = context;
51 public final @NonNull T getDataObject() {
56 public final @NonNull NodeIdentifier name() {
61 @Deprecated(since = "12.0.0", forRemoval = true)
62 public final @NonNull NodeIdentifier getIdentifier() {
68 public final ContainerNode getDelegate() {
73 public Collection<DataContainerChild> body() {
74 return delegate().body();
78 public DataContainerChild childByArg(final NodeIdentifier child) {
79 return delegate().childByArg(child);
83 public PrettyTree prettyTree() {
84 // Do not touch delegate() until we really need to
85 return new PrettyTree() {
87 public void appendTo(final StringBuilder sb, final int depth) {
88 delegate().prettyTree().appendTo(sb, depth);
94 public int hashCode() {
95 return delegate().hashCode();
99 public boolean equals(final @Nullable Object obj) {
103 if (!(obj instanceof ContainerNode other)) {
106 return delegate().equals(other);
110 protected final @NonNull ContainerNode delegate() {
111 ContainerNode local = delegate;
113 synchronized (this) {
116 local = delegate = requireNonNull(computeContainerNode(context));
126 protected abstract @NonNull ContainerNode computeContainerNode(C context);