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.verifyNotNull;
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
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.
21 public final class ClassGeneratorBridge {
22 interface BridgeProvider {
26 interface LocalNameProvider extends BridgeProvider {
28 @NonNull String resolveLocalName(@NonNull String methodName);
31 interface NodeContextSupplierProvider extends BridgeProvider {
33 @NonNull NodeContextSupplier resolveNodeContextSupplier(@NonNull String methodName);
36 private static final ThreadLocal<BridgeProvider> CURRENT_CUSTOMIZER = new ThreadLocal<>();
38 private ClassGeneratorBridge() {
42 public static @NonNull NodeContextSupplier resolveNodeContextSupplier(final @NonNull String methodName) {
43 return current(NodeContextSupplierProvider.class).resolveNodeContextSupplier(methodName);
46 public static @NonNull String resolveLocalName(final @NonNull String methodName) {
47 return current(LocalNameProvider.class).resolveLocalName(methodName);
50 static @Nullable BridgeProvider setup(final @NonNull BridgeProvider next) {
51 final BridgeProvider prev = CURRENT_CUSTOMIZER.get();
52 CURRENT_CUSTOMIZER.set(verifyNotNull(next));
56 static void tearDown(final @Nullable BridgeProvider prev) {
58 CURRENT_CUSTOMIZER.remove();
60 CURRENT_CUSTOMIZER.set(prev);
64 private static <T extends BridgeProvider> @NonNull T current(final Class<T> requested) {
65 return requested.cast(verifyNotNull(CURRENT_CUSTOMIZER.get(), "No customizer attached"));