Bind CodecDataObject string instances
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / ClassGeneratorBridge.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 generated instance constants during class loading time. This class is public only due to
18  * implementation restrictions and can change at any time.
19  */
20 @Beta
21 public final class ClassGeneratorBridge {
22     interface BridgeProvider {
23
24     }
25
26     interface LocalNameProvider extends BridgeProvider {
27
28         @NonNull String resolveLocalName(@NonNull String methodName);
29     }
30
31     interface NodeContextSupplierProvider extends BridgeProvider {
32
33         @NonNull NodeContextSupplier resolveNodeContextSupplier(@NonNull String methodName);
34     }
35
36     private static final ThreadLocal<BridgeProvider> CURRENT_CUSTOMIZER = new ThreadLocal<>();
37
38     private ClassGeneratorBridge() {
39
40     }
41
42     public static @NonNull NodeContextSupplier resolveNodeContextSupplier(final @NonNull String methodName) {
43         return current(NodeContextSupplierProvider.class).resolveNodeContextSupplier(methodName);
44     }
45
46     public static @NonNull String resolveLocalName(final @NonNull String methodName) {
47         return current(LocalNameProvider.class).resolveLocalName(methodName);
48     }
49
50     static @Nullable BridgeProvider setup(final @NonNull BridgeProvider next) {
51         final BridgeProvider prev = CURRENT_CUSTOMIZER.get();
52         CURRENT_CUSTOMIZER.set(verifyNotNull(next));
53         return prev;
54     }
55
56     static void tearDown(final @Nullable BridgeProvider prev) {
57         if (prev == null) {
58             CURRENT_CUSTOMIZER.remove();
59         } else {
60             CURRENT_CUSTOMIZER.set(prev);
61         }
62     }
63
64     private static <T extends BridgeProvider> @NonNull T current(final Class<T> requested) {
65         return requested.cast(verifyNotNull(CURRENT_CUSTOMIZER.get(), "No customizer attached"));
66     }
67 }