+ this(new HashMap<>());
+ }
+
+ public AdderBusinessLogic(Map<String, AdderResult> state)
+ {
+ this.state = state;
+ }
+
+
+ public synchronized Optional<Long> getSum(String user)
+ {
+ return Optional.ofNullable(state.get(user)).map(result -> result.sum);
+ }
+
+ public synchronized void addToSum(String user, Integer value)
+ {
+ if (value == null || value < 1)
+ throw new IllegalArgumentException("Not a positive number: " + value);
+
+ long sum =
+ Optional
+ .ofNullable(state.get(user))
+ .map(result -> result.sum)
+ .orElse(0l);
+ state.put(user, new AdderResult(value, sum + value));
+ }
+
+ public synchronized AdderResult calculate(String user)
+ {
+ if (!state.containsKey(user))
+ throw new IllegalStateException("No sumation for " + user + " in progress");
+
+ return state.remove(user);