1 /*******************************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Francis Giraldeau - Initial implementation and API
11 * Geneviève Bastien - Initial implementation and API
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.analysis
.graph
.core
.base
;
16 import java
.util
.Comparator
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
21 * Timed vertex for TmfGraph
23 * @author Francis Giraldeau
24 * @author Geneviève Bastien
26 public class TmfVertex
implements Comparable
<TmfVertex
> {
28 private static long count
= 0;
31 * Describe the four edges coming in and out of a vertex
33 public enum EdgeDirection
{
35 * Constant for the outgoing vertical edge (to other object)
37 OUTGOING_VERTICAL_EDGE
,
39 * Constant for the incoming vertical edge (from other object)
41 INCOMING_VERTICAL_EDGE
,
43 * Constant for the outgoing horizontal edge (to same object)
45 OUTGOING_HORIZONTAL_EDGE
,
47 * Constant for the incoming horizontal edge (from same object)
49 INCOMING_HORIZONTAL_EDGE
53 * Compare vertices by ascending timestamps
55 public static Comparator
<TmfVertex
> ascending
= new Comparator
<TmfVertex
>() {
57 public int compare(@Nullable TmfVertex v1
, @Nullable TmfVertex v2
) {
64 return v1
.getTs() > v2
.getTs() ?
1 : (v1
.getTs() == v2
.getTs() ?
0 : -1);
69 * Compare vertices by descending timestamps
71 public static Comparator
<TmfVertex
> descending
= new Comparator
<TmfVertex
>() {
73 public int compare(@Nullable TmfVertex v1
, @Nullable TmfVertex v2
) {
80 return v1
.getTs() < v2
.getTs() ?
1 : (v1
.getTs() == v2
.getTs() ?
0 : -1);
84 private @Nullable TmfEdge fOutgoingVertical
= null;
85 private @Nullable TmfEdge fIncomingVertical
= null;
86 private @Nullable TmfEdge fOutgoingHorizontal
= null;
87 private @Nullable TmfEdge fIncomingHorizontal
= null;
88 private final long fTimestamp
;
89 private final long fId
;
99 * Constructor with timestamp
102 * The vertex's timestamp
104 public TmfVertex(final long ts
) {
106 synchronized (TmfVertex
.class) {
112 * Copy constructor. Keeps same timestamp, but does not keep edges
117 public TmfVertex(TmfVertex node
) {
118 this(node
.fTimestamp
);
122 * Copy constructor, but changes the timestamp
127 * The timestamp of this new node
129 public TmfVertex(TmfVertex node
, final long ts
) {
131 synchronized (TmfVertex
.class) {
134 fOutgoingVertical
= node
.fOutgoingVertical
;
135 fIncomingVertical
= node
.fIncomingVertical
;
136 fOutgoingHorizontal
= node
.fOutgoingHorizontal
;
137 fIncomingHorizontal
= node
.fIncomingHorizontal
;
141 * Getters and setters
145 * Returns the timestamps of this node
147 * @return the timstamp
149 public long getTs() {
154 * Returns the unique ID of this node
156 * @return the vertex's id
158 public long getID() {
163 * Adds an horizontal edge from the current vertex to the 'to' vertex
166 * The vertex to link to, belongs to the same object
168 * @return The new edge
170 public TmfEdge
linkHorizontal(TmfVertex to
) {
173 return linkHorizontalRaw(to
);
176 private TmfEdge
linkHorizontalRaw(TmfVertex node
) {
177 TmfEdge link
= new TmfEdge(this, node
);
178 fOutgoingHorizontal
= link
;
179 node
.fIncomingHorizontal
= link
;
184 * Adds a vertical edge from the current vertex to the 'to' vertex
187 * The vertex to link to, belongs to a different object
188 * @return The new edge
190 public TmfEdge
linkVertical(TmfVertex to
) {
193 return linkVerticalRaw(to
);
196 private TmfEdge
linkVerticalRaw(TmfVertex to
) {
197 TmfEdge link
= new TmfEdge(this, to
);
198 fOutgoingVertical
= link
;
199 to
.fIncomingVertical
= link
;
203 private void checkTimestamps(TmfVertex to
) {
204 if (this.fTimestamp
> to
.fTimestamp
) {
205 throw new IllegalArgumentException(Messages
.TmfVertex_ArgumentTimestampLower
+
206 String
.format(": (curr=%d,next=%d,elapsed=%d)", fTimestamp
, to
.fTimestamp
, to
.fTimestamp
- fTimestamp
)); //$NON-NLS-1$
210 private void checkNotSelf(TmfVertex to
) {
212 throw new IllegalArgumentException(Messages
.TmfVertex_CannotLinkToSelf
);
218 * Get an edge to or from this vertex in the appropriate direction
221 * The direction of the requested edge
222 * @return The edge from this vertex to the requested direction
224 public @Nullable TmfEdge
getEdge(EdgeDirection dir
) {
226 case OUTGOING_VERTICAL_EDGE
:
227 return fOutgoingVertical
;
228 case INCOMING_VERTICAL_EDGE
:
229 return fIncomingVertical
;
230 case OUTGOING_HORIZONTAL_EDGE
:
231 return fOutgoingHorizontal
;
232 case INCOMING_HORIZONTAL_EDGE
:
233 return fIncomingHorizontal
;
235 throw new IllegalStateException("Unknown edge direction type : " + dir
); //$NON-NLS-1$
240 * Removes a directed edge from this vertex. The edge in that direction will
244 * The direction to remove the edge from
246 public void removeEdge(EdgeDirection dir
) {
248 case OUTGOING_VERTICAL_EDGE
:
249 fOutgoingVertical
= null;
251 case INCOMING_VERTICAL_EDGE
:
252 fIncomingVertical
= null;
254 case OUTGOING_HORIZONTAL_EDGE
:
255 fOutgoingHorizontal
= null;
257 case INCOMING_HORIZONTAL_EDGE
:
258 fIncomingHorizontal
= null;
261 throw new IllegalStateException("Unknown edge direction type : " + dir
); //$NON-NLS-1$
266 * Get the neighbor of a vertex from a directed edge. Incoming edges will
267 * return the vertex from the edge and outgoing edges will return the vertex
268 * to. This method is a utility method that can be used in code where the
269 * direction is a variable. If the edge direction is known (using one of the
270 * EdgeDirection constant), it is preferable to use the
271 * {@link TmfEdge#getVertexFrom()} and {@link TmfEdge#getVertexTo()}
275 * The edge for which to get the right neighbor
277 * The direction of this edge
278 * @return The vertex that neighbors another vertex in the requested
281 public static TmfVertex
getNeighborFromEdge(TmfEdge edge
, EdgeDirection dir
) {
283 case OUTGOING_VERTICAL_EDGE
:
284 case OUTGOING_HORIZONTAL_EDGE
:
285 return edge
.getVertexTo();
286 case INCOMING_VERTICAL_EDGE
:
287 case INCOMING_HORIZONTAL_EDGE
:
288 return edge
.getVertexFrom();
290 throw new IllegalStateException("Unknown edge direction type : " + dir
); //$NON-NLS-1$
295 public int compareTo(@Nullable TmfVertex other
) {
299 return this.fTimestamp
> other
.fTimestamp ?
1 : (this.fTimestamp
== other
.fTimestamp ?
0 : -1);
303 public String
toString() {
304 return "[" + fId
+ "," + fTimestamp
+ "]"; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$