class PixelQualitySummaryMerger extends AbstractElementMerger {

    @Override
    public void mergeNodes(List<Node> fromParents, Element toParent, Document toDocument) throws PDUStitchingException {
        final String grid = fromParents.get(0).getAttributes().getNamedItem("grid").getNodeValue();
        toParent.setAttribute("grid", grid);
        final Collection<List<Node>> summaryNodesLists = collectSummaryNodes(fromParents);
        final NumberFormatter numberFormatter = getNumberFormatter();
        for (List<Node> summaryNodesList : summaryNodesLists) {
            if (summaryNodesList.size() != fromParents.size()) {
                //todo throw exception? - tf 20160216
                continue;
            }
            int totalClassifiedPixels = 0;
            int totalPixels = 0;
            for (Node summaryNode : summaryNodesList) {
                final NamedNodeMap summaryNodeAttributes = summaryNode.getAttributes();
                int classifiedPixels = Integer.parseInt(summaryNodeAttributes.getNamedItem("value").getNodeValue());
                final double percentage = Double.parseDouble(summaryNodeAttributes.getNamedItem("percentage").getNodeValue());
                totalClassifiedPixels += classifiedPixels;
                totalPixels += classifiedPixels / percentage * 100;
            }
            final Element summaryElement = toDocument.createElement(summaryNodesList.get(0).getNodeName());
            summaryElement.setAttribute("value", "" + totalClassifiedPixels);
            if (totalPixels > 0) {
                Double percentage = (totalClassifiedPixels / (double) totalPixels) * 100;
                try {
                    summaryElement.setAttribute("percentage", numberFormatter.valueToString(percentage));
                } catch (ParseException e) {
                    throw new PDUStitchingException("Could not format number: " + e.getMessage());
                }
            } else {
                summaryElement.setAttribute("percentage", "0.000000");
            }
            toParent.appendChild(summaryElement);
        }
    }

}