@Test public void testSubtract() throws Exception { ResourceProfile rp1 = new ResourceProfile(1.0, 100, 100, 100, 100, 100, Collections.emptyMap()); ResourceProfile rp2 = new ResourceProfile(2.0, 200, 200, 200, 200, 200, Collections.emptyMap()); ResourceProfile rp3 = new ResourceProfile(3.0, 300, 300, 300, 300, 300, Collections.emptyMap()); assertEquals(rp1, rp3.subtract(rp2)); assertEquals(rp1, rp2.subtract(rp1)); ResourceProfile rp4 = new ResourceProfile(Double.MAX_VALUE, 100, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Collections.emptyMap()); ResourceProfile rp5 = new ResourceProfile(Double.MAX_VALUE, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Collections.emptyMap()); assertEquals(rp5, rp4.subtract(rp1)); try { rp1.subtract(rp2); fail("The subtract should failed due to trying to subtract a larger resource"); } catch (IllegalArgumentException ex) { } assertEquals(ResourceProfile.ANY, ResourceProfile.ANY.subtract(rp3)); assertEquals(ResourceProfile.ANY, ResourceProfile.ANY.subtract(ResourceProfile.ANY)); assertEquals(ResourceProfile.ANY, rp3.subtract(ResourceProfile.ANY)); assertEquals(ResourceProfile.UNKNOWN, ResourceProfile.UNKNOWN.subtract(rp3)); assertEquals(ResourceProfile.UNKNOWN, rp3.subtract(ResourceProfile.UNKNOWN)); assertEquals(ResourceProfile.UNKNOWN, ResourceProfile.UNKNOWN.subtract(ResourceProfile.UNKNOWN)); }
public ResourceProfile subtract(ResourceProfile other) { if (equals(ANY) || other.equals(ANY)) { return ANY; } if (this.equals(UNKNOWN) || other.equals(UNKNOWN)) { return UNKNOWN; } checkArgument(isMatching(other), "Try to subtract an unmatched resource profile from this one."); Map<String, Resource> resultExtendedResource = new HashMap<>(extendedResources); other.extendedResources.forEach((String name, Resource resource) -> { resultExtendedResource.compute(name, (ignored, oldResource) -> { Resource resultResource = oldResource.subtract(resource); return resultResource.getValue() == 0 ? null : resultResource; }); }); return new ResourceProfile( subtractDoublesConsideringInf(cpuCores, other.cpuCores), subtractIntegersConsideringInf(heapMemoryInMB, other.heapMemoryInMB), subtractIntegersConsideringInf(directMemoryInMB, other.directMemoryInMB), subtractIntegersConsideringInf(nativeMemoryInMB, other.nativeMemoryInMB), subtractIntegersConsideringInf(networkMemoryInMB, other.networkMemoryInMB), subtractIntegersConsideringInf(managedMemoryInMB, other.managedMemoryInMB), resultExtendedResource ); }
ResourceProfile implements Serializable, Comparable<ResourceProfile> { public ResourceProfile subtract(ResourceProfile other) { if (equals(ANY) || other.equals(ANY)) { return ANY; } if (this.equals(UNKNOWN) || other.equals(UNKNOWN)) { return UNKNOWN; } checkArgument(isMatching(other), "Try to subtract an unmatched resource profile from this one."); Map<String, Resource> resultExtendedResource = new HashMap<>(extendedResources); other.extendedResources.forEach((String name, Resource resource) -> { resultExtendedResource.compute(name, (ignored, oldResource) -> { Resource resultResource = oldResource.subtract(resource); return resultResource.getValue() == 0 ? null : resultResource; }); }); return new ResourceProfile( subtractDoublesConsideringInf(cpuCores, other.cpuCores), subtractIntegersConsideringInf(heapMemoryInMB, other.heapMemoryInMB), subtractIntegersConsideringInf(directMemoryInMB, other.directMemoryInMB), subtractIntegersConsideringInf(nativeMemoryInMB, other.nativeMemoryInMB), subtractIntegersConsideringInf(networkMemoryInMB, other.networkMemoryInMB), subtractIntegersConsideringInf(managedMemoryInMB, other.managedMemoryInMB), resultExtendedResource ); } }
ResourceProfile implements Serializable, Comparable<ResourceProfile> { public ResourceProfile subtract(ResourceProfile other) { if (equals(ANY) || other.equals(ANY)) { return ANY; } if (this.equals(UNKNOWN) || other.equals(UNKNOWN)) { return UNKNOWN; } checkArgument(isMatching(other), "Try to subtract an unmatched resource profile from this one."); Map<String, Resource> resultExtendedResource = new HashMap<>(extendedResources); other.extendedResources.forEach((String name, Resource resource) -> { resultExtendedResource.compute(name, (ignored, oldResource) -> { Resource resultResource = oldResource.subtract(resource); return resultResource.getValue() == 0 ? null : resultResource; }); }); return new ResourceProfile( subtractDoublesConsideringInf(cpuCores, other.cpuCores), subtractIntegersConsideringInf(heapMemoryInMB, other.heapMemoryInMB), subtractIntegersConsideringInf(directMemoryInMB, other.directMemoryInMB), subtractIntegersConsideringInf(nativeMemoryInMB, other.nativeMemoryInMB), subtractIntegersConsideringInf(networkMemoryInMB, other.networkMemoryInMB), subtractIntegersConsideringInf(managedMemoryInMB, other.managedMemoryInMB), resultExtendedResource ); } ResourceProfile(
			double cpuCores,
			int heapMemoryInMB,
			int directMemoryInMB,
			int nativeMemoryInMB,
			int networkMemoryInMB,
			int managedMemoryInMB,
			Map<String, Resource> extendedResources);  ResourceProfile(double cpuCores, int heapMemoryInMB); private  ResourceProfile();  ResourceProfile(ResourceProfile other); }
