-
Notifications
You must be signed in to change notification settings - Fork 276
/
ProductDemand.java
74 lines (55 loc) · 2.18 KB
/
ProductDemand.java
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
package io.dddbyexamples.factory.demand.forecasting;
import lombok.AllArgsConstructor;
import io.dddbyexamples.factory.product.management.RefNoId;
import java.time.Clock;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
@AllArgsConstructor
class ProductDemand {
final RefNoId id;
final List<DailyDemand.DemandUpdated> updates;
private final Demands demands;
private final Clock clock;
private final DemandEvents events;
interface Demands {
DailyDemand get(LocalDate date);
}
void adjust(AdjustDemand adjustDemand) {
LocalDate today = LocalDate.now(clock);
List<DailyDemand.Result> results = adjustDemand
.forEachStartingFrom(today, this::adjustDaily);
updates.addAll(DailyDemand.Result.updates(results));
Map<DailyId, DemandedLevelsChanged.Change> changes = DailyDemand.Result.levelChanges(results);
if (!changes.isEmpty()) {
events.emit(new DemandedLevelsChanged(id, changes));
}
}
void process(Document document) {
LocalDate today = LocalDate.now(clock);
List<DailyDemand.Result> results = document
.forEachStartingFrom(today, this::updateDaily);
updates.addAll(DailyDemand.Result.updates(results));
Map<DailyId, DemandedLevelsChanged.Change> changes = DailyDemand.Result.levelChanges(results);
if (!changes.isEmpty()) {
events.emit(new DemandedLevelsChanged(id, changes));
}
List<ReviewRequired.ToReview> reviews = DailyDemand.Result.reviews(results);
if (!reviews.isEmpty()) {
events.emit(new ReviewRequired(id, reviews));
}
}
void review(ApplyReviewDecision reviewDecision) {
if (reviewDecision.requireAdjustment()) {
adjust(reviewDecision.toAdjustment());
}
}
private DailyDemand.Result adjustDaily(LocalDate date, Adjustment adjustment) {
DailyDemand demand = demands.get(date);
return demand.adjust(adjustment);
}
private DailyDemand.Result updateDaily(LocalDate date, Demand demand) {
DailyDemand daily = demands.get(date);
return daily.update(demand);
}
}