Skip to content
Snippets Groups Projects
Commit aae42993 authored by Nelson Tavares de Sousa's avatar Nelson Tavares de Sousa
Browse files

added a CountingMap class

parent a4fd502f
No related branches found
No related tags found
No related merge requests found
package teetime.stage; package teetime.stage;
import java.util.HashMap;
import java.util.Map;
import teetime.framework.AbstractConsumerStage; import teetime.framework.AbstractConsumerStage;
import teetime.framework.OutputPort; import teetime.framework.OutputPort;
import teetime.stage.util.CountingMap;
/** /**
* This counts how many of different elements are sent to this stage. Nothing is forwarded. * This counts how many of different elements are sent to this stage. Nothing is forwarded.
...@@ -16,8 +14,8 @@ import teetime.framework.OutputPort; ...@@ -16,8 +14,8 @@ import teetime.framework.OutputPort;
*/ */
public class DistributedMapCounter<T> extends AbstractConsumerStage<T> { public class DistributedMapCounter<T> extends AbstractConsumerStage<T> {
private final Map<T, Integer> counter = new HashMap<T, Integer>(); private final CountingMap<T> counter = new CountingMap<T>();
private final OutputPort<Map<T, Integer>> port = createOutputPort(); private final OutputPort<CountingMap<T>> port = createOutputPort();
public DistributedMapCounter() { public DistributedMapCounter() {
...@@ -25,13 +23,7 @@ public class DistributedMapCounter<T> extends AbstractConsumerStage<T> { ...@@ -25,13 +23,7 @@ public class DistributedMapCounter<T> extends AbstractConsumerStage<T> {
@Override @Override
protected void execute(final T element) { protected void execute(final T element) {
if (counter.containsKey(element)) { counter.increment(element);
Integer i = counter.get(element);
i++;
counter.put(element, i);
} else {
counter.put(element, 0);
}
} }
......
package teetime.stage.util;
import java.util.HashMap;
public class CountingMap<T> extends HashMap<T, Integer> {
/**
* Generated serialVersionUID
*/
private static final long serialVersionUID = -8036971796701648200L;
public void increment(final T key) {
if (super.containsKey(key)) {
Integer i = super.get(key);
i++;
super.put(key, i);
} else {
super.put(key, 0);
}
}
}
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