Do not inline NodeContextSuppliers
[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 import org.opendaylight.mdsal.binding.dom.codec.impl.CodecDataObjectGenerator.Fixed;
16
17 /**
18  * Bridge for initializing {@link CodecDataObject} instance constants during class loading time. This class is public
19  * only due to implementation restrictions and can change at any time.
20  */
21 @Beta
22 public final class CodecDataObjectBridge {
23     private static final ThreadLocal<Fixed<?>> CURRENT_CUSTOMIZER = new ThreadLocal<>();
24
25     private CodecDataObjectBridge() {
26
27     }
28
29     public static @NonNull NodeContextSupplier resolve(final @NonNull String methodName) {
30         return current().resolve(methodName);
31     }
32
33     static @Nullable Fixed<?> setup(final @NonNull Fixed<?> next) {
34         final Fixed<?> prev = CURRENT_CUSTOMIZER.get();
35         CURRENT_CUSTOMIZER.set(verifyNotNull(next));
36         return prev;
37     }
38
39     static void tearDown(final @Nullable Fixed<?> prev) {
40         if (prev == null) {
41             CURRENT_CUSTOMIZER.remove();
42         } else {
43             CURRENT_CUSTOMIZER.set(prev);
44         }
45     }
46
47     private static @NonNull Fixed<?> current() {
48         return verifyNotNull(CURRENT_CUSTOMIZER.get(), "No customizer attached");
49     }
50 }