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