Skip to content
Snippets Groups Projects
Commit bb54192a authored by Christian Wulf's avatar Christian Wulf
Browse files

improved InstanceOfFilter

parent 6da059be
No related branches found
No related tags found
No related merge requests found
......@@ -19,12 +19,13 @@ import teetime.framework.AbstractConsumerStage;
import teetime.framework.OutputPort;
/**
* @author Jan Waller, Nils Christian Ehmke, Christian Wulf
* @author Jan Waller, Nils Christian Ehmke, Christian Wulf, Nelson Tavares de Sousa
*
*/
public final class InstanceOfFilter<I, O> extends AbstractConsumerStage<I> {
public final class InstanceOfFilter<I, O extends I> extends AbstractConsumerStage<I> {
private final OutputPort<O> outputPort = this.createOutputPort();
private final OutputPort<O> matchedOutputPort = this.createOutputPort();
private final OutputPort<I> mismatchedOutputPort = this.createOutputPort();
private Class<O> type;
......@@ -36,11 +37,12 @@ public final class InstanceOfFilter<I, O> extends AbstractConsumerStage<I> {
@Override
protected void execute(final I element) {
if (this.type.isInstance(element)) {
outputPort.send((O) element);
} else { // swallow up the element
matchedOutputPort.send((O) element);
} else {
if (this.logger.isDebugEnabled()) {
this.logger.debug("element is not an instance of " + this.type.getName() + ", but of " + element.getClass());
}
mismatchedOutputPort.send(element);
}
}
......@@ -52,8 +54,23 @@ public final class InstanceOfFilter<I, O> extends AbstractConsumerStage<I> {
this.type = type;
}
/**
*
* @return the output port that outputs
*
* @deprecated 1.1. Use {@link #getMatchedOutputPort()} instead.
*/
@Deprecated
public OutputPort<O> getOutputPort() {
return this.outputPort;
return matchedOutputPort;
}
public OutputPort<O> getMatchedOutputPort() {
return matchedOutputPort;
}
public OutputPort<I> getMismatchedOutputPort() {
return mismatchedOutputPort;
}
}
wiki @ 0e447457
Subproject commit 63ccbbc87bd2c0e6599ca91502149dba3cfb99de
Subproject commit 0e4474577e1f49bc96e734c286b2d9e0363895e8
......@@ -45,7 +45,7 @@ public class InstanceOfFilterTest {
final List<Clazz> results = new ArrayList<InstanceOfFilterTest.Clazz>();
final Object clazz = new Clazz();
test(filter).and().send(clazz).to(filter.getInputPort()).and().receive(results).from(filter.getOutputPort()).start();
test(filter).and().send(clazz).to(filter.getInputPort()).and().receive(results).from(filter.getMatchedOutputPort()).start();
assertThat(results, contains(clazz));
}
......@@ -55,7 +55,7 @@ public class InstanceOfFilterTest {
final List<Clazz> results = new ArrayList<InstanceOfFilterTest.Clazz>();
final Object clazz = new SubClazz();
test(filter).and().send(clazz).to(filter.getInputPort()).and().receive(results).from(filter.getOutputPort()).start();
test(filter).and().send(clazz).to(filter.getInputPort()).and().receive(results).from(filter.getMatchedOutputPort()).start();
assertThat(results, contains(clazz));
}
......@@ -65,7 +65,7 @@ public class InstanceOfFilterTest {
final List<Clazz> results = new ArrayList<InstanceOfFilterTest.Clazz>();
final Object object = new Object();
test(filter).and().send(object).to(filter.getInputPort()).and().receive(results).from(filter.getOutputPort()).start();
test(filter).and().send(object).to(filter.getInputPort()).and().receive(results).from(filter.getMatchedOutputPort()).start();
assertThat(results, is(empty()));
}
......@@ -81,7 +81,7 @@ public class InstanceOfFilterTest {
input.add(new SubClazz());
input.add(new Object());
test(filter).and().send(input).to(filter.getInputPort()).and().receive(results).from(filter.getOutputPort()).start();
test(filter).and().send(input).to(filter.getInputPort()).and().receive(results).from(filter.getMatchedOutputPort()).start();
assertThat(results, hasSize(2));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment