Deprecate Clustered(DOM)DataTreeChangeListener
[mdsal.git] / trace / mdsal-trace-impl / src / main / java / org / opendaylight / mdsal / trace / impl / CloseTrackedTrait.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. 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.trace.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import org.eclipse.jdt.annotation.Nullable;
14
15 /**
16  * Implementation of {@link CloseTracked} which can be used as a field in another class which implements
17  * {@link CloseTracked} and delegates its methods to this.
18  *
19  * <p>
20  * This is useful if that class already has another parent class. If it does not, then it's typically more convenient to
21  *  just extend AbstractCloseTracked.
22  *
23  * @author Michael Vorburger.ch
24  */
25 final class CloseTrackedTrait<T extends CloseTracked<T>> implements CloseTracked<T> {
26     // NB: It's important that we keep a Throwable here, and not directly the StackTraceElement[] !
27     // This is because creating a new Throwable() is a lot less expensive in terms of runtime overhead
28     // than actually calling its getStackTrace(), which we can delay until we really need to.
29     // see also e.g. https://stackoverflow.com/a/26122232/421602
30     private final @Nullable Throwable allocationContext;
31     private final CloseTrackedRegistry<T> closeTrackedRegistry;
32     private final CloseTracked<T> realCloseTracked;
33
34     CloseTrackedTrait(final CloseTrackedRegistry<T> transactionChainRegistry, final CloseTracked<T> realCloseTracked) {
35         if (transactionChainRegistry.isDebugContextEnabled()) {
36             // NB: We're NOT doing the (expensive) getStackTrace() here just yet (only below)
37             // TODO When we're on Java 9, then instead use the new java.lang.StackWalker API..
38             allocationContext = new Throwable();
39         } else {
40             allocationContext = null;
41         }
42         this.realCloseTracked = requireNonNull(realCloseTracked, "realCloseTracked");
43         closeTrackedRegistry = requireNonNull(transactionChainRegistry, "transactionChainRegistry");
44         closeTrackedRegistry.add(this);
45     }
46
47     @Override
48     @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
49     public @Nullable StackTraceElement[] getAllocationContextStackTrace() {
50         final var local = allocationContext;
51         return local != null ? local.getStackTrace() : null;
52     }
53
54     public void removeFromTrackedRegistry() {
55         closeTrackedRegistry.remove(this);
56     }
57
58     @Override
59     public CloseTracked<T> getRealCloseTracked() {
60         return realCloseTracked;
61     }
62 }