X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FAdderBusinessLogic.java;h=1f3d9aa2e67db2934b836d4093cb7485d354f881;hb=de4f94c45fd0678777fecba4dbcb63f89e0ebafa;hp=2e3d126394cc66137a1493409b8f57029e7cf1df;hpb=0aa4a1863bf9ee5d345c652f47f350f82d73e4aa;p=demos%2Fkafka%2Ftraining diff --git a/src/main/java/de/juplo/kafka/AdderBusinessLogic.java b/src/main/java/de/juplo/kafka/AdderBusinessLogic.java index 2e3d126..1f3d9aa 100644 --- a/src/main/java/de/juplo/kafka/AdderBusinessLogic.java +++ b/src/main/java/de/juplo/kafka/AdderBusinessLogic.java @@ -1,13 +1,61 @@ package de.juplo.kafka; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + + public class AdderBusinessLogic { + private final Map state; + + public AdderBusinessLogic() { + this(new HashMap<>()); + } + + public AdderBusinessLogic(Map state) + { + this.state = state; } + public synchronized void startSum(String user) { + if (state.containsKey(user)) + throw new IllegalStateException("Sumation for " + user + " already in progress, state: " + state.get(user)); + + state.put(user, 0l); + } + + public synchronized Optional getSum(String user) + { + return Optional.ofNullable(state.get(user)); + } + + public synchronized void addToSum(String user, Integer value) + { + if (!state.containsKey(user)) + throw new IllegalStateException("No sumation for " + user + " in progress"); + if (value == null || value < 1) + throw new IllegalArgumentException("Not a positive number: " + value); + + long result = state.get(user) + value; + state.put(user, result); + } + + public synchronized Long endSum(String user) + { + if (!state.containsKey(user)) + throw new IllegalStateException("No sumation for " + user + " in progress"); + + return state.remove(user); + } + + protected Map getState() + { + return state; } }