* Neue Erwartungen an `AdderBusinessLogic` implementiert.
* Die Implementierung hat sich über die nicht von den Unit-Tests
abgedeckte Methode auch auf andere Teile der Anwendung ausgewirkt.
* `AdderBusinessLogic.getState()` liefert jetzt in der Map die neue
Klasse `AdderResult` und benötigt diese auch als Konstruktor-Argument.
* Über die Integration-Tests ist sichergestellt, dass die Datenhaltung
trotz der Umstellung von `Long` auf `AdderResult` funktioniert.
public class AdderBusinessLogic
{
public class AdderBusinessLogic
{
- private final Map<String, Long> state;
+ private final Map<String, AdderResult> state;
public AdderBusinessLogic()
public AdderBusinessLogic()
- public AdderBusinessLogic(Map<String, Long> state)
+ public AdderBusinessLogic(Map<String, AdderResult> state)
public synchronized Optional<Long> getSum(String user)
{
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)
}
public synchronized void addToSum(String user, Integer value)
long sum =
Optional
.ofNullable(state.get(user))
long sum =
Optional
.ofNullable(state.get(user))
+ .map(result -> result.sum)
- state.put(user, sum + value);
+ state.put(user, new AdderResult(value, sum + value));
}
public synchronized AdderResult calculate(String user)
}
public synchronized AdderResult calculate(String user)
if (!state.containsKey(user))
throw new IllegalStateException("No sumation for " + user + " in progress");
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()
offset);
if (commitsEnabled)
{
offset);
if (commitsEnabled)
{
- Map<String, Long> removed = recordHandler.removePartition(partition);
+ Map<String, AdderResult> removed = recordHandler.removePartition(partition);
stateRepository.save(new StateDocument(partition, removed, offset));
}
else
stateRepository.save(new StateDocument(partition, removed, offset));
}
else
state.get(partition).addToSum(user, Integer.parseInt(message));
}
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));
}
{
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();
}
{
return this.state.remove(partition).getState();
}
- public Map<Integer, Map<String, Long>> state()
+ public Map<Integer, Map<String, AdderResult>> state()
@Id
public String id;
public long offset = -1l;
@Id
public String id;
public long offset = -1l;
- public Map<String, Long> state;
+ public Map<String, AdderResult> state;
public StateDocument(
Integer partition,
public StateDocument(
Integer partition,
- Map<String, Long> state,
+ Map<String, AdderResult> state,
long offset)
{
this.id = Integer.toString(partition);
long offset)
{
this.id = Integer.toString(partition);