Merge branch 'mdsal-trace' from controller
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / util / 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.util;
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 java.util.Map;
16 import java.util.Optional;
17 import javax.annotation.concurrent.GuardedBy;
18 import javax.annotation.concurrent.ThreadSafe;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingLazyContainerNode;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28
29 /**
30  * A {@link ContainerNode} backed by a binding {@link DataObject}, with lazy instantiation of the ContainerNode view.
31  *
32  * @param <T> Binding DataObject type
33  * @author Robert Varga
34  */
35 @Beta
36 @ThreadSafe
37 public abstract class AbstractBindingLazyContainerNode<T extends DataObject, C> extends ForwardingObject
38         implements BindingLazyContainerNode<T> {
39     private final @NonNull NodeIdentifier identifier;
40     private final @NonNull T bindingData;
41
42     private volatile @Nullable ContainerNode delegate;
43     @GuardedBy("this")
44     private @Nullable C context;
45
46     protected AbstractBindingLazyContainerNode(final @NonNull NodeIdentifier identifier, final @NonNull T bindingData,
47             final C context) {
48         this.identifier = requireNonNull(identifier);
49         this.bindingData = requireNonNull(bindingData);
50         this.context = context;
51     }
52
53     @Override
54     public final @NonNull T getDataObject() {
55         return bindingData;
56     }
57
58     @Override
59     public final @NonNull NodeIdentifier getIdentifier() {
60         return identifier;
61     }
62
63     @Override
64     public final QName getNodeType() {
65         return identifier.getNodeType();
66     }
67
68     @Override
69     public final ContainerNode getDelegate() {
70         return delegate();
71     }
72
73     @Override
74     public Map<QName, String> getAttributes() {
75         return delegate().getAttributes();
76     }
77
78     @Override
79     public Object getAttributeValue(final QName name) {
80         return delegate().getAttributeValue(name);
81     }
82
83     @Override
84     public Collection<DataContainerChild<? extends PathArgument, ?>> getValue() {
85         return delegate().getValue();
86     }
87
88     @Override
89     public Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
90         return delegate().getChild(child);
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)) {
104             return false;
105         }
106         final ContainerNode other = (ContainerNode) obj;
107         return delegate().equals(obj);
108     }
109
110     @Override
111     protected final @NonNull ContainerNode delegate() {
112         ContainerNode local = delegate;
113         if (local == null) {
114             synchronized (this) {
115                 local = delegate;
116                 if (local == null) {
117                     local = delegate = requireNonNull(computeContainerNode(context));
118                     context = null;
119                 }
120             }
121         }
122
123         return local;
124     }
125
126     @GuardedBy("this")
127     protected abstract @NonNull ContainerNode computeContainerNode(C context);
128 }