DependencyConnection.java utility method for parsing connection id, TODO direct storing of connection IDs (not a quick issue), update disconnect() with clearing of connectedCOnnections and correctly removing itself from all connected connection's connectedConnections
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
public abstract class DependencyConnection<T> {
@@ -104,6 +106,9 @@ public void connectInputToOutput(DependencyConnection<T> fromConnection) {
/*if (this.connectedConnection != null) {
this.connectedConnection.connectedConnection = null;
}*/
+ if(this.connectionType == Type.INPUT) {
+ connectedConnections.clear();
+ }
this.connectedConnections.putIfAbsent(fromConnection.getName(), fromConnection);
this.connectedConnections.get(fromConnection.getName()).getConnectedConnections().putIfAbsent(this.getName(), this);
this.data = fromConnection.getData();
@@ -113,8 +118,8 @@ public void connectInputToOutput(DependencyConnection<T> fromConnection) {
* Remove connections
*/
public void disconnect() {
- this.connectedConnections.forEach((k, v)->v.connectedConnections.remove(this));
- this.connectedConnections = null;
+ this.connectedConnections.forEach((k, v)->v.connectedConnections.remove(this.getName()));
+ this.connectedConnections.clear();
if (this.connectionType == Type.INPUT) {
this.data = null;
}
@@ -125,14 +130,37 @@ public void disconnect() {
@Override
public String toString() {
StringBuilder connectedConnectionString = new StringBuilder("");
+ if (connectedConnections != null) {
connectedConnections.forEach((k, v) -> connectedConnectionString.append(k).append(", "));
connectedConnectionString.append(";");
+ }
return (connectionType == Type.OUTPUT)
? String.format("Output:%s(connected to %s)", connectionName, connectedConnectionString.toString())
: String.format("Input:%s(connected to %s)", connectionName, connectedConnectionString.toString());
}
+ // TODO - would take a lot of time to add this atm
+// public getId() {
+// return this.id;
+// }
+
+ public static int getIdFromConnectionName(String connectionName) {
+ int id;
+ final Pattern lastIntPattern = Pattern.compile("[^0-9]+([0-9]+)$");
+
+ String input = connectionName;
+ Matcher matcher = lastIntPattern.matcher(input);
+
+ if (matcher.find()) {
+ String digits = matcher.group(1);
+ id = Integer.parseInt(digits);
+ } else {
+ throw new RuntimeException("Could not parse ID from connection name");
+ }
+ return id;
+ }
+
/*
* Constructs DependencyConnection name based on ID and Dep.connection type.
* @param id
