Skip to content
Snippets Groups Projects
Commit 94015a19 authored by Nils Christian Ehmke's avatar Nils Christian Ehmke
Browse files

Modifications for performance reasons

parent 4591ba39
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,7 @@ import teetime.framework.OutputPort; ...@@ -13,6 +13,7 @@ import teetime.framework.OutputPort;
public final class MultipleInstanceOfFilter<I> extends AbstractConsumerStage<I> { public final class MultipleInstanceOfFilter<I> extends AbstractConsumerStage<I> {
private final Map<Class<? extends I>, OutputPort<? super I>> outputPortsMap = new HashMap<Class<? extends I>, OutputPort<? super I>>(); private final Map<Class<? extends I>, OutputPort<? super I>> outputPortsMap = new HashMap<Class<? extends I>, OutputPort<? super I>>();
private Entry<Class<? extends I>, OutputPort<? super I>>[] cachedOutputPortsMap;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends I> OutputPort<T> getOutputPortForType(final Class<T> clazz) { public <T extends I> OutputPort<T> getOutputPortForType(final Class<T> clazz) {
...@@ -22,13 +23,21 @@ public final class MultipleInstanceOfFilter<I> extends AbstractConsumerStage<I> ...@@ -22,13 +23,21 @@ public final class MultipleInstanceOfFilter<I> extends AbstractConsumerStage<I>
return (OutputPort<T>) this.outputPortsMap.get(clazz); return (OutputPort<T>) this.outputPortsMap.get(clazz);
} }
@Override
@SuppressWarnings("unchecked")
public void onStarting() throws Exception {
super.onStarting();
// We cache the map to avoid the creating of iterators during runtime
cachedOutputPortsMap = (Entry<Class<? extends I>, OutputPort<? super I>>[]) outputPortsMap.entrySet().toArray(new Entry<?, ?>[outputPortsMap.size()]);
}
@Override @Override
protected void execute(final I element) { protected void execute(final I element) {
for (Entry<Class<? extends I>, OutputPort<? super I>> outputPortMapEntry : outputPortsMap.entrySet()) { for (Entry<Class<? extends I>, OutputPort<? super I>> outputPortMapEntry : cachedOutputPortsMap) {
if (outputPortMapEntry.getKey().isInstance(element)) { if (outputPortMapEntry.getKey().isInstance(element)) {
outputPortMapEntry.getValue().send(element); outputPortMapEntry.getValue().send(element);
} }
} }
} }
} }
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