From aae429930f6cb084f3217a2f98f1e1b9b9f83d09 Mon Sep 17 00:00:00 2001 From: Nelson Tavares de Sousa <ntd@informatik.uni-kiel.de> Date: Tue, 17 Feb 2015 13:18:31 +0100 Subject: [PATCH] added a CountingMap class --- .../teetime/stage/DistributedMapCounter.java | 16 ++++---------- .../java/teetime/stage/util/CountingMap.java | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 src/main/java/teetime/stage/util/CountingMap.java diff --git a/src/main/java/teetime/stage/DistributedMapCounter.java b/src/main/java/teetime/stage/DistributedMapCounter.java index 2b5dd2a3..de7b7b7f 100644 --- a/src/main/java/teetime/stage/DistributedMapCounter.java +++ b/src/main/java/teetime/stage/DistributedMapCounter.java @@ -1,10 +1,8 @@ package teetime.stage; -import java.util.HashMap; -import java.util.Map; - import teetime.framework.AbstractConsumerStage; import teetime.framework.OutputPort; +import teetime.stage.util.CountingMap; /** * This counts how many of different elements are sent to this stage. Nothing is forwarded. @@ -16,8 +14,8 @@ import teetime.framework.OutputPort; */ public class DistributedMapCounter<T> extends AbstractConsumerStage<T> { - private final Map<T, Integer> counter = new HashMap<T, Integer>(); - private final OutputPort<Map<T, Integer>> port = createOutputPort(); + private final CountingMap<T> counter = new CountingMap<T>(); + private final OutputPort<CountingMap<T>> port = createOutputPort(); public DistributedMapCounter() { @@ -25,13 +23,7 @@ public class DistributedMapCounter<T> extends AbstractConsumerStage<T> { @Override protected void execute(final T element) { - if (counter.containsKey(element)) { - Integer i = counter.get(element); - i++; - counter.put(element, i); - } else { - counter.put(element, 0); - } + counter.increment(element); } diff --git a/src/main/java/teetime/stage/util/CountingMap.java b/src/main/java/teetime/stage/util/CountingMap.java new file mode 100644 index 00000000..d9bb6f39 --- /dev/null +++ b/src/main/java/teetime/stage/util/CountingMap.java @@ -0,0 +1,22 @@ +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); + } + } + +} -- GitLab