cc65a621bc4a769ee47591650734dfce43766653
[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     @Deprecated(since = "12.0.0", forRemoval = true)
59     public final NodeIdentifier getIdentifier() {
60         return identifier;
61     }
62
63     @Override
64     public final ContainerNode getDelegate() {
65         return delegate();
66     }
67
68     @Override
69     public Collection<@NonNull DataContainerChild> body() {
70         return delegate().body();
71     }
72
73     @Override
74     public DataContainerChild childByArg(final NodeIdentifier child) {
75         return delegate().childByArg(child);
76     }
77
78     @Override
79     public PrettyTree prettyTree() {
80         // Do not touch delegate() until we really need to
81         return new PrettyTree() {
82             @Override
83             public void appendTo(final StringBuilder sb, final int depth) {
84                 delegate().prettyTree().appendTo(sb, depth);
85             }
86         };
87     }
88
89     @Override
90     public int hashCode() {
91         return delegate().hashCode();
92     }
93
94     @Override
95     public boolean equals(final Object obj) {
96         return this == obj || obj instanceof ContainerNode other && delegate().equals(other);
97     }
98
99     @Override
100     protected final @NonNull ContainerNode delegate() {
101         var local = delegate;
102         if (local == null) {
103             synchronized (this) {
104                 local = delegate;
105                 if (local == null) {
106                     delegate = local = requireNonNull(computeContainerNode(context));
107                     context = null;
108                 }
109             }
110         }
111         return local;
112     }
113
114     @GuardedBy("this")
115     protected abstract @NonNull ContainerNode computeContainerNode(C context);
116 }