@Nonnull
    public ResourceProfile merge(final ResourceProfile other) {
        checkNotNull(other, "Cannot merge with null resources");

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

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

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

        other.extendedResources.forEach(
                (String name, ExternalResource resource) -> {
                    resultExtendedResource.compute(
                            name,
                            (ignored, oldResource) ->
                                    oldResource == null ? resource : oldResource.merge(resource));
                });

        return new ResourceProfile(
                cpuCores.merge(other.cpuCores),
                taskHeapMemory.add(other.taskHeapMemory),
                taskOffHeapMemory.add(other.taskOffHeapMemory),
                managedMemory.add(other.managedMemory),
                networkMemory.add(other.networkMemory),
                resultExtendedResource);
    }
@Test
    void testMerge() {
        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)
                        .setExtendedResource(new ExternalResource(EXTERNAL_RESOURCE_NAME, 2.0))
                        .build();

        final ResourceProfile rp1MergeRp1 =
                ResourceProfile.newBuilder()
                        .setCpuCores(2.0)
                        .setTaskHeapMemoryMB(200)
                        .setTaskOffHeapMemoryMB(200)
                        .setManagedMemoryMB(200)
                        .setNetworkMemoryMB(200)
                        .build();
        final ResourceProfile rp1MergeRp2 =
                ResourceProfile.newBuilder()
                        .setCpuCores(3.0)
                        .setTaskHeapMemoryMB(300)
                        .setTaskOffHeapMemoryMB(300)
                        .setManagedMemoryMB(300)
                        .setNetworkMemoryMB(300)
                        .setExtendedResource(new ExternalResource(EXTERNAL_RESOURCE_NAME, 2.0))
                        .build();
        final ResourceProfile rp2MergeRp2 =
                ResourceProfile.newBuilder()
                        .setCpuCores(4.0)
                        .setTaskHeapMemoryMB(400)
                        .setTaskOffHeapMemoryMB(400)
                        .setManagedMemoryMB(400)
                        .setNetworkMemoryMB(400)
                        .setExtendedResource(new ExternalResource(EXTERNAL_RESOURCE_NAME, 4.0))
                        .build();

        assertThat(rp1.merge(rp1)).isEqualTo(rp1MergeRp1);
        assertThat(rp1.merge(rp2)).isEqualTo(rp1MergeRp2);
        assertThat(rp2.merge(rp1)).isEqualTo(rp1MergeRp2);
        assertThat(rp2.merge(rp2)).isEqualTo(rp2MergeRp2);

        assertThat(rp1.merge(ResourceProfile.UNKNOWN)).isEqualTo(ResourceProfile.UNKNOWN);
        assertThat(ResourceProfile.UNKNOWN.merge(rp1)).isEqualTo(ResourceProfile.UNKNOWN);
        assertThat(ResourceProfile.UNKNOWN.merge(ResourceProfile.UNKNOWN))
                .isEqualTo(ResourceProfile.UNKNOWN);
        assertThat(rp1.merge(ResourceProfile.ANY)).isEqualTo(ResourceProfile.ANY);
        assertThat(ResourceProfile.ANY.merge(rp1)).isEqualTo(ResourceProfile.ANY);
        assertThat(ResourceProfile.ANY.merge(ResourceProfile.ANY)).isEqualTo(ResourceProfile.ANY);
    }