ResourceProfile implements Serializable, Comparable<ResourceProfile> { public ResourceProfile subtract(ResourceProfile other) { if (equals(ANY) || other.equals(ANY)) { return ANY; } if (this.equals(UNKNOWN) || other.equals(UNKNOWN)) { return UNKNOWN; } checkArgument(isMatching(other), "Try to subtract an unmatched resource profile from this one."); Map<String, Resource> resultExtendedResource = new HashMap<>(extendedResources); other.extendedResources.forEach((String name, Resource resource) -> { resultExtendedResource.compute(name, (ignored, oldResource) -> { Resource resultResource = oldResource.subtract(resource); return resultResource.getValue() == 0 ? null : resultResource; }); }); return new ResourceProfile( subtractDoublesConsideringInf(cpuCores, other.cpuCores), subtractIntegersConsideringInf(heapMemoryInMB, other.heapMemoryInMB), subtractIntegersConsideringInf(directMemoryInMB, other.directMemoryInMB), subtractIntegersConsideringInf(nativeMemoryInMB, other.nativeMemoryInMB), subtractIntegersConsideringInf(networkMemoryInMB, other.networkMemoryInMB), subtractIntegersConsideringInf(managedMemoryInMB, other.managedMemoryInMB), resultExtendedResource ); } ResourceProfile(
			double cpuCores,
			int heapMemoryInMB,
			int directMemoryInMB,
			int nativeMemoryInMB,
			int networkMemoryInMB,
			int managedMemoryInMB,
			Map<String, Resource> extendedResources);  ResourceProfile(double cpuCores, int heapMemoryInMB); private  ResourceProfile();  ResourceProfile(ResourceProfile other); double getCpuCores(); int getHeapMemoryInMB(); int getDirectMemoryInMB(); int getNativeMemoryInMB(); int getNetworkMemoryInMB(); int getManagedMemoryInMB(); int getMemoryInMB(); int getOperatorsMemoryInMB(); Map<String, Resource> getExtendedResources(); boolean isMatching(ResourceProfile required); @Override int compareTo(@Nonnull ResourceProfile other); @Override int hashCode(); @Override boolean equals(Object obj); @Nonnull ResourceProfile merge(@Nonnull ResourceProfile other); ResourceProfile subtract(ResourceProfile other); @Override String toString(); static ResourceProfile fromResourceSpec(ResourceSpec resourceSpec, int networkMemory); }
ResourceProfile implements Serializable, Comparable<ResourceProfile> { public ResourceProfile subtract(ResourceProfile other) { if (equals(ANY) || other.equals(ANY)) { return ANY; } if (this.equals(UNKNOWN) || other.equals(UNKNOWN)) { return UNKNOWN; } checkArgument(isMatching(other), "Try to subtract an unmatched resource profile from this one."); Map<String, Resource> resultExtendedResource = new HashMap<>(extendedResources); other.extendedResources.forEach((String name, Resource resource) -> { resultExtendedResource.compute(name, (ignored, oldResource) -> { Resource resultResource = oldResource.subtract(resource); return resultResource.getValue() == 0 ? null : resultResource; }); }); return new ResourceProfile( subtractDoublesConsideringInf(cpuCores, other.cpuCores), subtractIntegersConsideringInf(heapMemoryInMB, other.heapMemoryInMB), subtractIntegersConsideringInf(directMemoryInMB, other.directMemoryInMB), subtractIntegersConsideringInf(nativeMemoryInMB, other.nativeMemoryInMB), subtractIntegersConsideringInf(networkMemoryInMB, other.networkMemoryInMB), subtractIntegersConsideringInf(managedMemoryInMB, other.managedMemoryInMB), resultExtendedResource ); } ResourceProfile(
			double cpuCores,
			int heapMemoryInMB,
			int directMemoryInMB,
			int nativeMemoryInMB,
			int networkMemoryInMB,
			int managedMemoryInMB,
			Map<String, Resource> extendedResources);  ResourceProfile(double cpuCores, int heapMemoryInMB); private  ResourceProfile();  ResourceProfile(ResourceProfile other); double getCpuCores(); int getHeapMemoryInMB(); int getDirectMemoryInMB(); int getNativeMemoryInMB(); int getNetworkMemoryInMB(); int getManagedMemoryInMB(); int getMemoryInMB(); int getOperatorsMemoryInMB(); Map<String, Resource> getExtendedResources(); boolean isMatching(ResourceProfile required); @Override int compareTo(@Nonnull ResourceProfile other); @Override int hashCode(); @Override boolean equals(Object obj); @Nonnull ResourceProfile merge(@Nonnull ResourceProfile other); ResourceProfile subtract(ResourceProfile other); @Override String toString(); static ResourceProfile fromResourceSpec(ResourceSpec resourceSpec, int networkMemory); static final ResourceProfile UNKNOWN; static final ResourceProfile ANY; static final ResourceProfile ZERO; }