private Notifications readNotifications(Element root) {
Element notificationsElement = XML.getChild(root, "notifications");
if (notificationsElement == null)
return Notifications.none();
When defaultWhen = stringAttribute("when", notificationsElement).map(When::fromValue).orElse(When.failingCommit);
Map<When, List<String>> emailAddresses = new HashMap<>();
Map<When, List<Role>> emailRoles = new HashMap<>();
for (When when : When.values()) {
emailAddresses.put(when, new ArrayList<>());
emailRoles.put(when, new ArrayList<>());
}
for (Element emailElement : XML.getChildren(notificationsElement, "email")) {
Optional<String> addressAttribute = stringAttribute("address", emailElement);
Optional<Role> roleAttribute = stringAttribute("role", emailElement).map(Role::fromValue);
When when = stringAttribute("when", emailElement).map(When::fromValue).orElse(defaultWhen);
if (addressAttribute.isPresent() == roleAttribute.isPresent())
throw new IllegalArgumentException("Exactly one of 'role' and 'address' must be present in 'email' elements.");
addressAttribute.ifPresent(address -> emailAddresses.get(when).add(address));
roleAttribute.ifPresent(role -> emailRoles.get(when).add(role));
}
return Notifications.of(emailAddresses, emailRoles);
}