diff --git a/src/main/java/teetime/framework/Stage.java b/src/main/java/teetime/framework/Stage.java index 99f6c0b8d15139c066de47dd9c287e63dd797da9..988fb9e609a6f934075a52cdf000fd78f06e9038 100644 --- a/src/main/java/teetime/framework/Stage.java +++ b/src/main/java/teetime/framework/Stage.java @@ -1,7 +1,8 @@ package teetime.framework; import java.util.List; -import java.util.UUID; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,17 +10,18 @@ import org.slf4j.LoggerFactory; import teetime.framework.signal.ISignal; import teetime.framework.validation.InvalidPortConnection; -public abstract class Stage { +public abstract class Stage { // NOPMD (should not start with "Abstract" private final String id; + private static final Map<String, Integer> INSTANCES_COUNTER = new ConcurrentHashMap<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.logger = LoggerFactory.getLogger(this.getClass().getName() + "(" + this.id + ")"); + this.id = this.createId(); + this.logger = LoggerFactory.getLogger(this.getClass().getName() + "-" + this.id); } public String getId() { @@ -31,6 +33,19 @@ public abstract class Stage { return this.getClass().getName() + ": " + this.getId(); } + private String createId() { + String simpleName = this.getClass().getSimpleName(); + + Integer numInstances = INSTANCES_COUNTER.get(simpleName); + if (null == numInstances) { + numInstances = 0; + } + + String newId = simpleName + "-" + numInstances; + INSTANCES_COUNTER.put(simpleName, ++numInstances); + return newId; + } + // 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 0000000000000000000000000000000000000000..2a63e93e38e57b2f1a342e4005477f7537e08b81 --- /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()); + } + } + +}