-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.java
227 lines (191 loc) · 6.81 KB
/
Node.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.util.Set;
import java.util.HashSet;
public class Node implements Comparable<Node> {
private static int cpt = 0;
private final int id;
private Linkable data;
private Node next;
public Node(Linkable data) {
this.id = cpt++;
this.data = data;
}
public Linkable getData() {
return this.data;
}
public Node getNext() {
return this.next;
}
public void setNext(Node next) {
this.next = next;
}
/**
* Goes through each linked Node to count how many nodes there are.
* @return int for the number of nodes
*/
public int size() {
Node current = this;
int size = 0;
while (current != null) {
current = current.getNext();
size++;
}
return size;
}
/**
* Goes to the last Node.
* @return Node representing the last linked Node, the one that has no next.
*/
public Node getLast() {
Node current = this;
while (current.next != null) {
current = current.next;
}
return current;
}
/**
* Makes a copy of the current Node with its following Nodes
* @return Node that is independent is its source.
*/
public Node copy(Node stop) {
Node copy = new Node(null);
Node currentOnCopy = copy;
Node currentOnThis = this;
int stopId = stop.id;
while (currentOnThis != null && stopId != currentOnThis.id) {
currentOnCopy.next = new Node(currentOnThis.getData());
currentOnCopy = currentOnCopy.next;
currentOnThis = currentOnThis.next;
}
currentOnCopy.next = new Node(currentOnThis.getData());
return copy.next;
}
/**
* Makes the concatenation of the specified Node and the current one.
* @param another the Node to append
* @return Node starting with the parameter Node and followed by the current Node
*/
public Node concat(Node another) {
if (another != null) {
Node toJoin = another.getLast();
if (toJoin.getData().equals(this.getData())) {
toJoin.next = this.next;
} else {
toJoin.next = this;
}
return another;
} else {
return this;
}
}
/**
* Determines which components are crossed by following this linked Node
* @return Set<String> containing all crossed components
*/
public Set<String> whichComponentsCrossed() {
Set<String> crossedComponents = new HashSet<>();
Set<String> intersection;
Node current = this;
while (current.next != null) {
intersection = Node.intersect(current.getData().getComponents(), current.next.getData().getComponents());
if (!intersection.isEmpty()) {
crossedComponents.addAll(intersection);
}
current = current.next;
}
return crossedComponents;
}
/**
* Determine whether the given Node can replace the specified segment of Nodes
* without changing the crossed components
* @param another replacement
* @param to end Node of the current linked Node
* @param origin the entire linked Node of the current Node
* @param all crossed components
* @return true replaceable
* @return false not replaceable
*/
public boolean isReplaceableInComponentsBy(Node another, Node to, Node origin, Set<String> all) {
Set<String> fusion;
// Determine the components before this;
Node saveNextThis = this.next;
this.next = null;
fusion = origin.whichComponentsCrossed();
this.next = saveNextThis;
// Adding the components of another
fusion.addAll(another.whichComponentsCrossed());
// Adding components after
fusion.addAll(to.whichComponentsCrossed());
return fusion.containsAll(all);
}
/**
* Replaces a segment of the current linked Node from a start with a specified one.
* @param stop last Node of the replaced segment
* @param with linked Nodes replacer
*/
public void replaceUntil(Node stop, Node with) {
this.next = (this.equals(with)) ? with.next : with; // Skipping the first node in case of an equality
Node last = with.getLast();
last.next = (last.equals(stop)) ? stop.next : stop; // Skipping the last node of the new path in case of equality
}
/**
* Intersection of 2 Sets
* @param first Set to intersect
* @param Set to intersect
* @param the intersection as a Set without modifying the parameters.
*/
public static Set<String> intersect(Set<String> first, Set<String> second) {
Set<String> intersection = new HashSet<>(first);
intersection.retainAll(second);
return intersection;
}
/**
* Determines which components are crossed by following this linked Node
* @return Set<String> containing all crossed components
*/
public Set<String> getCrossedComponents() {
Set<String> crossedComponents = new HashSet<>();
Set<String> intersection;
Node current = this;
while (current.next != null) {
intersection = Node.intersect(current.data.getComponents(),current.next.data.getComponents());
if (intersection.size() >= 1) {
crossedComponents.addAll(intersection);
}
current = current.next;
}
return crossedComponents;
}
public boolean equals(Node another) {
return (this.getData().equals(another.getData()));
}
@Override
public boolean equals(Object another) {
if (!this.getClass().equals(another.getClass())) {
return false;
}
Node anotherNode = (Node)another;
return (this.equals(anotherNode));
}
@Override
public int hashCode() {
return this.getData().hashCode();
}
@Override
public int compareTo(Node another) {
return (this.size() - another.size());
}
@Override
public String toString() { // Print reversed chain
StringBuilder strb = new StringBuilder();
if (this.next == null) {
return ("\n\tGroupe(s) : " + this.getData().getComponents() + "\t : " + this.getData().toString() + "\n\t");
} else {
strb.append(this.next);
// Groupe = Composantes connexes
strb.append("Groupe(s) : " + this.data.getComponents());
strb.append("\t : " +this.getData());
strb.append("\n\t");
return strb.toString();
}
}
}