|
|
The following code shows a simple ProducerStage. It takes a list of elements are sends them to the ongoing stage.
|
|
|
|
|
|
```java
|
|
|
package teetime.stage;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
import teetime.framework.AbstractProducerStage;
|
|
|
|
|
|
public final class InitialElementProducer<T> extends AbstractProducerStage<T> {
|
|
|
|
|
|
private Iterable<T> elements;
|
|
|
|
|
|
public InitialElementProducer(final T... elements) {
|
|
|
this.elements = Arrays.asList(elements);
|
|
|
}
|
|
|
|
|
|
public InitialElementProducer(final Iterable<T> elements) {
|
|
|
this.elements = elements;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onStarting() throws Exception {
|
|
|
if (elements == null) {
|
|
|
throw new IllegalArgumentException("The given iterable must not be null");
|
|
|
}
|
|
|
super.onStarting();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void execute() {
|
|
|
for (final T element : this.elements) {
|
|
|
this.outputPort.send(element);
|
|
|
}
|
|
|
this.terminate();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
```
|
|
|
|
|
|
First, we implement ``execute``.
|
|
|
It simply iterates over the given list and sends all elements by calling ``this.outputPort.send(element)``.
|
|
|
|
|
|
Furthermore, we want to check if the field ``elements`` is not empty.
|
|
|
This can be achieved by running a check while starting the stage.
|
|
|
For this, we overwrite the method ``onStarting`` and add a null check to it.
|
|
|
You also need to make sure that the super method is also called. |
|
|
\ No newline at end of file |