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 void startSum(String user)
27 if (state.containsKey(user))
28 throw new IllegalStateException("Sumation for " + user + " already in progress, state: " + state.get(user));
33 public synchronized Optional<Long> getSum(String user)
35 return Optional.ofNullable(state.get(user));
38 public synchronized void addToSum(String user, Integer value)
40 if (!state.containsKey(user))
41 throw new IllegalStateException("No sumation for " + user + " in progress");
42 if (value == null || value < 1)
43 throw new IllegalArgumentException("Not a positive number: " + value);
45 long result = state.get(user) + value;
46 state.put(user, result);
49 public synchronized Long endSum(String user)
51 if (!state.containsKey(user))
52 throw new IllegalStateException("No sumation for " + user + " in progress");
54 return state.remove(user);
57 protected Map<String, Long> getState()