Remove @(Not)ThreadSafe annotation
[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 org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingLazyContainerNode;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27
28 /**
29  * A {@link ContainerNode} backed by a binding {@link DataObject}, with lazy instantiation of the ContainerNode view.
30  * This class is thread-safe.
31  *
32  * @param <T> Binding DataObject type
33  * @author Robert Varga
34  */
35 @Beta
36 public abstract class AbstractBindingLazyContainerNode<T extends DataObject, C> extends ForwardingObject
37         implements BindingLazyContainerNode<T> {
38     private final @NonNull NodeIdentifier identifier;
39     private final @NonNull T bindingData;
40
41     private volatile @Nullable ContainerNode delegate;
42     @GuardedBy("this")
43     private @Nullable C context;
44
45     protected AbstractBindingLazyContainerNode(final @NonNull NodeIdentifier identifier, final @NonNull T bindingData,
46             final C context) {
47         this.identifier = requireNonNull(identifier);
48         this.bindingData = requireNonNull(bindingData);
49         this.context = context;
50     }
51
52     @Override
53     public final @NonNull T getDataObject() {
54         return bindingData;
55     }
56
57     @Override
58     public final @NonNull NodeIdentifier getIdentifier() {
59         return identifier;
60     }
61
62     @Override
63     public final QName getNodeType() {
64         return identifier.getNodeType();
65     }
66
67     @Override
68     public final ContainerNode getDelegate() {
69         return delegate();
70     }
71
72     @Override
73     public Map<QName, String> getAttributes() {
74         return delegate().getAttributes();
75     }
76
77     @Override
78     public Object getAttributeValue(final QName name) {
79         return delegate().getAttributeValue(name);
80     }
81
82     @Override
83     public Collection<DataContainerChild<? extends PathArgument, ?>> getValue() {
84         return delegate().getValue();
85     }
86
87     @Override
88     public Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
89         return delegate().getChild(child);
90     }
91
92     @Override
93     public int hashCode() {
94         return delegate().hashCode();
95     }
96
97     @Override
98     public boolean equals(final @Nullable Object obj) {
99         if (this == obj) {
100             return true;
101         }
102         if (!(obj instanceof ContainerNode)) {
103             return false;
104         }
105         final ContainerNode other = (ContainerNode) obj;
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 }