1 package de.juplo.kafka;
4 import org.springframework.stereotype.Component;
6 import java.util.HashMap;
8 import java.util.Optional;
12 public class AdderBusinessLogic
14 private final Map<String, AdderResult> state;
17 public AdderBusinessLogic()
19 this(new HashMap<>());
22 public AdderBusinessLogic(Map<String, AdderResult> state)
28 public synchronized Optional<Long> getSum(String user)
30 return Optional.ofNullable(state.get(user)).map(result -> result.sum);
33 public synchronized void addToSum(String user, Integer value)
35 if (value == null || value < 1)
36 throw new IllegalArgumentException("Not a positive number: " + value);
40 .ofNullable(state.get(user))
41 .map(result -> result.sum)
43 state.put(user, new AdderResult(value, sum + value));
46 public synchronized AdderResult calculate(String user)
48 if (!state.containsKey(user))
49 throw new IllegalStateException("No sumation for " + user + " in progress");
51 return state.remove(user);