package javaflow.network.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import javaflow.components.api.OutputPort;
import javaflow.components.api.OutputPorts;
import javaflow.network.api.NetworkComponent;
import javaflow.network.api.PortReference;
class OutputPortsImpl<T> implements OutputPorts<T>, Port {
private final String componentName;
private final String portBaseName;
private final ArrayList<OutputPortImpl> outputs = new ArrayList<>();
private AbstractRunnableComponent owner;
OutputPortsImpl(PortReference portDef) {
this.componentName = portDef.componentName();
this.portBaseName = portDef.portName();
}
@Override
public int size() {
return outputs.size();
}
@Override
public void closeAll() {
for (OutputPortImpl out : outputs) {
out.close();
}
}
@Override
public Iterator<OutputPort<T>> iterator() {
return (Iterator) Collections.unmodifiableList(outputs).iterator();
}
@Override
public void close() {
closeAll();
}
@Override
public int getOpenPortCount() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean hasPacket() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setOwner(AbstractRunnableComponent owner) {
for (Port port : outputs) {
port.setOwner(owner);
}
this.owner = owner;
}
OutputPortImpl get(int i) {
while (i >= outputs.size()) {
int index = outputs.size();
OutputPortImpl out = new OutputPortImpl(new PortReference(componentName, portBaseName, index));
outputs.add(out);
out.setOwner(owner);
}
return outputs.get(i);
}
@Override
public OutputPortImpl port(int index) {
return outputs.get(index);
}
@Override
public String toString() {
return toString(0);
}
public String toString(int indentation) {
StringBuilder builder = new StringBuilder();
builder.append(Utils.indent(indentation, componentName));
builder.append(".");
builder.append(portBaseName);
builder.append("[]:\n");
for (int i = 0; i < size(); i++) {
OutputPortImpl port = port(i);
builder.append(Utils.indent(indentation, " #"));
builder.append(i);
builder.append(" ");
builder.append(port);
builder.append("\n");
}
return builder.toString();
}
@Override
public NetworkComponent getOwner() {
return owner;
}
public void reopen() {
for(OutputPortImpl port : outputs){
port.reopen();
}
}
}