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.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 * A {@link ContainerNode} backed by a binding {@link DataObject}, with lazy instantiation of the ContainerNode view.
28 * This class is thread-safe.
30 * @param <T> Binding DataObject type
31 * @param <C> Context type
32 * @author Robert Varga
35 public abstract class AbstractBindingLazyContainerNode<T extends DataObject, C> extends ForwardingObject
36 implements BindingLazyContainerNode<T> {
37 private final @NonNull NodeIdentifier identifier;
38 private final @NonNull T bindingData;
40 private volatile @Nullable ContainerNode delegate;
42 private @Nullable C context;
44 protected AbstractBindingLazyContainerNode(final @NonNull NodeIdentifier identifier, final @NonNull T bindingData,
46 this.identifier = requireNonNull(identifier);
47 this.bindingData = requireNonNull(bindingData);
48 this.context = context;
52 public final @NonNull T getDataObject() {
57 public final @NonNull NodeIdentifier getIdentifier() {
62 public final ContainerNode getDelegate() {
67 public Collection<DataContainerChild> body() {
68 return delegate().body();
72 public DataContainerChild childByArg(final PathArgument child) {
73 return delegate().childByArg(child);
77 public PrettyTree prettyTree() {
78 // Do not touch delegate() until we really need to
79 return new PrettyTree() {
81 public void appendTo(final StringBuilder sb, final int depth) {
82 delegate().prettyTree().appendTo(sb, depth);
88 public int hashCode() {
89 return delegate().hashCode();
93 public boolean equals(final @Nullable Object obj) {
97 if (!(obj instanceof ContainerNode)) {
100 final ContainerNode other = (ContainerNode) obj;
101 return delegate().equals(other);
105 protected final @NonNull ContainerNode delegate() {
106 ContainerNode local = delegate;
108 synchronized (this) {
111 local = delegate = requireNonNull(computeContainerNode(context));
121 protected abstract @NonNull ContainerNode computeContainerNode(C context);