-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeviceConfiguration.java
More file actions
96 lines (84 loc) · 3.26 KB
/
Copy pathDeviceConfiguration.java
File metadata and controls
96 lines (84 loc) · 3.26 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
85
86
87
88
89
90
91
92
93
94
95
96
package devices.configuration.management;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
@AllArgsConstructor
class DeviceConfiguration {
final String deviceId;
final List<DomainEvent> events;
private Ownership ownership;
private Location location;
private OpeningHours openingHours;
private Settings settings;
static DeviceConfiguration newDevice(String deviceId) {
DeviceConfiguration device =
new DeviceConfiguration(
deviceId,
new ArrayList<>(),
Ownership.unowned(),
null,
OpeningHours.alwaysOpened(),
Settings.defaultSettings());
device.events.add(new DomainEvent.DeviceCreated(deviceId));
return device;
}
void changeOwnership(Ownership newOwnership) {
Objects.requireNonNull(newOwnership, "Ownership cannot be null");
if (!Objects.equals(this.ownership, newOwnership)) {
this.ownership = newOwnership;
events.add(new DomainEvent.OwnershipChanged(deviceId, newOwnership));
if (newOwnership.isUnowned()) {
resetToDefaults();
}
}
}
void changeLocation(Location newLocation) {
if (!Objects.equals(this.location, newLocation)) {
this.location = newLocation;
events.add(new DomainEvent.LocationChanged(deviceId, newLocation));
events.add(new DomainEvent.DeviceConfigurationChanged(deviceId));
}
}
void changeOpeningHours(OpeningHours newOpeningHours) {
Objects.requireNonNull(newOpeningHours, "OpeningHours cannot be null");
if (!Objects.equals(this.openingHours, newOpeningHours)) {
this.openingHours = newOpeningHours;
events.add(new DomainEvent.OpeningHoursChanged(deviceId, newOpeningHours));
events.add(new DomainEvent.DeviceConfigurationChanged(deviceId));
}
}
void changeSettings(Settings newSettings) {
Objects.requireNonNull(newSettings, "Settings cannot be null");
if (!Objects.equals(this.settings, newSettings)) {
this.settings = newSettings;
events.add(new DomainEvent.SettingsChanged(deviceId, newSettings));
events.add(new DomainEvent.DeviceConfigurationChanged(deviceId));
}
}
private void resetToDefaults() {
changeLocation(null);
changeOpeningHours(OpeningHours.alwaysOpened());
changeSettings(Settings.defaultSettings());
}
private Violations checkViolations() {
return Violations.builder()
.operatorNotAssigned(ownership.isUnowned())
.providerNotAssigned(ownership.isUnowned())
.locationMissing(location == null)
.showOnMapButMissingLocation(settings.showOnMap() && location == null)
.showOnMapButNoPublicAccess(settings.showOnMap() && !settings.publicAccess())
.build();
}
private Visibility calculateVisibility() {
Violations violations = checkViolations();
return Visibility.calculateFrom(violations, settings.publicAccess(), settings.showOnMap());
}
DeviceConfigurationSnapshot toSnapshot() {
Violations violations = checkViolations();
Visibility visibility =
Visibility.calculateFrom(violations, settings.publicAccess(), settings.showOnMap());
return new DeviceConfigurationSnapshot(
deviceId, ownership, location, openingHours, settings, violations, visibility);
}
}