Analysis: Add the dependency graph plugin and base classes
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.graph.core / src / org / eclipse / tracecompass / analysis / graph / core / base / TmfVertex.java
1 /*******************************************************************************
2 * Copyright (c) 2015 École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
10 * Francis Giraldeau - Initial implementation and API
11 * Geneviève Bastien - Initial implementation and API
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.analysis.graph.core.base;
15
16 import java.util.Comparator;
17
18 import org.eclipse.jdt.annotation.Nullable;
19
20 /**
21 * Timed vertex for TmfGraph
22 *
23 * @author Francis Giraldeau
24 * @author Geneviève Bastien
25 */
26 public class TmfVertex implements Comparable<TmfVertex> {
27
28 private static long count = 0;
29
30 /**
31 * Describe the four edges coming in and out of a vertex
32 */
33 public enum EdgeDirection {
34 /**
35 * Constant for the outgoing vertical edge (to other object)
36 */
37 OUTGOING_VERTICAL_EDGE,
38 /**
39 * Constant for the incoming vertical edge (from other object)
40 */
41 INCOMING_VERTICAL_EDGE,
42 /**
43 * Constant for the outgoing horizontal edge (to same object)
44 */
45 OUTGOING_HORIZONTAL_EDGE,
46 /**
47 * Constant for the incoming horizontal edge (from same object)
48 */
49 INCOMING_HORIZONTAL_EDGE
50 }
51
52 /**
53 * Compare vertices by ascending timestamps
54 */
55 public static Comparator<TmfVertex> ascending = new Comparator<TmfVertex>() {
56 @Override
57 public int compare(@Nullable TmfVertex v1, @Nullable TmfVertex v2) {
58 if (v1 == null) {
59 return 1;
60 }
61 if (v2 == null) {
62 return -1;
63 }
64 return v1.getTs() > v2.getTs() ? 1 : (v1.getTs() == v2.getTs() ? 0 : -1);
65 }
66 };
67
68 /**
69 * Compare vertices by descending timestamps
70 */
71 public static Comparator<TmfVertex> descending = new Comparator<TmfVertex>() {
72 @Override
73 public int compare(@Nullable TmfVertex v1, @Nullable TmfVertex v2) {
74 if (v1 == null) {
75 return -1;
76 }
77 if (v2 == null) {
78 return 1;
79 }
80 return v1.getTs() < v2.getTs() ? 1 : (v1.getTs() == v2.getTs() ? 0 : -1);
81 }
82 };
83
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;
90
91 /**
92 * Default Constructor
93 */
94 public TmfVertex() {
95 this(0);
96 }
97
98 /**
99 * Constructor with timestamp
100 *
101 * @param ts
102 * The vertex's timestamp
103 */
104 public TmfVertex(final long ts) {
105 fTimestamp = ts;
106 synchronized (TmfVertex.class) {
107 fId = count++;
108 }
109 }
110
111 /**
112 * Copy constructor. Keeps same timestamp, but does not keep edges
113 *
114 * @param node
115 * vertex to copy
116 */
117 public TmfVertex(TmfVertex node) {
118 this(node.fTimestamp);
119 }
120
121 /**
122 * Copy constructor, but changes the timestamp
123 *
124 * @param node
125 * vertex to copy
126 * @param ts
127 * The timestamp of this new node
128 */
129 public TmfVertex(TmfVertex node, final long ts) {
130 fTimestamp = ts;
131 synchronized (TmfVertex.class) {
132 fId = count++;
133 }
134 fOutgoingVertical = node.fOutgoingVertical;
135 fIncomingVertical = node.fIncomingVertical;
136 fOutgoingHorizontal = node.fOutgoingHorizontal;
137 fIncomingHorizontal = node.fIncomingHorizontal;
138 }
139
140 /*
141 * Getters and setters
142 */
143
144 /**
145 * Returns the timestamps of this node
146 *
147 * @return the timstamp
148 */
149 public long getTs() {
150 return fTimestamp;
151 }
152
153 /**
154 * Returns the unique ID of this node
155 *
156 * @return the vertex's id
157 */
158 public long getID() {
159 return fId;
160 }
161
162 /**
163 * Adds an horizontal edge from the current vertex to the 'to' vertex
164 *
165 * @param to
166 * The vertex to link to, belongs to the same object
167 *
168 * @return The new edge
169 */
170 public TmfEdge linkHorizontal(TmfVertex to) {
171 checkTimestamps(to);
172 return linkHorizontalRaw(to);
173 }
174
175 private TmfEdge linkHorizontalRaw(TmfVertex node) {
176 TmfEdge link = new TmfEdge(this, node);
177 fOutgoingHorizontal = link;
178 node.fIncomingHorizontal = link;
179 return link;
180 }
181
182 /**
183 * Adds a vertical edge from the current vertex to the 'to' vertex
184 *
185 * @param to
186 * The vertex to link to, belongs to a different object
187 * @return The new edge
188 */
189 public TmfEdge linkVertical(TmfVertex to) {
190 checkTimestamps(to);
191 return linkVerticalRaw(to);
192 }
193
194 private TmfEdge linkVerticalRaw(TmfVertex to) {
195 TmfEdge link = new TmfEdge(this, to);
196 fOutgoingVertical = link;
197 to.fIncomingVertical = link;
198 return link;
199 }
200
201 private void checkTimestamps(TmfVertex to) {
202 if (this.fTimestamp > to.fTimestamp) {
203 throw new IllegalArgumentException(Messages.TmfVertex_ArgumentTimestampLower +
204 String.format(": (curr=%d,next=%d,elapsed=%d)", fTimestamp, to.fTimestamp, to.fTimestamp - fTimestamp)); //$NON-NLS-1$
205 }
206 }
207
208 /**
209 * Get an edge to or from this vertex in the appropriate direction
210 *
211 * @param dir
212 * The direction of the requested edge
213 * @return The edge from this vertex to the requested direction
214 */
215 public @Nullable TmfEdge getEdge(EdgeDirection dir) {
216 switch (dir) {
217 case OUTGOING_VERTICAL_EDGE:
218 return fOutgoingVertical;
219 case INCOMING_VERTICAL_EDGE:
220 return fIncomingVertical;
221 case OUTGOING_HORIZONTAL_EDGE:
222 return fOutgoingHorizontal;
223 case INCOMING_HORIZONTAL_EDGE:
224 return fIncomingHorizontal;
225 default:
226 throw new IllegalStateException();
227 }
228 }
229
230 /**
231 * Removes a directed edge from this vertex. The edge in that direction will
232 * be null.
233 *
234 * @param dir
235 * The direction to remove the edge from
236 */
237 public void removeEdge(EdgeDirection dir) {
238 switch (dir) {
239 case OUTGOING_VERTICAL_EDGE:
240 fOutgoingVertical = null;
241 break;
242 case INCOMING_VERTICAL_EDGE:
243 fIncomingVertical = null;
244 break;
245 case OUTGOING_HORIZONTAL_EDGE:
246 fOutgoingHorizontal = null;
247 break;
248 case INCOMING_HORIZONTAL_EDGE:
249 fIncomingHorizontal = null;
250 break;
251 default:
252 throw new IllegalStateException();
253 }
254 }
255
256 /**
257 * Get the neighbor of a vertex from a directed edge. Incoming edges will
258 * return the vertex from the edge and outgoing edges will return the vertex
259 * to. This method is a utility method that can be used in code where the
260 * direction is a variable. If the edge direction is known (using one of the
261 * EdgeDirection constant), it is preferable to use the
262 * {@link TmfEdge#getVertexFrom()} and {@link TmfEdge#getVertexTo()}
263 * directly.
264 *
265 * @param edge
266 * The edge for which to get the right neighbor
267 * @param dir
268 * The direction of this edge
269 * @return The vertex that neighbors another vertex in the requested
270 * direction
271 */
272 public static TmfVertex getNeighborFromEdge(TmfEdge edge, EdgeDirection dir) {
273 switch (dir) {
274 case OUTGOING_VERTICAL_EDGE:
275 case OUTGOING_HORIZONTAL_EDGE:
276 return edge.getVertexTo();
277 case INCOMING_VERTICAL_EDGE:
278 case INCOMING_HORIZONTAL_EDGE:
279 return edge.getVertexFrom();
280 default:
281 throw new IllegalStateException();
282 }
283 }
284
285 @Override
286 public int compareTo(@Nullable TmfVertex other) {
287 if (other == null) {
288 return 1;
289 }
290 return this.fTimestamp > other.fTimestamp ? 1 : (this.fTimestamp == other.fTimestamp ? 0 : -1);
291 }
292
293 @Override
294 public String toString() {
295 return "[" + fId + "," + fTimestamp + "]"; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
296 }
297
298 }
This page took 0.038913 seconds and 6 git commands to generate.