Cache NodeContextSuppliers in CodecDataObject
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / CodecDataObject.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, 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.impl;
9
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import java.util.Optional;
16 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
23
24 /**
25  * A base class for {@link DataObject}s backed by {@link DataObjectCodecContext}. While this class is public, it not
26  * part of API surface and is an implementation detail. The only reason for it being public is that it needs to be
27  * accessible by code generated at runtime.
28  *
29  * @param <T> DataObject type
30  */
31 public abstract class CodecDataObject<T extends DataObject> implements DataObject {
32     private static final @NonNull Object NULL_VALUE = new Object();
33
34     @SuppressWarnings("rawtypes")
35     private final @NonNull NormalizedNodeContainer data;
36
37     private volatile Integer cachedHashcode = null;
38
39     public CodecDataObject(final NormalizedNodeContainer<?, ?, ?> data) {
40         this.data = requireNonNull(data, "Data must not be null");
41     }
42
43     @Override
44     public final int hashCode() {
45         final Integer cached = cachedHashcode;
46         if (cached != null) {
47             return cached;
48         }
49
50         final int result = codecAugmentedHashCode();
51         cachedHashcode = result;
52         return result;
53     }
54
55     @Override
56     public final boolean equals(final Object obj) {
57         if (obj == this) {
58             return true;
59         }
60         final Class<? extends DataObject> iface = implementedInterface();
61         if (!iface.isInstance(obj)) {
62             return false;
63         }
64         @SuppressWarnings("unchecked")
65         final T other = (T) iface.cast(obj);
66         if (other instanceof CodecDataObject) {
67             return data.equals(((CodecDataObject<?>) obj).data);
68         }
69         return codecAugmentedEquals(other);
70     }
71
72     @Override
73     public final String toString() {
74         return codecAugmentedFillToString(MoreObjects.toStringHelper(implementedInterface()).omitNullValues())
75                 .toString();
76     }
77
78     // TODO: consider switching to VarHandles for Java 9+
79     protected final Object codecMember(final AtomicReferenceFieldUpdater<CodecDataObject<?>, Object> updater,
80             final NodeContextSupplier supplier) {
81         final Object cached = updater.get(this);
82         return cached != null ? unmaskNull(cached) : loadMember(updater, supplier);
83     }
84
85     protected final Object codecMember(final AtomicReferenceFieldUpdater<CodecDataObject<?>, Object> updater,
86             final IdentifiableItemCodec codec) {
87         final Object cached = updater.get(this);
88         return cached != null ? unmaskNull(cached) : loadKey(updater, codec);
89     }
90
91     protected abstract int codecHashCode();
92
93     protected abstract boolean codecEquals(T other);
94
95     protected abstract ToStringHelper codecFillToString(ToStringHelper helper);
96
97     @SuppressWarnings("rawtypes")
98     final @NonNull NormalizedNodeContainer codecData() {
99         return data;
100     }
101
102     // Non-final to allow specialization in AugmentableCodecDataObject
103     int codecAugmentedHashCode() {
104         return codecHashCode();
105     }
106
107     // Non-final to allow specialization in AugmentableCodecDataObject
108     boolean codecAugmentedEquals(final T other) {
109         return codecEquals(other);
110     }
111
112     // Non-final to allow specialization in AugmentableCodecDataObject
113     ToStringHelper codecAugmentedFillToString(final ToStringHelper helper) {
114         return codecFillToString(helper);
115     }
116
117     // Helpers split out of codecMember to aid its inlining
118     private Object loadMember(final AtomicReferenceFieldUpdater<CodecDataObject<?>, Object> updater,
119             final NodeContextSupplier supplier) {
120         final NodeCodecContext context = supplier.get();
121
122         @SuppressWarnings("unchecked")
123         final Optional<NormalizedNode<?, ?>> child = data.getChild(context.getDomPathArgument());
124
125         // We do not want to use Optional.map() here because we do not want to invoke defaultObject() when we have
126         // normal value because defaultObject() may end up throwing an exception intentionally.
127         return updateCache(updater, child.isPresent() ? context.deserializeObject(child.get())
128                 : context.defaultObject());
129     }
130
131     // Helpers split out of codecMember to aid its inlining
132     private Object loadKey(final AtomicReferenceFieldUpdater<CodecDataObject<?>, Object> updater,
133             final IdentifiableItemCodec codec) {
134         verify(data instanceof MapEntryNode, "Unsupported value %s", data);
135         return updateCache(updater, codec.deserialize(((MapEntryNode) data).getIdentifier()).getKey());
136     }
137
138     private Object updateCache(final AtomicReferenceFieldUpdater<CodecDataObject<?>, Object> updater,
139             final Object obj) {
140         return updater.compareAndSet(this, null, maskNull(obj)) ? obj : unmaskNull(updater.get(this));
141     }
142
143     private static @NonNull Object maskNull(final @Nullable Object unmasked) {
144         return unmasked == null ? NULL_VALUE : unmasked;
145     }
146
147     private static @Nullable Object unmaskNull(final Object masked) {
148         return masked == NULL_VALUE ? null : masked;
149     }
150 }