Bump yangtools to 13.0.0
[mdsal.git] / binding / mdsal-binding-dom-codec-spi / src / main / java / org / opendaylight / mdsal / binding / dom / codec / spi / AbstractBindingLazyContainerNode.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.spi;
9
10 import static java.util.Objects.requireNonNull;
11
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;
23
24 /**
25  * A {@link ContainerNode} backed by a binding {@link DataObject}, with lazy instantiation of the ContainerNode view.
26  * This class is thread-safe.
27  *
28  * @param <T> Binding DataObject type
29  * @param <C> Context type
30  */
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;
35
36     private volatile @Nullable ContainerNode delegate;
37     @GuardedBy("this")
38     private @Nullable C context;
39
40     protected AbstractBindingLazyContainerNode(final @NonNull NodeIdentifier identifier, final @NonNull T bindingData,
41             final C context) {
42         this.identifier = requireNonNull(identifier);
43         this.bindingData = requireNonNull(bindingData);
44         this.context = context;
45     }
46
47     @Override
48     public final T getDataObject() {
49         return bindingData;
50     }
51
52     @Override
53     public final NodeIdentifier name() {
54         return identifier;
55     }
56
57     @Override
58     public final ContainerNode getDelegate() {
59         return delegate();
60     }
61
62     @Override
63     public Collection<@NonNull DataContainerChild> body() {
64         return delegate().body();
65     }
66
67     @Override
68     public DataContainerChild childByArg(final NodeIdentifier child) {
69         return delegate().childByArg(child);
70     }
71
72     @Override
73     public PrettyTree prettyTree() {
74         // Do not touch delegate() until we really need to
75         return new PrettyTree() {
76             @Override
77             public void appendTo(final StringBuilder sb, final int depth) {
78                 delegate().prettyTree().appendTo(sb, depth);
79             }
80         };
81     }
82
83     @Override
84     public int hashCode() {
85         return delegate().hashCode();
86     }
87
88     @Override
89     public boolean equals(final Object obj) {
90         return this == obj || obj instanceof ContainerNode other && delegate().equals(other);
91     }
92
93     @Override
94     protected final @NonNull ContainerNode delegate() {
95         var local = delegate;
96         if (local == null) {
97             synchronized (this) {
98                 local = delegate;
99                 if (local == null) {
100                     delegate = local = requireNonNull(computeContainerNode(context));
101                     context = null;
102                 }
103             }
104         }
105         return local;
106     }
107
108     @GuardedBy("this")
109     protected abstract @NonNull ContainerNode computeContainerNode(C context);
110 }