-
Notifications
You must be signed in to change notification settings - Fork 7
/
AbstractNode.java
83 lines (72 loc) · 2.14 KB
/
AbstractNode.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
package com.ercatcher.ConcurrencyAnalysis;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public abstract class AbstractNode <T extends AbstractNode, S> {
private static int created = 0;
private static int deleted = 0;
protected Set<T> children = new HashSet<>();
protected Set<T> parents = new HashSet<>();
protected String title = null;
private S sourceNode;
protected AbstractNode(S correspondingNode){
created += 1;
this.sourceNode = correspondingNode;
}
public Set<T> getParents() {
return new HashSet<>(parents);
}
@Override
public void finalize() {
deleted += 1;
}
public S getSource(){
return sourceNode;
}
public void changeSource(S correspondingNode){
this.sourceNode = correspondingNode;
}
public Set<T> getChildren(){
return new HashSet<>(children);
}
public void addChild(T child){
children.add(child);
T thisT = (T) this;
child.parents.add(thisT);
}
public void removeMySelf(){
List<T> copyChildren = new ArrayList<>(children);
List<T> copyParent = new ArrayList<>(parents);
for (T child : copyChildren)
child.parents.remove(this);
for(T parent : copyParent) {
parent.children.remove(this);
for (T child : copyChildren) {
parent.addChild(child);
}
}
// TODO: make children and parents null
}
public void destroy(){
children = null;
parents = null;
sourceNode = null;
}
public boolean haveCycle(){
return this.haveCycle(new HashSet<>(), new HashSet<>());
}
protected boolean haveCycle(Set<T> visited, Set<T> working){
T thisT = (T) this;
if(working.contains(thisT))
return true;
working.add(thisT);
for(T child : getChildren()){
if(!visited.contains(child) && child.haveCycle(visited, working))
return true;
}
working.remove(thisT);
visited.add(thisT);
return false;
}
}