Switch NormalizedNode->Binding codegen to ByteBuddy
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / CodecDataObjectBridge.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.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15
16 /**
17  * Bridge for initializing {@link CodecDataObject} instance constants during class loading time. This class is public
18  * only due to implementation restrictions and can change at any time.
19  */
20 @Beta
21 public final class CodecDataObjectBridge {
22     private static final ThreadLocal<CodecDataObjectGenerator> CURRENT_CUSTOMIZER = new ThreadLocal<>();
23
24     private CodecDataObjectBridge() {
25
26     }
27
28     public static @NonNull NodeContextSupplier resolve(final @NonNull String methodName) {
29         return current().resolve(methodName);
30     }
31
32     public static @NonNull IdentifiableItemCodec resolveKey(final @NonNull String methodName) {
33         return current().resolveKey(methodName);
34     }
35
36     static @Nullable CodecDataObjectGenerator setup(final @NonNull CodecDataObjectGenerator next) {
37         final CodecDataObjectGenerator prev = CURRENT_CUSTOMIZER.get();
38         CURRENT_CUSTOMIZER.set(verifyNotNull(next));
39         return prev;
40     }
41
42     static void tearDown(final @Nullable CodecDataObjectGenerator prev) {
43         if (prev == null) {
44             CURRENT_CUSTOMIZER.remove();
45         } else {
46             CURRENT_CUSTOMIZER.set(prev);
47         }
48     }
49
50     private static @NonNull CodecDataObjectGenerator current() {
51         return verifyNotNull(CURRENT_CUSTOMIZER.get(), "No customizer attached");
52     }
53 }