Remove @(Not)ThreadSafe annotation
[mdsal.git] / trace / mdsal-trace-impl / src / main / java / org / opendaylight / mdsal / trace / impl / CloseTrackedRegistry.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.Arrays.asList;
11 import static java.util.Collections.emptyList;
12
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.Comparator;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentSkipListSet;
23
24 /**
25  * Registry of {@link CloseTracked} instances. This class is thread-safe.
26  *
27  * @author Michael Vorburger.ch
28  */
29 class CloseTrackedRegistry<T extends CloseTracked<T>> {
30
31     private final Object anchor;
32     private final String createDescription;
33
34     private final Set<CloseTracked<T>> tracked =
35         new ConcurrentSkipListSet<>(Comparator.comparingInt(System::identityHashCode));
36
37     private final boolean isDebugContextEnabled;
38
39     /**
40      * Constructor.
41      *
42      * @param anchor
43      *            object where this registry is stored in, used for human output in
44      *            logging and other output
45      * @param createDescription
46      *            description of creator of instances of this registry, typically
47      *            e.g. name of method in the anchor class
48      * @param isDebugContextEnabled
49      *            whether or not the call stack should be preserved; this is (of
50      *            course) an expensive operation, and should only be used during
51      *            troubleshooting
52      */
53     public CloseTrackedRegistry(final Object anchor, final String createDescription,
54             final boolean isDebugContextEnabled) {
55         this.anchor = anchor;
56         this.createDescription = createDescription;
57         this.isDebugContextEnabled = isDebugContextEnabled;
58     }
59
60     public boolean isDebugContextEnabled() {
61         return isDebugContextEnabled;
62     }
63
64     public Object getAnchor() {
65         return anchor;
66     }
67
68     public String getCreateDescription() {
69         return createDescription;
70     }
71
72     // package protected, not public; only CloseTrackedTrait invokes this
73     void add(final CloseTracked<T> closeTracked) {
74         tracked.add(closeTracked);
75     }
76
77     // package protected, not public; only CloseTrackedTrait invokes this
78     void remove(final CloseTracked<T> closeTracked) {
79         tracked.remove(closeTracked);
80     }
81
82     /**
83      * Creates and returns a "report" of (currently) tracked but not (yet) closed
84      * instances.
85      *
86      * @return Set of CloseTrackedRegistryReportEntry, of which each the stack trace
87      *         element identifies a unique allocation context (or an empty List if
88      *         debugContextEnabled is false), and value is the number of open
89      *         instances created at that place in the code.
90      */
91     // For some reason, FB sees 'map' as useless but it clearly isn't.
92     @SuppressFBWarnings("UC_USELESS_OBJECT")
93     public Set<CloseTrackedRegistryReportEntry<T>> getAllUnique() {
94         Map<List<StackTraceElement>, Long> map = new HashMap<>();
95         Set<CloseTracked<T>> copyOfTracked = new HashSet<>(tracked);
96         for (CloseTracked<T> closeTracked : copyOfTracked) {
97             final StackTraceElement[] stackTraceArray = closeTracked.getAllocationContextStackTrace();
98             List<StackTraceElement> stackTraceElements =
99                     stackTraceArray != null ? Arrays.asList(stackTraceArray) : Collections.emptyList();
100             map.merge(stackTraceElements, 1L, (oldValue, value) -> oldValue + 1);
101         }
102
103         Set<CloseTrackedRegistryReportEntry<T>> report = new HashSet<>();
104         map.forEach((stackTraceElements, number) -> copyOfTracked.stream().filter(closeTracked -> {
105             StackTraceElement[] closeTrackedStackTraceArray = closeTracked.getAllocationContextStackTrace();
106             List<StackTraceElement> closeTrackedStackTraceElements =
107                 closeTrackedStackTraceArray != null ? asList(closeTrackedStackTraceArray) : emptyList();
108             return closeTrackedStackTraceElements.equals(stackTraceElements);
109         }).findAny().ifPresent(exampleCloseTracked -> report.add(
110             new CloseTrackedRegistryReportEntry<>(exampleCloseTracked, number, stackTraceElements))));
111         return report;
112     }
113 }