-
Notifications
You must be signed in to change notification settings - Fork 10
/
AbstractTmfGraphProvider.java
153 lines (130 loc) · 4.18 KB
/
AbstractTmfGraphProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
* Copyright (c) 2015, 2024 École Polytechnique de Montréal and others
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.analysis.graph.core.building;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.analysis.graph.core.graph.ITmfGraph;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
/**
* Base class for graph providers. It implements most methods common for all
* graph builder.
*
* @author Geneviève Bastien
* @author Francis Giraldeau
*/
public abstract class AbstractTmfGraphProvider implements ITmfGraphProvider {
private static final Comparator<ITraceEventHandler> HANDLER_COMPARATOR = (@Nullable ITraceEventHandler o1, @Nullable ITraceEventHandler o2) -> {
if (o1 == null) {
return 1;
}
if (o2 == null) {
return -1;
}
int res = Integer.compare(o1.getPriority(), o2.getPriority());
// If the handlers have the same priority, arbitrarily compare their names
return (res != 0 ? res : o1.getClass().getName().compareTo(o2.getClass().getName()));
};
private final ITmfTrace fTrace;
private final List<ITraceEventHandler> fHandlers;
private boolean fGraphAssigned;
/** Graph in which to insert the state changes */
private @Nullable ITmfGraph fGraph = null;
/**
* Instantiate a new graph builder plugin.
*
* @param trace
* The trace
* @param id
* Name given to this state change input. Only used internally.
*/
public AbstractTmfGraphProvider(ITmfTrace trace, String id) {
this.fTrace = trace;
fGraphAssigned = false;
fHandlers = new ArrayList<>();
}
@Override
public ITmfTrace getTrace() {
return fTrace;
}
@Override
public long getStartTime() {
return fTrace.getStartTime().toNanos();
}
@Override
public void assignGraph(@NonNull ITmfGraph graph) {
fGraph = graph;
fGraphAssigned = true;
}
@Override
public @Nullable ITmfGraph getGraph() {
return fGraph;
}
@Override
public void processEvent(ITmfEvent event) {
/* Make sure the target graph has been assigned */
if (!fGraphAssigned) {
return;
}
eventHandle(event);
}
@Override
public void dispose() {
fGraphAssigned = false;
fGraph = null;
}
@Override
public void done() {
}
/**
* Internal event handler, using the phase's handlers
*
* @param event
* The event
*/
protected void eventHandle(ITmfEvent event) {
for (ITraceEventHandler handler : fHandlers) {
handler.handleEvent(event);
}
}
@Override
public void handleCancel() {
}
/**
* Register a handler to a series of events
*
* @param handler
* The trace event handler
*/
public void registerHandler(ITraceEventHandler handler) {
int pos = Collections.binarySearch(fHandlers, handler, HANDLER_COMPARATOR);
if (pos < 0) {
fHandlers.add(-pos - 1, handler);
}
// If pos >= 0, the handler is already in the list
}
/**
* Get the list of handlers for this class. Used for unit testing only.
*
* @return The list of handlers
* @since 1.2
*/
@VisibleForTesting
protected List<ITraceEventHandler> getHandlers() {
return ImmutableList.copyOf(fHandlers);
}
}