com.datastax.insight.core.dag.DAG Maven / Gradle / Ivy
The newest version!
package com.datastax.insight.core.dag;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
public class DAG {
private long id;
private String name;
private List vertices=new ArrayList<>();
private List edges=new ArrayList<>();
public final static String START_VERTEX="start";
public final static String END_VERTEX="end";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getVertices() {
return vertices;
}
public void setVertices(List vertices) {
this.vertices = vertices;
}
public List getEdges() {
return edges;
}
public void setEdges(List edges) {
this.edges = edges;
}
public Vertex getVById(long id){
for(Vertex v : vertices){
if(v.getId()==id) return v;
}
return null;
}
public Vertex getVByName(String name){
for(Vertex v : vertices){
if(v.getType().equals(name)) return v;
}
return null;
}
public List next(long id){
List vertexList=new ArrayList<>();
for(Edge e : edges){
Long source = e.getSource() == null ? e.getStart() : e.getSource();
if(source.equals(id)){
Long target = e.getTarget() == null ? e.getEnd() : e.getTarget();
Vertex v=getVById(target);
vertexList.add(v);
}
}
return vertexList;
}
public List prev(long id){
List vertexList=new ArrayList<>();
for(Edge e : edges){
Long target = e.getTarget() == null ? e.getEnd() : e.getTarget();
if(target.equals(id)){
Long source = e.getSource() == null ? e.getStart() : e.getSource();
Vertex v=getVById(source);
vertexList.add(v);
}
}
return vertexList;
}
public void addVertex(Vertex vertex){
if(!containsVertex(vertex)){
vertices.add(vertex);
}
}
public boolean containsVertex(Vertex vertex){
boolean flag=false;
for(Vertex v : vertices){
if(v.getId()==vertex.getId()) flag=true;
}
return flag;
}
public void addEdge(Edge edge){
if(!containsEdge(edge)){
edges.add(edge);
}
}
public boolean containsEdge(Edge edge){
boolean flag=false;
for(Edge e : edges){
Long sourceL = e.getSource() == null ? e.getStart() : e.getSource();
Long sourceR = edge.getSource() == null ? edge.getStart() : edge.getSource();
Long targetL = e.getTarget() == null ? e.getEnd() : e.getTarget();
Long targetR = edge.getTarget() == null ? edge.getEnd() : edge.getTarget();
if (sourceL.equals(sourceR) &&
targetL.equals(targetR) &&
e.getShape().equals(edge.getShape())) {
flag = true;
}
}
return flag;
}
public Optional findEdge(long start, long end) {
return this.edges.stream().filter(edge -> edge.getStart() == start && edge.getEnd() == end).findAny();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}