@Override public <T> Collection<Resource> getAllocatedResources(Resource parent, Class<T> cls) { checkNotNull(parent); checkNotNull(cls); checkArgument(parent instanceof DiscreteResource); Versioned<Set<Resource>> children = childMap.get((DiscreteResource) parent); if (children == null) { return ImmutableList.of(); } Stream<DiscreteResource> discrete = children .value() .stream() .filter(x -> x.last().getClass().equals(cls)) .filter(x -> x instanceof DiscreteResource) .map(x -> (DiscreteResource) x) .filter(discreteConsumers::containsKey); Stream<ContinuousResource> continuous = children .value() .stream() .filter(x -> x.id().equals(parent.id().child(cls))) .filter(x -> x instanceof ContinuousResource) .map(x -> (ContinuousResource) x) .filter(x -> continuousConsumers.containsKey(x.id())) .filter(x -> continuousConsumers.get(x.id()) != null) .filter(x -> !continuousConsumers.get(x.id()).value().allocations().isEmpty()); return Stream.concat(discrete, continuous).collect(Collectors.toList()); }
@Override public boolean isAvailable(Resource resource) { checkNotNull(resource); checkArgument(resource instanceof DiscreteResource || resource instanceof ContinuousResource); // check if it's registered or not. Versioned<Set<Resource>> v = childMap.get(resource.parent().get()); if (v == null || !v.value().contains(resource)) { return false; } if (resource instanceof DiscreteResource) { // check if already consumed return getConsumers((DiscreteResource) resource).isEmpty(); } else { ContinuousResource requested = (ContinuousResource) resource; ContinuousResource registered = v.value() .stream() .filter(c -> c.id().equals(resource.id())) .findFirst() .map(c -> (ContinuousResource) c) .get(); if (registered.value() < requested.value()) { // Capacity < requested, can never satisfy return false; } // check if there's enough left return isAvailable(requested); } }