-
Notifications
You must be signed in to change notification settings - Fork 10
/
TmfVertex.java
338 lines (305 loc) · 9.93 KB
/
TmfVertex.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*******************************************************************************
* 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
*
* Contributors:
* Francis Giraldeau - Initial implementation and API
* Geneviève Bastien - Initial implementation and API
*******************************************************************************/
package org.eclipse.tracecompass.internal.analysis.graph.core.base;
import java.util.Comparator;
import java.util.Objects;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.analysis.graph.core.graph.ITmfVertex;
import org.eclipse.tracecompass.internal.analysis.graph.core.base.TmfEdge.EdgeType;
/**
* Timed vertex for TmfGraph
*
* @author Francis Giraldeau
* @author Geneviève Bastien
*/
public class TmfVertex implements ITmfVertex {
private static final String UNKNOWN_EDGE_DIRECTION_TYPE = "Unknown edge direction type : "; //$NON-NLS-1$
private static long count = 0;
/**
* Describe the four edges coming in and out of a vertex
*/
public enum EdgeDirection {
/**
* Constant for the outgoing vertical edge (to other object)
*/
OUTGOING_VERTICAL_EDGE,
/**
* Constant for the incoming vertical edge (from other object)
*/
INCOMING_VERTICAL_EDGE,
/**
* Constant for the outgoing horizontal edge (to same object)
*/
OUTGOING_HORIZONTAL_EDGE,
/**
* Constant for the incoming horizontal edge (from same object)
*/
INCOMING_HORIZONTAL_EDGE
}
/**
* Compare vertices by ascending timestamps
*/
public static Comparator<TmfVertex> ascending = Objects.requireNonNull(Comparator.nullsLast(Comparator.comparing(TmfVertex::getTs)));
/**
* Compare vertices by descending timestamps
*/
public static Comparator<TmfVertex> descending = Objects.requireNonNull(Comparator.nullsLast(Comparator.comparing(TmfVertex::getTs).reversed()));
private @Nullable TmfEdge fOutgoingVertical = null;
private @Nullable TmfEdge fIncomingVertical = null;
private @Nullable TmfEdge fOutgoingHorizontal = null;
private @Nullable TmfEdge fIncomingHorizontal = null;
private final long fTimestamp;
private final long fId;
/**
* Default Constructor
*/
public TmfVertex() {
this(0);
}
/**
* Constructor with timestamp
*
* @param ts
* The vertex's timestamp
*/
public TmfVertex(final long ts) {
fTimestamp = ts;
synchronized (TmfVertex.class) {
fId = count++;
}
}
/**
* Copy constructor. Keeps same timestamp, but does not keep edges
*
* @param node
* vertex to copy
*/
public TmfVertex(TmfVertex node) {
this(node.fTimestamp);
}
/**
* Copy constructor, but changes the timestamp
*
* @param node
* vertex to copy
* @param ts
* The timestamp of this new node
*/
public TmfVertex(TmfVertex node, final long ts) {
fTimestamp = ts;
synchronized (TmfVertex.class) {
fId = count++;
}
fOutgoingVertical = node.fOutgoingVertical;
fIncomingVertical = node.fIncomingVertical;
fOutgoingHorizontal = node.fOutgoingHorizontal;
fIncomingHorizontal = node.fIncomingHorizontal;
}
/*
* Getters and setters
*/
/**
* Returns the timestamps of this node
*
* @return the timstamp
*/
public long getTs() {
return fTimestamp;
}
@Override
public long getTimestamp() {
return fTimestamp;
}
/**
* Returns the unique ID of this node
*
* @return the vertex's id
*/
public long getID() {
return fId;
}
/**
* Adds an horizontal edge from the current vertex to the 'to' vertex
*
* @param to
* The vertex to link to, belongs to the same object
*
* @return The new edge
*/
public TmfEdge linkHorizontal(TmfVertex to) {
checkTimestamps(to);
checkNotSelf(to);
return linkHorizontalRaw(to);
}
/**
* Adds an horizontal edge from the current vertex to the 'to' vertex
*
* @param to
* The vertex to link to, belongs to the same object
* @param type
* The type of the link to add
* @param linkQualifier
* An optional qualifier to identify this link
*
* @return The new edge
* @since 2.1
*/
public TmfEdge linkHorizontal(TmfVertex to, EdgeType type, @Nullable String linkQualifier) {
checkTimestamps(to);
checkNotSelf(to);
TmfEdge link = linkHorizontalRaw(to);
link.setType(type, linkQualifier);
return link;
}
private TmfEdge linkHorizontalRaw(TmfVertex node) {
TmfEdge link = new TmfEdge(this, node);
fOutgoingHorizontal = link;
node.fIncomingHorizontal = link;
return link;
}
/**
* Adds a vertical edge from the current vertex to the 'to' vertex
*
* @param to
* The vertex to link to, belongs to a different object
* @return The new edge
*/
public TmfEdge linkVertical(TmfVertex to) {
checkTimestamps(to);
checkNotSelf(to);
return linkVerticalRaw(to);
}
/**
* Adds a vertical edge from the current vertex to the 'to' vertex
*
* @param to
* The vertex to link to, belongs to a different object
* @param type
* The type of the link to add
* @param linkQualifier
* An optional qualifier to identify this link
* @return The new edge
* @since 2.1
*/
public TmfEdge linkVertical(TmfVertex to, EdgeType type, @Nullable String linkQualifier) {
checkTimestamps(to);
checkNotSelf(to);
TmfEdge link = linkVerticalRaw(to);
link.setType(type, linkQualifier);
return link;
}
private TmfEdge linkVerticalRaw(TmfVertex to) {
TmfEdge link = new TmfEdge(this, to);
fOutgoingVertical = link;
to.fIncomingVertical = link;
return link;
}
private void checkTimestamps(TmfVertex to) {
if (this.fTimestamp > to.fTimestamp) {
throw new IllegalArgumentException(Messages.TmfVertex_ArgumentTimestampLower +
String.format(": (curr=%d,next=%d,elapsed=%d)", fTimestamp, to.fTimestamp, to.fTimestamp - fTimestamp)); //$NON-NLS-1$
}
}
private void checkNotSelf(TmfVertex to) {
if (this == to) {
throw new IllegalArgumentException(Messages.TmfVertex_CannotLinkToSelf);
}
}
/**
* Get an edge to or from this vertex in the appropriate direction
*
* @param dir
* The direction of the requested edge
* @return The edge from this vertex to the requested direction
*/
public @Nullable TmfEdge getEdge(EdgeDirection dir) {
switch (dir) {
case OUTGOING_VERTICAL_EDGE:
return fOutgoingVertical;
case INCOMING_VERTICAL_EDGE:
return fIncomingVertical;
case OUTGOING_HORIZONTAL_EDGE:
return fOutgoingHorizontal;
case INCOMING_HORIZONTAL_EDGE:
return fIncomingHorizontal;
default:
throw new IllegalStateException(UNKNOWN_EDGE_DIRECTION_TYPE + dir);
}
}
/**
* Removes a directed edge from this vertex. The edge in that direction will
* be null.
*
* @param dir
* The direction to remove the edge from
*/
public void removeEdge(EdgeDirection dir) {
switch (dir) {
case OUTGOING_VERTICAL_EDGE:
fOutgoingVertical = null;
break;
case INCOMING_VERTICAL_EDGE:
fIncomingVertical = null;
break;
case OUTGOING_HORIZONTAL_EDGE:
fOutgoingHorizontal = null;
break;
case INCOMING_HORIZONTAL_EDGE:
fIncomingHorizontal = null;
break;
default:
throw new IllegalStateException(UNKNOWN_EDGE_DIRECTION_TYPE + dir);
}
}
/**
* Get the neighbor of a vertex from a directed edge. Incoming edges will
* return the vertex from the edge and outgoing edges will return the vertex
* to. This method is a utility method that can be used in code where the
* direction is a variable. If the edge direction is known (using one of the
* EdgeDirection constant), it is preferable to use the
* {@link TmfEdge#getVertexFrom()} and {@link TmfEdge#getVertexTo()}
* directly.
*
* @param edge
* The edge for which to get the right neighbor
* @param dir
* The direction of this edge
* @return The vertex that neighbors another vertex in the requested
* direction
*/
public static TmfVertex getNeighborFromEdge(TmfEdge edge, EdgeDirection dir) {
switch (dir) {
case OUTGOING_VERTICAL_EDGE:
case OUTGOING_HORIZONTAL_EDGE:
return edge.getVertexTo();
case INCOMING_VERTICAL_EDGE:
case INCOMING_HORIZONTAL_EDGE:
return edge.getVertexFrom();
default:
throw new IllegalStateException(UNKNOWN_EDGE_DIRECTION_TYPE + dir);
}
}
@Override
public int compareTo(@Nullable ITmfVertex other) {
if (other == null) {
return 1;
}
return Long.compare(fTimestamp, other.getTimestamp());
}
@Override
public String toString() {
return "[" + fId + "," + fTimestamp + "]"; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
}
}