public ResourceProfile subtract(final ResourceProfile other) {
        checkNotNull(other, "Cannot subtract with null resources");

        if (equals(ANY) || other.equals(ANY)) {
            return ANY;
        }

        if (this.equals(UNKNOWN) || other.equals(UNKNOWN)) {
            return UNKNOWN;
        }

        checkArgument(
                allFieldsNoLessThan(other),
                "Try to subtract an unmatched resource profile from this one.");

        Map<String, ExternalResource> resultExtendedResource = new HashMap<>(extendedResources);

        other.extendedResources.forEach(
                (String name, ExternalResource resource) ->
                        resultExtendedResource.compute(
                                name, (ignored, oldResource) -> oldResource.subtract(resource)));

        return new ResourceProfile(
                cpuCores.subtract(other.cpuCores),
                taskHeapMemory.subtract(other.taskHeapMemory),
                taskOffHeapMemory.subtract(other.taskOffHeapMemory),
                managedMemory.subtract(other.managedMemory),
                networkMemory.subtract(other.networkMemory),
                resultExtendedResource);
    }
@Test
    void testSubtract() {
        final ResourceProfile rp1 =
                ResourceProfile.newBuilder()
                        .setCpuCores(1.0)
                        .setTaskHeapMemoryMB(100)
                        .setTaskOffHeapMemoryMB(100)
                        .setManagedMemoryMB(100)
                        .setNetworkMemoryMB(100)
                        .build();
        final ResourceProfile rp2 =
                ResourceProfile.newBuilder()
                        .setCpuCores(2.0)
                        .setTaskHeapMemoryMB(200)
                        .setTaskOffHeapMemoryMB(200)
                        .setManagedMemoryMB(200)
                        .setNetworkMemoryMB(200)
                        .build();
        final ResourceProfile rp3 =
                ResourceProfile.newBuilder()
                        .setCpuCores(3.0)
                        .setTaskHeapMemoryMB(300)
                        .setTaskOffHeapMemoryMB(300)
                        .setManagedMemoryMB(300)
                        .setNetworkMemoryMB(300)
                        .build();

        assertThat(rp3.subtract(rp2)).isEqualTo(rp1);
        assertThat(rp2.subtract(rp1)).isEqualTo(rp1);

        assertThatExceptionOfType(IllegalArgumentException.class)
                .as("The subtract should failed due to trying to subtract a larger resource")
                .isThrownBy(() -> rp1.subtract(rp2));

        assertThat(ResourceProfile.ANY.subtract(rp3)).isEqualTo(ResourceProfile.ANY);
        assertThat(ResourceProfile.ANY.subtract(ResourceProfile.ANY))
                .isEqualTo(ResourceProfile.ANY);
        assertThat(rp3.subtract(ResourceProfile.ANY)).isEqualTo(ResourceProfile.ANY);

        assertThat(ResourceProfile.UNKNOWN.subtract(rp3)).isEqualTo(ResourceProfile.UNKNOWN);
        assertThat(rp3.subtract(ResourceProfile.UNKNOWN)).isEqualTo(ResourceProfile.UNKNOWN);
        assertThat(ResourceProfile.UNKNOWN.subtract(ResourceProfile.UNKNOWN))
                .isEqualTo(ResourceProfile.UNKNOWN);
    }