|
|
For example, consider the `InstanceOfFilter` below. It relays the element from its default input port (got from its super class `AbstractConsumerStage`) to its self-declared output port if the element is an instance of the class represented by `type`. The logics is implemented in the `execute()` method.
|
|
|
|
|
|
```java
|
|
|
public class InstanceOfFilter<I, O> extends AbstractConsumerStage<I> {
|
|
|
|
|
|
private final OutputPort<O> matchingOutputPort = this.createOutputPort();
|
|
|
private final OutputPort<I> defaultOutputPort = this.createOutputPort();
|
|
|
|
|
|
private final Class<O> type;
|
|
|
|
|
|
public InstanceOfFilter(final Class<O> type) {
|
|
|
this.type = type;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void execute(final I element) {
|
|
|
if (this.type.isInstance(element)) {
|
|
|
this.matchingOutputPort.send((O) element);
|
|
|
} else {
|
|
|
this.defaultOutputPort.send((I) element);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Class<O> getType() {
|
|
|
return this.type;
|
|
|
}
|
|
|
|
|
|
public OutputPort<O> getMatchingOutputPort() {
|
|
|
return this.matchingOutputPort;
|
|
|
}
|
|
|
|
|
|
public OutputPort<O> getDefaultOutputPort() {
|
|
|
return this.defaultOutputPort;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
``` |