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.time.Instant;
11 import java.util.Objects;
12 import javax.annotation.Nullable;
15 * Implementation of {@link CloseTracked} which can be used as a field in
16 * another class which implements {@link CloseTracked} and delegates its methods
19 * <p>This is useful if that class already has another parent class.
20 * If it does not, then it's typically more convenient to just extend AbstractCloseTracked.
22 * @author Michael Vorburger.ch
24 public class CloseTrackedTrait<T extends CloseTracked<T>> implements CloseTracked<T> {
26 private final Instant created;
27 private final @Nullable Throwable allocationContext;
28 private final CloseTrackedRegistry<T> closeTrackedRegistry;
30 public CloseTrackedTrait(CloseTrackedRegistry<T> transactionChainRegistry) {
31 this.created = Instant.now();
32 if (transactionChainRegistry.isDebugContextEnabled()) {
33 this.allocationContext = new Throwable("allocated at");
35 this.allocationContext = null;
37 this.closeTrackedRegistry = Objects.requireNonNull(transactionChainRegistry, "transactionChainRegistry");
38 this.closeTrackedRegistry.add(this);
42 public Instant getObjectCreated() {
47 public Throwable getAllocationContext() {
48 return allocationContext;
51 public void removeFromTrackedRegistry() {
52 closeTrackedRegistry.remove(this);