class OrganizationResourcesDistributor {

  @VisibleForTesting
  void checkResourcesAvailability(
      String suborganizationId, List<? extends Resource> newResourcesCap)
      throws NotFoundException, ConflictException, ServerException {
    Map<String, Resource> usedResources =
        resourceManager
            .getUsedResources(suborganizationId)
            .stream()
            .collect(Collectors.toMap(Resource::getType, Function.identity()));
    for (Resource resourceToCheck : newResourcesCap) {
      Resource usedResource = usedResources.get(resourceToCheck.getType());
      if (usedResource != null) {
        try {
          resourceAggregator.deduct(resourceToCheck, usedResource);
        } catch (NoEnoughResourcesException e) {
          throw new ConflictException(
              "Resources are currently in use. "
                  + getMessage(e.getMissingResources().get(0).getType()));
        }
      }
    }
  }

  @Inject public  OrganizationResourcesDistributor(
      OrganizationDistributedResourcesDao organizationDistributedResourcesDao,
      OrganizationManager organizationManager,
      ResourcesLocks resourcesLocks,
      ResourceManager resourceManager,
      ResourceAggregator resourceAggregator);

}