2 * Copyright (c) 2017 Red Hat, Inc. 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.controller.md.sal.trace.closetracker.impl;
10 import java.util.Objects;
11 import javax.annotation.Nullable;
14 * Implementation of {@link CloseTracked} which can be used as a field in
15 * another class which implements {@link CloseTracked} and delegates its methods
18 * <p>This is useful if that class already has another parent class.
19 * If it does not, then it's typically more convenient to just extend AbstractCloseTracked.
21 * @author Michael Vorburger.ch
23 public class CloseTrackedTrait<T extends CloseTracked<T>> implements CloseTracked<T> {
25 private final @Nullable Throwable allocationContext;
26 private final CloseTrackedRegistry<T> closeTrackedRegistry;
27 private final CloseTracked<T> realCloseTracked;
29 public CloseTrackedTrait(CloseTrackedRegistry<T> transactionChainRegistry, CloseTracked<T> realCloseTracked) {
30 if (transactionChainRegistry.isDebugContextEnabled()) {
31 this.allocationContext = new Throwable("allocated at");
33 this.allocationContext = null;
35 this.realCloseTracked = Objects.requireNonNull(realCloseTracked, "realCloseTracked");
36 this.closeTrackedRegistry = Objects.requireNonNull(transactionChainRegistry, "transactionChainRegistry");
37 this.closeTrackedRegistry.add(this);
41 public StackTraceElement[] getAllocationContextStackTrace() {
42 return allocationContext != null ? allocationContext.getStackTrace() : null;
45 public void removeFromTrackedRegistry() {
46 closeTrackedRegistry.remove(this);
50 public CloseTracked<T> getRealCloseTracked() {
51 return realCloseTracked;