1 package de.juplo.kafka;
4 import java.util.HashMap;
6 import java.util.Optional;
9 public class AdderBusinessLogic
11 private final Map<String, AdderResult> state;
14 public AdderBusinessLogic()
16 this(new HashMap<>());
19 public AdderBusinessLogic(Map<String, AdderResult> state)
25 public synchronized Optional<Long> getSum(String user)
27 return Optional.ofNullable(state.get(user)).map(result -> result.sum);
30 public synchronized void addToSum(String user, Integer value)
32 if (value == null || value < 1)
33 throw new IllegalArgumentException("Not a positive number: " + value);
37 .ofNullable(state.get(user))
38 .map(result -> result.sum)
40 state.put(user, new AdderResult(value, sum + value));
43 public synchronized AdderResult calculate(String user)
45 if (!state.containsKey(user))
46 throw new IllegalStateException("No sumation for " + user + " in progress");
48 return state.remove(user);
51 protected Map<String, AdderResult> getState()