class ResourceProfile implements Serializable {

	@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, Resource> resultExtendedResource = new HashMap<>(extendedResources);

		other.extendedResources.forEach((String name, Resource 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);
	}

	private  ResourceProfile(
			final Resource cpuCores,
			final MemorySize taskHeapMemory,
			final MemorySize taskOffHeapMemory,
			final MemorySize managedMemory,
			final MemorySize networkMemory,
			final Map<String, Resource> extendedResources);
	private  ResourceProfile();

}