-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLink.java
32 lines (29 loc) · 1.03 KB
/
Link.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
public class Link {
private Vertex toVertex;
/*Constructor, toVertex takes a string name of the vertex to link TO.
* Method will search through existing vertices in GRAPH object.
* If no matching vertex is found, a new one will be created and added
* to the GRAPH object*/
public Link(String toVertex){
//Search for existing vertex to link to
for (Vertex vertexCurrent: Graph.getVertices()){
if(vertexCurrent.getName().equals(toVertex)) {
//Vertex exists
this.toVertex = vertexCurrent;
this.toVertex.increaseInDegree();
return;
}
}
//No existing vertex exists, create a new vertex
linkToNewVertex(toVertex);
}
private void linkToNewVertex(String toVertex){
Vertex newVertex = new Vertex(toVertex);
this.toVertex = newVertex;
this.toVertex.increaseInDegree();
Graph.addVertex(newVertex);
}
public Vertex getToVertex() {
return toVertex;
}
}