ResourceAggregator { public List<? extends Resource> min(Collection<? extends Resource> resources) { checkSupporting(resources); Map<String, Resource> result = new HashMap<>(); for (Resource resource : resources) { String type = resource.getType(); Resource min = result.get(type); if (min == null) { result.put(type, resource); } else if (resource.getAmount() != -1) { if (min.getAmount() == -1 || min.getAmount() > resource.getAmount()) { result.put(type, resource); } } } return new ArrayList<>(result.values()); } @Inject  ResourceAggregator(Set<ResourceType> resourcesTypes); Map<String, Resource> aggregateByType(List<? extends Resource> resources); List<? extends Resource> deduct(
      List<? extends Resource> sourceResources, List<? extends Resource> resourcesToDeduct); List<? extends Resource> excess(
      List<? extends Resource> sourceResources, List<? extends Resource> resourcesToCompare); Resource aggregate(Resource resourceA, Resource resourceB); Resource deduct(Resource totalResource, Resource deduction); List<? extends Resource> intersection(
      List<? extends Resource> resourcesA, List<? extends Resource> resourcesB); List<? extends Resource> min(Collection<? extends Resource> resources);  }
@Test public void shouldReturnMinResources() throws Exception { final ResourceImpl aResource = new ResourceImpl(A_RESOURCE_TYPE, 100, "unit"); final ResourceImpl bResource = new ResourceImpl(B_RESOURCE_TYPE, 1000, "unit"); final ResourceImpl minBResource = new ResourceImpl(B_RESOURCE_TYPE, 500, "unit"); final ResourceImpl anotherBResource = new ResourceImpl(B_RESOURCE_TYPE, 2000, "unit"); List<? extends Resource> min = resourceAggregator.min(asList(aResource, bResource, minBResource, anotherBResource)); assertEquals(min.size(), 2); assertTrue(min.contains(aResource)); assertTrue(min.contains(minBResource)); }
@Test public void shouldReturnMinResourcesWhenTheyContainsMinusOneValue() throws Exception { final ResourceImpl aResource = new ResourceImpl(A_RESOURCE_TYPE, -1, "unit"); final ResourceImpl bResource = new ResourceImpl(B_RESOURCE_TYPE, -1, "unit"); final ResourceImpl minAResource = new ResourceImpl(A_RESOURCE_TYPE, 250, "unit"); final ResourceImpl minBResource = new ResourceImpl(B_RESOURCE_TYPE, 500, "unit"); List<? extends Resource> min = resourceAggregator.min(asList(aResource, minAResource, minBResource, bResource)); assertEquals(min.size(), 2); assertTrue(min.contains(minAResource)); assertTrue(min.contains(minBResource)); }
@Test(expectedExceptions = IllegalArgumentException.class) public void shouldThrowIllegalArgumentExceptionWhenListContainsResourceWithUnsupportedTypeOnMinFinding() throws Exception { final ResourceImpl dResource = mock(ResourceImpl.class); when(dResource.getType()).thenReturn("resourceD"); resourceAggregator.min(singletonList(dResource)); }