f76eeb17c60136f3587b58ff5ae03ed4b37ec673
[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 java.time.Instant;
11 import java.util.Objects;
12 import javax.annotation.Nullable;
13
14 /**
15  * Implementation of {@link CloseTracked} which can be used as a field in
16  * another class which implements {@link CloseTracked} and delegates its methods
17  * to this.
18  *
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.
21  *
22  * @author Michael Vorburger.ch
23  */
24 public class CloseTrackedTrait<T extends CloseTracked<T>> implements CloseTracked<T> {
25
26     private final Instant created;
27     private final @Nullable Throwable allocationContext;
28     private final CloseTrackedRegistry<T> closeTrackedRegistry;
29
30     public CloseTrackedTrait(CloseTrackedRegistry<T> transactionChainRegistry) {
31         this.created = Instant.now();
32         if (transactionChainRegistry.isDebugContextEnabled()) {
33             this.allocationContext = new Throwable("allocated at");
34         } else {
35             this.allocationContext = null;
36         }
37         this.closeTrackedRegistry = Objects.requireNonNull(transactionChainRegistry, "transactionChainRegistry");
38         this.closeTrackedRegistry.add(this);
39     }
40
41     @Override
42     public Instant getObjectCreated() {
43         return created;
44     }
45
46     @Override
47     public StackTraceElement[] getAllocationContextStackTrace() {
48         return allocationContext != null ? allocationContext.getStackTrace() : null;
49     }
50
51     public void removeFromTrackedRegistry() {
52         closeTrackedRegistry.remove(this);
53     }
54
55 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.