X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FAdderBusinessLogic.java;h=ecd9590de696b14501145682be4dbd58a0b6c955;hb=d576eea9bc9208d9e5003bd8c8c132bed96b5c40;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..ecd9590 100644 --- a/src/main/java/de/juplo/kafka/AdderBusinessLogic.java +++ b/src/main/java/de/juplo/kafka/AdderBusinessLogic.java @@ -1,13 +1,53 @@ 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 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 calculate(String user) + { + if (!state.containsKey(user)) + throw new IllegalStateException("No sumation for " + user + " in progress"); + + return state.remove(user); } - public synchronized void startSum(String user) + protected Map getState() { + return state; } }