Sho SHIMIZU
Committed by Gerrit Code Review

Refactor: Move logic for continuous resource to another method

Change-Id: I04136c933398891bf347f14874e93825ce3f2e33
...@@ -353,29 +353,29 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -353,29 +353,29 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
353 // check if already consumed 353 // check if already consumed
354 return getResourceAllocations(resource.id()).isEmpty(); 354 return getResourceAllocations(resource.id()).isEmpty();
355 } else { 355 } else {
356 + return isAvailable((ContinuousResource) resource);
357 + }
358 + }
359 +
360 + // computational complexity: O(n) where n is the number of existing allocations for the resource
361 + private boolean isAvailable(ContinuousResource resource) {
356 // check if it's registered or not. 362 // check if it's registered or not.
357 - Versioned<Set<Resource>> v = childMap.get(resource.parent().get().id()); 363 + Versioned<Set<Resource>> children = childMap.get(resource.parent().get().id());
358 - if (v == null) { 364 + if (children == null) {
359 return false; 365 return false;
360 } 366 }
361 367
362 - ContinuousResource requested = (ContinuousResource) resource; 368 + ContinuousResource registered = children.value().stream()
363 - ContinuousResource registered = v.value().stream()
364 .filter(c -> c.id().equals(resource.id())) 369 .filter(c -> c.id().equals(resource.id()))
365 .findFirst() 370 .findFirst()
366 .map(c -> (ContinuousResource) c) 371 .map(c -> (ContinuousResource) c)
367 .get(); 372 .get();
368 - if (registered.value() < requested.value()) { 373 + if (registered.value() < resource.value()) {
369 // Capacity < requested, can never satisfy 374 // Capacity < requested, can never satisfy
370 return false; 375 return false;
371 } 376 }
372 - // check if there's enough left
373 - return isAvailable(requested);
374 - }
375 - }
376 377
377 - // computational complexity: O(n) where n is the number of existing allocations for the resource 378 + // check if there's enough left
378 - private boolean isAvailable(ContinuousResource resource) {
379 Versioned<ContinuousResourceAllocation> allocation = continuousConsumers.get(resource.id()); 379 Versioned<ContinuousResourceAllocation> allocation = continuousConsumers.get(resource.id());
380 if (allocation == null) { 380 if (allocation == null) {
381 // no allocation (=no consumer) full registered resources available 381 // no allocation (=no consumer) full registered resources available
......