Deprecate all MD-SAL APIs
[controller.git] / opendaylight / md-sal / mdsal-trace / dom-impl / src / main / java / org / opendaylight / controller / md / sal / trace / closetracker / 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.controller.md.sal.trace.closetracker.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
17  * another class which implements {@link CloseTracked} and delegates its methods
18  * to this.
19  *
20  * <p>This is useful if that class already has another parent class.
21  * If it does not, then it's typically more convenient to just extend AbstractCloseTracked.
22  *
23  * @author Michael Vorburger.ch
24  */
25 @Deprecated
26 public class CloseTrackedTrait<T extends CloseTracked<T>> implements CloseTracked<T> {
27
28     // NB: It's important that we keep a Throwable here, and not directly the StackTraceElement[] !
29     // This is because creating a new Throwable() is a lot less expensive in terms of runtime overhead
30     // than actually calling its getStackTrace(), which we can delay until we really need to.
31     // see also e.g. https://stackoverflow.com/a/26122232/421602
32     private final @Nullable Throwable allocationContext;
33     private final CloseTrackedRegistry<T> closeTrackedRegistry;
34     private final CloseTracked<T> realCloseTracked;
35
36     @SuppressFBWarnings(value = "NP_STORE_INTO_NONNULL_FIELD", justification = "SpotBugs and JDT annotations")
37     public CloseTrackedTrait(CloseTrackedRegistry<T> transactionChainRegistry, CloseTracked<T> realCloseTracked) {
38         if (transactionChainRegistry.isDebugContextEnabled()) {
39             // NB: We're NOT doing the (expensive) getStackTrace() here just yet (only below)
40             // TODO When we're on Java 9, then instead use the new java.lang.StackWalker API..
41             this.allocationContext = new Throwable();
42         } else {
43             this.allocationContext = null;
44         }
45         this.realCloseTracked = requireNonNull(realCloseTracked, "realCloseTracked");
46         this.closeTrackedRegistry = requireNonNull(transactionChainRegistry, "transactionChainRegistry");
47         this.closeTrackedRegistry.add(this);
48     }
49
50     @Override
51     @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
52     public StackTraceElement[] getAllocationContextStackTrace() {
53         return allocationContext != null ? allocationContext.getStackTrace() : null;
54     }
55
56     public void removeFromTrackedRegistry() {
57         closeTrackedRegistry.remove(this);
58     }
59
60     @Override
61     public CloseTracked<T> getRealCloseTracked() {
62         return realCloseTracked;
63     }
64
65 }