class NodeGroupUtils {

    public static NodeGroupBroadcastResult toBroadcastResult(NodeGroupBroadcastResponse response) {
        NodeGroupBroadcastResult result = new NodeGroupBroadcastResult();

        result.totalNodeCount = response.nodeCount;
        result.availableNodeCount = response.availableNodeCount;
        result.unavailableNodeCount = response.nodeCount - response.availableNodeCount;
        result.membershipQuorum = response.membershipQuorum;


        // entry example: "http://127.0.0.1:8000" => "host1"
        Map<URI, String> hostIdByUrl = response.selectedNodes.entrySet().stream()
                .collect(toMap(entry -> {
                    URI uri = entry.getValue();
                    return URI.create(uri.toString().replace(uri.getPath(), ""));
                }, Map.Entry::getKey));

        // for successful responses
        for (Entry<URI, String> entry : response.jsonResponses.entrySet()) {
            PeerNodeResult singleResult = new PeerNodeResult();
            URI requestUri = entry.getKey();
            String json = entry.getValue();

            URI hostUri = URI.create(requestUri.toString().replace(requestUri.getPath(), ""));
            String hostId = hostIdByUrl.get(hostUri);
            URI nodeGroupUri = response.selectedNodes.get(hostId);

            singleResult.requestUri = requestUri;
            singleResult.hostId = hostId;
            singleResult.nodeGroupUri = nodeGroupUri;
            singleResult.json = json;
            singleResult.errorResponse = null;

            result.allResponses.add(singleResult);
            result.successResponses.add(singleResult);
        }

        // for failure responses
        for (Entry<URI, ServiceErrorResponse> entry : response.failures.entrySet()) {
            PeerNodeResult singleResult = new PeerNodeResult();
            URI requestUri = entry.getKey();
            ServiceErrorResponse errorResponse = entry.getValue();

            URI hostUri = URI.create(requestUri.toString().replace(requestUri.getPath(), ""));
            String hostId = hostIdByUrl.get(hostUri);
            URI nodeGroupUri = response.selectedNodes.get(hostId);

            singleResult.requestUri = requestUri;
            singleResult.hostId = hostId;
            singleResult.nodeGroupUri = nodeGroupUri;
            singleResult.json = null;
            singleResult.errorResponse = errorResponse;

            result.allResponses.add(singleResult);
            result.failureResponses.add(singleResult);
            result.failureErrorResponses.add(errorResponse);
        }

        return result;
    }

    private  NodeGroupUtils();

}