public class AdderBusinessLogic
{
- private final Map<String, Long> state;
+ private final Map<String, AdderResult> state;
public AdderBusinessLogic()
this(new HashMap<>());
}
- public AdderBusinessLogic(Map<String, Long> state)
+ public AdderBusinessLogic(Map<String, AdderResult> state)
{
this.state = state;
}
public synchronized Optional<Long> getSum(String user)
{
- return Optional.ofNullable(state.get(user));
+ return Optional.ofNullable(state.get(user)).map(result -> result.sum);
}
public synchronized void addToSum(String user, Integer value)
long sum =
Optional
.ofNullable(state.get(user))
+ .map(result -> result.sum)
.orElse(0l);
- state.put(user, sum + value);
+ 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 new AdderResult(66, state.remove(user));
+ return state.remove(user);
}
- protected Map<String, Long> getState()
+ protected Map<String, AdderResult> getState()
{
return state;
}
state.get(partition).addToSum(user, Integer.parseInt(message));
}
- protected void addPartition(Integer partition, Map<String, Long> state)
+ protected void addPartition(Integer partition, Map<String, AdderResult> state)
{
this.state.put(partition, new AdderBusinessLogic(state));
}
- protected Map<String, Long> removePartition(Integer partition)
+ protected Map<String, AdderResult> removePartition(Integer partition)
{
return this.state.remove(partition).getState();
}
@Id
public String id;
public long offset = -1l;
- public Map<String, Long> state;
+ public Map<String, AdderResult> state;
public StateDocument()
{
public StateDocument(
Integer partition,
- Map<String, Long> state,
+ Map<String, AdderResult> state,
long offset)
{
this.id = Integer.toString(partition);