Search in sources :

Example 36 with Resource

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());
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList)

Example 37 with Resource

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;
}
Also used : HashMap(java.util.HashMap) Resource(org.eclipse.che.multiuser.resource.model.Resource)

Example 38 with Resource

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);
}
Also used : ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Example 39 with Resource

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);
}
Also used : ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Example 40 with Resource

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);
}
Also used : ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Aggregations

Resource (org.eclipse.che.multiuser.resource.model.Resource)52 ResourceImpl (org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)24 Test (org.testng.annotations.Test)24 ArrayList (java.util.ArrayList)14 NotFoundException (org.eclipse.che.api.core.NotFoundException)12 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)12 Optional (java.util.Optional)10 Inject (javax.inject.Inject)10 Singleton (javax.inject.Singleton)10 AccountManager (org.eclipse.che.account.api.AccountManager)10 Account (org.eclipse.che.account.shared.model.Account)10 ServerException (org.eclipse.che.api.core.ServerException)10 ResourceUsageTracker (org.eclipse.che.multiuser.resource.api.ResourceUsageTracker)10 NoEnoughResourcesException (org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException)10 List (java.util.List)8 WorkspaceManager (org.eclipse.che.api.workspace.server.WorkspaceManager)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 HashMap (java.util.HashMap)6 Collectors (java.util.stream.Collectors)6 Provider (javax.inject.Provider)6