Source:
public Collection<Container> addContainers(String accountName, Collection<Container> containers) { checkOpen(); if (accountName == null || accountName.isEmpty() || containers == null || containers.isEmpty()) { throw new IllegalArgumentException("Account or container is null or empty"); } Account account = getAccountByName(accountName); if (account == null) { logger.error("Account is not found", accountName); throw new IllegalArgumentException("Account " + accountName + " is not found"); } Set<Container> existingContainers = new HashSet<>(); Set<Container> newContainers = new HashSet<>(containers); for (Container existingContainer : account.getAllContainers()) { for (Container newContainer : containers) { if (existingContainer.getName().equals(newContainer.getName())) { if (existingContainer.isSameContainer(newContainer)) { existingContainers.add(existingContainer); } else { throw new IllegalArgumentException("There is a conflicting container in account " + accountName); } } } } newContainers.removeAll(existingContainers); short nextContainerId = account.getAllContainers() .stream() .map(Container::getId) .max(Short::compareTo) .map(maxId -> (short) (maxId + 1)) .orElse(config.containerIdStartNumber); List<Container> createdContainers = new ArrayList<>(); for (Container container : newContainers) { createdContainers.add( new ContainerBuilder(container).setId(nextContainerId).setParentAccountId(account.getId()).build()); ++nextContainerId; } Account accountCopy = new AccountBuilder(account).build(); accountCopy.updateContainerMap(createdContainers); boolean hasSucceeded = updateAccounts(Collections.singletonList(accountCopy)); if (!hasSucceeded) { throw new IllegalStateException("Account update failed for " + accountName); } account.updateContainerMap(createdContainers); List<Container> result = new ArrayList<>(existingContainers); result.addAll(createdContainers); return result; }

Target:
public Collection<Container> addContainers(String accountName, Collection<Container> containers) throws AccountServiceException { checkOpen(); if (accountName == null || accountName.isEmpty() || containers == null || containers.isEmpty()) { throw new AccountServiceException("Account or container is null or empty", AccountServiceErrorCode.BadRequest); } Account account = getAccountByName(accountName); if (account == null) { logger.error("Account is not found", accountName); throw new AccountServiceException("Account " + accountName + " is not found", AccountServiceErrorCode.NotFound); } Set<Container> existingContainers = new HashSet<>(); Set<Container> newContainers = new HashSet<>(containers); Map<String, Container> existingContainersInAccount = new HashMap<>(); account.getAllContainers().forEach(c -> existingContainersInAccount.put(c.getName(), c)); for (Container container : containers) { Container existingContainer = existingContainersInAccount.get(container.getName()); if (existingContainer != null) { if (existingContainer.isSameContainer(container)) { existingContainers.add(container); } else { throw new AccountServiceException("There is a conflicting container in account " + accountName, AccountServiceErrorCode.ResourceConflict); } } } newContainers.removeAll(existingContainers); List<Container> createdContainers = new ArrayList<>(); if (!newContainers.isEmpty()) { short nextContainerId = account.getAllContainers() .stream() .map(Container::getId) .max(Short::compareTo) .map(maxId -> (short) (maxId + 1)) .orElse(config.containerIdStartNumber); for (Container container : newContainers) { createdContainers.add( new ContainerBuilder(container).setId(nextContainerId).setParentAccountId(account.getId()).build()); ++nextContainerId; } Account accountCopy = new AccountBuilder(account).build(); accountCopy.updateContainerMap(createdContainers); boolean hasSucceeded = updateAccounts(Collections.singletonList(accountCopy)); if (!hasSucceeded) { throw new AccountServiceException("Account update failed for " + accountName, AccountServiceErrorCode.AccountUpdateError); } account.updateContainerMap(createdContainers); } return createdContainers; }