2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.util;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ForwardingObject;
14 import java.util.Collection;
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;
29 * A {@link ContainerNode} backed by a binding {@link DataObject}, with lazy instantiation of the ContainerNode view.
30 * This class is thread-safe.
32 * @param <T> Binding DataObject type
33 * @author Robert Varga
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;
41 private volatile @Nullable ContainerNode delegate;
43 private @Nullable C context;
45 protected AbstractBindingLazyContainerNode(final @NonNull NodeIdentifier identifier, final @NonNull T bindingData,
47 this.identifier = requireNonNull(identifier);
48 this.bindingData = requireNonNull(bindingData);
49 this.context = context;
53 public final @NonNull T getDataObject() {
58 public final @NonNull NodeIdentifier getIdentifier() {
63 public final QName getNodeType() {
64 return identifier.getNodeType();
68 public final ContainerNode getDelegate() {
73 public Map<QName, String> getAttributes() {
74 return delegate().getAttributes();
78 public Object getAttributeValue(final QName name) {
79 return delegate().getAttributeValue(name);
83 public Collection<DataContainerChild<? extends PathArgument, ?>> getValue() {
84 return delegate().getValue();
88 public Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
89 return delegate().getChild(child);
93 public int hashCode() {
94 return delegate().hashCode();
98 public boolean equals(final @Nullable Object obj) {
102 if (!(obj instanceof ContainerNode)) {
105 final ContainerNode other = (ContainerNode) obj;
106 return delegate().equals(other);
110 protected final @NonNull ContainerNode delegate() {
111 ContainerNode local = delegate;
113 synchronized (this) {
116 local = delegate = requireNonNull(computeContainerNode(context));
126 protected abstract @NonNull ContainerNode computeContainerNode(C context);