2 * Copyright (c) 2022 PANTHEON.tech, 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.impl;
10 import java.lang.invoke.MethodHandles;
11 import java.lang.invoke.VarHandle;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.binding.runtime.api.ContainerLikeRuntimeType;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
18 * A {@link ContainerLikeCodecContext} specialized for {@code container}s which do not have a presence statement.
20 final class StructuralContainerCodecContext<D extends DataObject> extends ContainerLikeCodecContext<D> {
21 private static final VarHandle EMPTY_OBJECT;
25 EMPTY_OBJECT = MethodHandles.lookup().findVarHandle(StructuralContainerCodecContext.class,
26 "emptyObject", DataObject.class);
27 } catch (NoSuchFieldException | IllegalAccessException e) {
28 throw new ExceptionInInitializerError(e);
32 @SuppressWarnings("unused")
33 private volatile D emptyObject;
35 StructuralContainerCodecContext(final DataContainerCodecPrototype<ContainerLikeRuntimeType<?, ?>> prototype) {
39 @NonNull D emptyObject() {
40 final var local = (D) EMPTY_OBJECT.getAcquire(this);
41 return local != null ? local : loadEmptyObject();
44 private @NonNull D loadEmptyObject() {
45 final var local = createBindingProxy(
46 Builders.containerBuilder().withNodeIdentifier(getDomPathArgument()).build());
47 final var witness = (D) EMPTY_OBJECT.compareAndExchangeRelease(this, null, local);
48 return witness != null ? witness : local;