1 package de.juplo.kafka;
3 import java.util.HashMap;
5 import java.util.Optional;
8 public class SumBusinessLogic
10 private final Map<String, Long> state;
13 public SumBusinessLogic()
15 this(new HashMap<>());
18 public SumBusinessLogic(Map<String, Long> state)
24 public synchronized void startSum(String user)
26 if (state.containsKey(user))
27 throw new IllegalStateException("Sumation for " + user + " already in progress, state: " + state.get(user));
32 public synchronized Optional<Long> getSum(String user)
34 return Optional.ofNullable(state.get(user));
37 public synchronized void addToSum(String user, Integer value)
39 if (!state.containsKey(user))
40 throw new IllegalStateException("No sumation for " + user + " in progress");
41 if (value == null || value < 1)
42 throw new IllegalArgumentException("Not a positive number: " + value);
44 long result = state.get(user) + value;
45 state.put(user, result);
48 public synchronized Long endSum(String user)
50 if (!state.containsKey(user))
51 throw new IllegalStateException("No sumation for " + user + " in progress");
53 return state.get(user);