Add BindingLazyContainerNode
[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     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     protected final @NonNull ContainerNode delegate() {
94         ContainerNode local = delegate;
95         if (local == null) {
96             synchronized (this) {
97                 local = delegate;
98                 if (local == null) {
99                     local = delegate = requireNonNull(computeContainerNode(context));
100                     context = null;
101                 }
102             }
103         }
104
105         return local;
106     }
107
108     @GuardedBy("this")
109     protected abstract @NonNull ContainerNode computeContainerNode(C context);
110 }