use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.
the class ResourceAggregator method excess.
/**
* Returns list which contains resources from specified {@code sourceResources} which have
* excessive amount in compare to specified {@code resourcesToCompare}.
*
* <p>Example :
*
* <pre>
* | \ | Source | To compare| Result |
* |:------------|:----------|:----------|:---------|
* | Resource1 | 5 | 3 | 2 |
* | ----------- | --------- | --------- | -------- |
* | Resource2 | - | 9 | - |
* | ----------- | --------- | --------- | -------- |
* | Resource3 | 1 | - | 1 |
* | ----------- | --------- | --------- | -------- |
* </pre>
*
* @param sourceResources the source resources
* @param resourcesToCompare the resources which should be compared to {@code sourceResources}
* @throws IllegalArgumentException when {@code sourceResources} or {@code resourcesToCompare}
* contain resource with not supported type
*/
public List<? extends Resource> excess(List<? extends Resource> sourceResources, List<? extends Resource> resourcesToCompare) {
checkSupporting(sourceResources);
checkSupporting(resourcesToCompare);
final Map<String, Resource> result = sourceResources.stream().collect(Collectors.toMap(Resource::getType, Function.identity()));
for (Resource toCompare : resourcesToCompare) {
String resourceType = toCompare.getType();
final Resource sourceResource = result.get(resourceType);
if (sourceResource != null) {
if (sourceResource.getAmount() == toCompare.getAmount()) {
// source resource doesn't have excessive amount
result.remove(resourceType);
continue;
}
try {
Resource excess = deduct(sourceResource, toCompare);
if (excess.getAmount() == 0) {
// source resource doesn't have excessive amount
result.remove(resourceType);
} else {
result.put(resourceType, excess);
}
} catch (NoEnoughResourcesException e) {
// source resource doesn't have excessive amount
result.remove(resourceType);
}
}
}
return new ArrayList<>(result.values());
}
use of org.eclipse.che.multiuser.resource.model.Resource in project devspaces-images by redhat-developer.
the class ResourceAggregator method aggregateByType.
/**
* Aggregates resources of the same type.
*
* @param resources resources list which can contain more that one instance for some type
* @return map where key is resources type and value is aggregated resource
* @throws IllegalArgumentException when resources list contains resource with not supported type
*/
public Map<String, Resource> aggregateByType(List<? extends Resource> resources) {
checkSupporting(resources);
Map<String, Resource> type2Resource = new HashMap<>();
for (Resource resource : resources) {
final Resource resource1 = type2Resource.get(resource.getType());
if (resource1 != null) {
type2Resource.put(resource.getType(), aggregate(resource1, resource));
} else {
type2Resource.put(resource.getType(), resource);
}
}
return type2Resource;
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class AbstractExhaustibleResourceTest method shouldReturnResourceWithMinusOneAmountWhenFirstResourceHasMinusOneAmountOnResourcesDeduction.
@Test
public void shouldReturnResourceWithMinusOneAmountWhenFirstResourceHasMinusOneAmountOnResourcesDeduction() throws Exception {
final Resource aggregate = resourceType.aggregate(new ResourceImpl(TestResourceType.ID, -1, TestResourceType.UNIT), new ResourceImpl(TestResourceType.ID, 500, TestResourceType.UNIT));
assertEquals(aggregate.getType(), TestResourceType.ID);
assertEquals(aggregate.getAmount(), -1);
assertEquals(aggregate.getUnit(), TestResourceType.UNIT);
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class AbstractExhaustibleResourceTest method shouldReturnResourceWithMinusOneAmountWhenSecondResourceHasMinusOneAmountOnResourcesDeduction.
@Test
public void shouldReturnResourceWithMinusOneAmountWhenSecondResourceHasMinusOneAmountOnResourcesDeduction() throws Exception {
final Resource aggregate = resourceType.aggregate(new ResourceImpl(TestResourceType.ID, 2000, TestResourceType.UNIT), new ResourceImpl(TestResourceType.ID, -1, TestResourceType.UNIT));
assertEquals(aggregate.getType(), TestResourceType.ID);
assertEquals(aggregate.getAmount(), -1);
assertEquals(aggregate.getUnit(), TestResourceType.UNIT);
}
use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.
the class AbstractExhaustibleResourceTest method shouldFindSumResourcesAmountsOnResourcesAggregation.
@Test
public void shouldFindSumResourcesAmountsOnResourcesAggregation() throws Exception {
final Resource aggregate = resourceType.aggregate(new ResourceImpl(TestResourceType.ID, 1000, TestResourceType.UNIT), new ResourceImpl(TestResourceType.ID, 500, TestResourceType.UNIT));
assertEquals(aggregate.getType(), TestResourceType.ID);
assertEquals(aggregate.getAmount(), 1500);
assertEquals(aggregate.getUnit(), TestResourceType.UNIT);
}
Aggregations