Adopt yangtools-11.0.0-SNAPSHOT
[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.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;
24
25 /**
26  * A {@link ContainerNode} backed by a binding {@link DataObject}, with lazy instantiation of the ContainerNode view.
27  * This class is thread-safe.
28  *
29  * @param <T> Binding DataObject type
30  * @param <C> Context type
31  * @author Robert Varga
32  */
33 @Beta
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;
38
39     private volatile @Nullable ContainerNode delegate;
40     @GuardedBy("this")
41     private @Nullable C context;
42
43     protected AbstractBindingLazyContainerNode(final @NonNull NodeIdentifier identifier, final @NonNull T bindingData,
44             final C context) {
45         this.identifier = requireNonNull(identifier);
46         this.bindingData = requireNonNull(bindingData);
47         this.context = context;
48     }
49
50     @Override
51     public final @NonNull T getDataObject() {
52         return bindingData;
53     }
54
55     @Override
56     public final @NonNull NodeIdentifier name() {
57         return identifier;
58     }
59
60     @Override
61     @Deprecated(since = "12.0.0", forRemoval = true)
62     public final @NonNull NodeIdentifier getIdentifier() {
63         return identifier;
64     }
65
66
67     @Override
68     public final ContainerNode getDelegate() {
69         return delegate();
70     }
71
72     @Override
73     public Collection<DataContainerChild> body() {
74         return delegate().body();
75     }
76
77     @Override
78     public DataContainerChild childByArg(final NodeIdentifier child) {
79         return delegate().childByArg(child);
80     }
81
82     @Override
83     public PrettyTree prettyTree() {
84         // Do not touch delegate() until we really need to
85         return new PrettyTree() {
86             @Override
87             public void appendTo(final StringBuilder sb, final int depth) {
88                 delegate().prettyTree().appendTo(sb, depth);
89             }
90         };
91     }
92
93     @Override
94     public int hashCode() {
95         return delegate().hashCode();
96     }
97
98     @Override
99     public boolean equals(final @Nullable Object obj) {
100         if (this == obj) {
101             return true;
102         }
103         if (!(obj instanceof ContainerNode other)) {
104             return false;
105         }
106         return delegate().equals(other);
107     }
108
109     @Override
110     protected final @NonNull ContainerNode delegate() {
111         ContainerNode local = delegate;
112         if (local == null) {
113             synchronized (this) {
114                 local = delegate;
115                 if (local == null) {
116                     local = delegate = requireNonNull(computeContainerNode(context));
117                     context = null;
118                 }
119             }
120         }
121
122         return local;
123     }
124
125     @GuardedBy("this")
126     protected abstract @NonNull ContainerNode computeContainerNode(C context);
127 }