package javaflow.network.definer;
import javaflow.network.api.CreateNextConnection;
import javaflow.network.api.NetworkApiUtil;
import javaflow.network.api.PortReference;
abstract class ConnectionDefinitionBase implements CreateNextConnection {
protected final NetworkDefiner definitionBuilder;
protected final PortReference fromReference;
public ConnectionDefinitionBase(PortReference fromReference, NetworkDefiner definitionBuilder) {
this.fromReference = fromReference;
this.definitionBuilder = definitionBuilder;
}
public CreateNextConnection to(String toReference) {
String inputPort = "IN";
String outputPort = "OUT";
if (!isComponentName(toReference)){
if (!NetworkApiUtil.isPortName(toReference)){
throw new Error(toReference+" is not existing component name or suitable port name");
}
PortReference portReference = PortReference.parse(toReference);
stepReady(portReference);
return new CreateNextConnection(){
@Override
public CreateNextConnection to(String portReference) {
throw new Error("Can not continue chain outside network");
}
@Override
public CreateNextConnection to(String inputPort, String componentName) {
throw new Error("Can not continue chain outside network");
}
@Override
public CreateNextConnection to(String in, String c, String s) {
throw new Error("Can not continue chain outside network");
}
};
}
return to(inputPort,toReference,outputPort);
}
abstract void stepReady(PortReference portReference);
private boolean isComponentName(String toReference) {
return definitionBuilder.isComponentName(toReference);
}
public final CreateNextConnection to(String string1, String string2) {
String inputPort = "IN";
String component = null;
String outputPort = "OUT";
if (isComponentName(string1)){
component = string1;
outputPort = string2;
} else if (isComponentName(string2)){
inputPort = string1;
component = string2;
}
if (component == null){
throw new Error("No such component: "+string1+" or "+string2);
}
return to(inputPort,component,outputPort);
}
public CreateNextConnection to(String inputPort, String component, String outputPort) {
stepReady(NetworkDefiner.createPortReference(component, inputPort));
return new NetworkDefiner.ConnectionDefinitionImpl(definitionBuilder, NetworkDefiner.createPortReference(component, outputPort));
}
}