-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArmy.java
More file actions
84 lines (72 loc) · 3.15 KB
/
Army.java
File metadata and controls
84 lines (72 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.dddheroes.heroesofddd.armies.write;
import com.dddheroes.heroesofddd.armies.write.addcreature.AddCreatureToArmy;
import com.dddheroes.heroesofddd.armies.events.CreatureAddedToArmy;
import com.dddheroes.heroesofddd.armies.write.addcreature.CanHaveMax7CreatureStacksInArmy;
import com.dddheroes.heroesofddd.armies.events.CreatureRemovedFromArmy;
import com.dddheroes.heroesofddd.armies.write.removecreature.CanRemoveOnlyPresentCreatures;
import com.dddheroes.heroesofddd.armies.write.removecreature.RemoveCreatureFromArmy;
import com.dddheroes.heroesofddd.shared.domain.valueobjects.Amount;
import com.dddheroes.heroesofddd.shared.domain.identifiers.ArmyId;
import com.dddheroes.heroesofddd.shared.domain.identifiers.CreatureId;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.modelling.command.AggregateCreationPolicy;
import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.modelling.command.CreationPolicy;
import org.axonframework.spring.stereotype.Aggregate;
import java.util.HashMap;
import java.util.Map;
import static org.axonframework.modelling.command.AggregateLifecycle.apply;
// todo: probably we should model events ArmyEstablished and ArmyDestroyed, more on that on Event Model
@Aggregate
class Army {
@AggregateIdentifier
private ArmyId armyId;
private final Map<CreatureId, Amount> creatureStacks = new HashMap<>();
@CommandHandler
@CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING) // performance downside in comparison to constructor
void decide(AddCreatureToArmy command) {
new CanHaveMax7CreatureStacksInArmy(command.creatureId(), creatureStacks).verify();
apply(
CreatureAddedToArmy.event(
command.armyId(),
command.creatureId(),
command.quantity()
)
);
}
@EventSourcingHandler
void evolve(CreatureAddedToArmy event) {
this.armyId = new ArmyId(event.armyId());
creatureStacks.merge(new CreatureId(event.creatureId()), new Amount(event.quantity()), Amount::plus);
}
@CommandHandler
void decide(RemoveCreatureFromArmy command) {
new CanRemoveOnlyPresentCreatures(command.creatureId(), command.quantity(), creatureStacks).verify();
apply(
CreatureRemovedFromArmy.event(
command.armyId(),
command.creatureId(),
command.quantity()
)
);
}
@EventSourcingHandler
void evolve(CreatureRemovedFromArmy event) {
var creatureId = new CreatureId(event.creatureId());
var currentQuantity = creatureStacks.get(creatureId);
var removedQuantity = new Amount(event.quantity());
if (currentQuantity.equals(removedQuantity)) {
creatureStacks.remove(creatureId);
} else {
creatureStacks.merge(
creatureId,
removedQuantity,
Amount::minus
);
}
}
Army() {
// required by Axon
}
}