private void validateResourceTypes(Iterable<String> resourceNames, List<ResourceTypeInfo> resourceTypes) { for (String resourceName : resourceNames) { if (!resourceTypes.stream().anyMatch(e -> e.getName().equals(resourceName))) { throw new ResourceNotFoundException("Unknown resource: " + resourceName); } } }
public static Resource multiplyTo(Resource lhs, double by) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation lhsValue = lhs.getResourceInformation(i); lhs.setResourceValue(i, (long) (lhsValue.getValue() * by)); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return lhs; }
protected void throwExceptionWhenArrayOutOfBound(int index) { String exceptionMsg = String.format( "Trying to access ResourceInformation for given index=%d. " + "Acceptable index range is [0,%d), please double check " + "configured resources in resource-types.xml", index, ResourceUtils.getNumberOfKnownResourceTypes()); throw new ResourceNotFoundException(exceptionMsg); }
public static Resource multiplyAndRoundDown(Resource lhs, double by) { Resource out = clone(lhs); int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation lhsValue = lhs.getResourceInformation(i); out.setResourceValue(i, (long) (lhsValue.getValue() * by)); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return out; }
/** * Set the percentage of the resource if this object is setup to use * percentages. If this object is set to use percentages, this method has * no effect. * * @param name the name of the resource * @param value the percentage */ void setPercentage(String name, double value) { if (percentages != null) { Integer index = ResourceUtils.getResourceTypeIndex().get(name); if (index != null) { percentages[index] = value; } else { throw new ResourceNotFoundException("The requested resource, \"" + name + "\", could not be found."); } } } }
public static Resource addTo(Resource lhs, Resource rhs) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation rhsValue = rhs.getResourceInformation(i); ResourceInformation lhsValue = lhs.getResourceInformation(i); lhs.setResourceValue(i, lhsValue.getValue() + rhsValue.getValue()); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return lhs; }
/** * Get ResourceInformation for a specified resource. * * @param resource name of the resource * @return the ResourceInformation object for the resource */ @Public @InterfaceStability.Unstable public ResourceInformation getResourceInformation(String resource) { Integer index = ResourceUtils.getResourceTypeIndex().get(resource); if (index != null) { return resources[index]; } throw new ResourceNotFoundException(this, resource); }
public static boolean fitsIn(Resource smaller, Resource bigger) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation rhsValue = bigger.getResourceInformation(i); ResourceInformation lhsValue = smaller.getResourceInformation(i); if (lhsValue.getValue() > rhsValue.getValue()) { return false; } } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return true; }
@Override public ResourceInformation[] getResources() { if (resources.length != ResourceUtils.getNumberOfKnownResourceTypes()) { // Retry once to reinitialize resource information. initResourceMap(); if (resources.length != ResourceUtils.getNumberOfKnownResourceTypes()) { throw new ResourceNotFoundException("Failed to reinitialize " + "FixedValueResource to get number of resource types same " + "as configured"); } } return resources; }
public static Resource subtractFrom(Resource lhs, Resource rhs) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation rhsValue = rhs.getResourceInformation(i); ResourceInformation lhsValue = lhs.getResourceInformation(i); lhs.setResourceValue(i, lhsValue.getValue() - rhsValue.getValue()); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return lhs; }
/** * Multiply {@code rhs} by {@code by}, and add the result to {@code lhs} * without creating any new {@link Resource} object */ public static Resource multiplyAndAddTo( Resource lhs, Resource rhs, double by) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation rhsValue = rhs.getResourceInformation(i); ResourceInformation lhsValue = lhs.getResourceInformation(i); long convertedRhs = (long) (rhsValue.getValue() * by); lhs.setResourceValue(i, lhsValue.getValue() + convertedRhs); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return lhs; }
public static Resource componentwiseMax(Resource lhs, Resource rhs) { Resource ret = createResource(0); int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation rhsValue = rhs.getResourceInformation(i); ResourceInformation lhsValue = lhs.getResourceInformation(i); ResourceInformation outInfo = lhsValue.getValue() > rhsValue.getValue() ? lhsValue : rhsValue; ret.setResourceInformation(i, outInfo); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return ret; }
public static Resource componentwiseMin(Resource lhs, Resource rhs) { Resource ret = createResource(0); int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation rhsValue = rhs.getResourceInformation(i); ResourceInformation lhsValue = lhs.getResourceInformation(i); ResourceInformation outInfo = lhsValue.getValue() < rhsValue.getValue() ? lhsValue : rhsValue; ret.setResourceInformation(i, outInfo); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return ret; }