2 * Copyright (c) 2019 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 static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.VarHandle;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 * A base class for {@link DataObject}s backed by {@link DataObjectCodecContext}. While this class is public, it not
24 * part of API surface and is an implementation detail. The only reason for it being public is that it needs to be
25 * accessible by code generated at runtime.
27 * @param <T> DataObject type
29 public abstract class CodecDataObject<T extends DataObject> implements DataObject {
30 // An object representing a null value in a member field.
31 private static final @NonNull Object NULL_VALUE = new Object();
33 private static final VarHandle CACHED_HASH_CODE;
37 CACHED_HASH_CODE = MethodHandles.lookup().findVarHandle(CodecDataObject.class, "cachedHashcode",
39 } catch (NoSuchFieldException | IllegalAccessException e) {
40 throw new ExceptionInInitializerError(e);
44 private final @NonNull DataObjectCodecContext<T, ?> context;
45 @SuppressWarnings("rawtypes")
46 private final @NonNull DistinctNodeContainer data;
48 // Accessed via a VarHandle
49 @SuppressWarnings("unused")
50 // FIXME: consider using a primitive int-based cache (with 0 being uninit)
51 private volatile Integer cachedHashcode;
53 protected CodecDataObject(final DataObjectCodecContext<T, ?> context, final DistinctNodeContainer<?, ?> data) {
54 this.data = requireNonNull(data, "Data must not be null");
55 this.context = requireNonNull(context, "Context must not be null");
59 public final int hashCode() {
60 final Integer cached = (Integer) CACHED_HASH_CODE.getAcquire(this);
61 return cached != null ? cached : loadHashCode();
65 public final boolean equals(final Object obj) {
66 // Indirection to keep checkstyle happy
67 return codecEquals(obj);
71 public abstract String toString();
73 protected final Object codecMember(final VarHandle handle, final String localName) {
74 final Object cached = handle.getAcquire(this);
75 return cached != null ? unmaskNull(cached) : loadMember(handle, context.getLeafChild(localName));
78 protected final Object codecMember(final VarHandle handle, final Class<? extends DataObject> bindingClass) {
79 final Object cached = handle.getAcquire(this);
80 return cached != null ? unmaskNull(cached) : loadMember(handle, context.streamChild(bindingClass));
83 protected final Object codecMember(final VarHandle handle, final NodeContextSupplier supplier) {
84 final Object cached = handle.getAcquire(this);
85 return cached != null ? unmaskNull(cached) : loadMember(handle, supplier.get());
88 protected final @NonNull Object codecKey(final VarHandle handle) {
89 final Object cached = handle.getAcquire(this);
90 return cached != null ? cached : loadKey(handle);
93 protected abstract int codecHashCode();
95 protected abstract boolean codecEquals(Object obj);
97 final @NonNull DataObjectCodecContext<T, ?> codecContext() {
101 @SuppressWarnings("rawtypes")
102 final @NonNull DistinctNodeContainer codecData() {
106 // Helper split out of codecMember to aid its inlining
107 private Object loadMember(final VarHandle handle, final NodeCodecContext childCtx) {
108 @SuppressWarnings("unchecked")
109 final NormalizedNode child = data.childByArg(childCtx.getDomPathArgument());
111 // We do not want to use Optional.map() here because we do not want to invoke defaultObject() when we have
112 // normal value because defaultObject() may end up throwing an exception intentionally.
113 final Object obj = child != null ? childCtx.deserializeObject(child) : childCtx.defaultObject();
114 final Object witness = handle.compareAndExchangeRelease(this, null, maskNull(obj));
115 return witness == null ? obj : unmaskNull(witness);
118 // Helper split out of codecKey to aid its inlining
119 private @NonNull Object loadKey(final VarHandle handle) {
120 verify(data instanceof MapEntryNode, "Unsupported value %s", data);
121 verify(context instanceof KeyedListNodeCodecContext, "Unexpected context %s", context);
122 final Object obj = ((KeyedListNodeCodecContext<?, ?>) context)
123 .deserialize(((MapEntryNode) data).getIdentifier());
124 // key is known to be non-null, no need to mask it
125 final Object witness = handle.compareAndExchangeRelease(this, null, obj);
126 return witness == null ? obj : witness;
129 // Helper split out of hashCode() to aid its inlining
130 private int loadHashCode() {
131 final int result = codecHashCode();
132 final Object witness = CACHED_HASH_CODE.compareAndExchangeRelease(this, null, result);
133 return witness == null ? result : (Integer) witness;
136 private static @NonNull Object maskNull(final @Nullable Object unmasked) {
137 return unmasked == null ? NULL_VALUE : unmasked;
140 private static @Nullable Object unmaskNull(final Object masked) {
141 return masked == NULL_VALUE ? null : masked;