private static Project addBranchChange(Collection<EventComponentChangeDto> changes) {
if (changes.size() != 2) {
throw new IllegalStateException(format("Too many changes on same project (%d) for eventComponentChange uuids : %s",
changes.size(),
changes.stream().map(EventComponentChangeDto::getUuid).collect(Collectors.joining(","))));
}
Optional<EventComponentChangeDto> addedChange = changes.stream().filter(c -> c.getCategory().equals(EventComponentChangeDto.ChangeCategory.ADDED)).findFirst();
Optional<EventComponentChangeDto> removedChange = changes.stream().filter(c -> c.getCategory().equals(EventComponentChangeDto.ChangeCategory.REMOVED)).findFirst();
if (!addedChange.isPresent() || !removedChange.isPresent() || addedChange.equals(removedChange)) {
Iterator<EventComponentChangeDto> iterator = changes.iterator();
EventComponentChangeDto firstChange = iterator.next();
EventComponentChangeDto secondChange = iterator.next();
throw new IllegalStateException(format("Incorrect changes : [uuid=%s change=%s, branch=%s] and [uuid=%s, change=%s, branch=%s]",
firstChange.getUuid(), firstChange.getCategory().name(), firstChange.getComponentBranchKey(),
secondChange.getUuid(), secondChange.getCategory().name(), secondChange.getComponentBranchKey()));
}
return new Project()
.setName(addedChange.get().getComponentName())
.setKey(addedChange.get().getComponentKey())
.setChangeType("BRANCH_CHANGED")
.setNewBranch(addedChange.get().getComponentBranchKey())
.setOldBranch(removedChange.get().getComponentBranchKey());
}