From 103c706aacf15ca5b181d2825407351e443b4b23 Mon Sep 17 00:00:00 2001 From: Nelson Tavares de Sousa <ntd@informatik.uni-kiel.de> Date: Thu, 11 Dec 2014 13:47:28 +0100 Subject: [PATCH] modified stage naming --- src/main/java/teetime/framework/Stage.java | 20 ++++++++++++++-- .../java/teetime/framework/StageTest.java | 24 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/test/java/teetime/framework/StageTest.java diff --git a/src/main/java/teetime/framework/Stage.java b/src/main/java/teetime/framework/Stage.java index 99f6c0b8..7e8bbef4 100644 --- a/src/main/java/teetime/framework/Stage.java +++ b/src/main/java/teetime/framework/Stage.java @@ -1,7 +1,7 @@ package teetime.framework; +import java.util.HashMap; import java.util.List; -import java.util.UUID; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,13 +12,15 @@ import teetime.framework.validation.InvalidPortConnection; public abstract class Stage { private final String id; + private static HashMap<String, Integer> instancesCounter = new HashMap<String, Integer>(); /** * A unique logger instance per stage instance */ protected final Logger logger; // NOPMD protected Stage() { - this.id = UUID.randomUUID().toString(); // the id should only be represented by a UUID, not additionally by the class name + this.id = this.nameInstance(); + // this.id = UUID.randomUUID().toString(); // the id should only be represented by a UUID, not additionally by the class name this.logger = LoggerFactory.getLogger(this.getClass().getName() + "(" + this.id + ")"); } @@ -31,6 +33,20 @@ public abstract class Stage { return this.getClass().getName() + ": " + this.getId(); } + private String nameInstance() { + int instances = 0; + String id; + String simpleName = this.getClass().getSimpleName(); + + if (instancesCounter.containsKey(simpleName)) { + instances = instancesCounter.get(simpleName); + } + + id = simpleName + "-" + instances; + instancesCounter.put(simpleName, ++instances); + return id; + } + // public abstract Stage getParentStage(); // // public abstract void setParentStage(Stage parentStage, int index); diff --git a/src/test/java/teetime/framework/StageTest.java b/src/test/java/teetime/framework/StageTest.java new file mode 100644 index 00000000..2a63e93e --- /dev/null +++ b/src/test/java/teetime/framework/StageTest.java @@ -0,0 +1,24 @@ +package teetime.framework; + +import org.junit.Assert; +import org.junit.Test; + +import teetime.stage.Cache; +import teetime.stage.Counter; + +public class StageTest { + + @Test + public void testId() { + Counter<Object> counter0 = new Counter<Object>(); + Counter<Object> counter1 = new Counter<Object>(); + Assert.assertEquals("Counter-0", counter0.getId()); + Assert.assertEquals("Counter-1", counter1.getId()); + + for (int i = 0; i < 100; i++) { + Cache<Object> cache = new Cache<Object>(); + Assert.assertEquals("Cache-" + i, cache.getId()); + } + } + +} -- GitLab