Teach BindingNormalizedNodeCache to cache leaf type objects
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataObjectStreamerBridge.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 DataObjectStreamer} instance constants during class loading time. This class is public
18  * only due to implementation restrictions and can change at any time.
19  */
20 // FIXME: this bridge is only necessary to work around Javassist compiler resolution of dependencies. If we switch to
21 //         a more bytecode-oriented framework, this bridge becomes superfluous.
22 //
23 @Beta
24 public final class DataObjectStreamerBridge {
25     private static final ThreadLocal<DataObjectStreamerCustomizer> CURRENT_CUSTOMIZER = new ThreadLocal<>();
26
27     private DataObjectStreamerBridge() {
28
29     }
30
31     public static @NonNull DataObjectStreamer<?> resolve(final @NonNull String methodName) {
32         return verifyNotNull(CURRENT_CUSTOMIZER.get(), "No customizer attached").resolve(methodName);
33     }
34
35     static @Nullable DataObjectStreamerCustomizer setup(final @NonNull DataObjectStreamerCustomizer next) {
36         final DataObjectStreamerCustomizer prev = CURRENT_CUSTOMIZER.get();
37         CURRENT_CUSTOMIZER.set(verifyNotNull(next));
38         return prev;
39     }
40
41     static void tearDown(final @Nullable DataObjectStreamerCustomizer prev) {
42         if (prev == null) {
43             CURRENT_CUSTOMIZER.remove();
44         } else {
45             CURRENT_CUSTOMIZER.set(prev);
46         }
47     }
48 }