class Validator {

    static void validateAdjacency(Document[] manifests) throws PDUStitchingException {
        int[] startOffsets = new int[manifests.length];
        int[] endOffsets = new int[manifests.length];
        for (int k = 0; k < manifests.length; k++) {
            Document manifest = manifests[k];
            final NodeList nadirImageSizes = manifest.getElementsByTagName("slstr:nadirImageSize");
            for (int i = 0; i < nadirImageSizes.getLength(); i++) {
                final Node nadirImageSize = nadirImageSizes.item(i);
                final Node grid = nadirImageSize.getAttributes().getNamedItem("grid");
                startOffsets[k] = -1;
                endOffsets[k] = -1;
                if (grid != null && grid.getNodeValue().equals("Tie Points")) {
                    final NodeList childNodes = nadirImageSize.getChildNodes();
                    for (int j = 0; j < childNodes.getLength(); j++) {
                        if (childNodes.item(j).getNodeName().equals("sentinel3:startOffset")) {
                            startOffsets[k] = Integer.parseInt(childNodes.item(j).getTextContent());
                        } else if (childNodes.item(j).getNodeName().equals("sentinel3:rows")) {
                            endOffsets[k] = startOffsets[k] + Integer.parseInt(childNodes.item(j).getTextContent());
                        }
                    }
                    break;
                }
            }
        }
        Arrays.sort(startOffsets);
        Arrays.sort(endOffsets);
        for (int i = 0; i < manifests.length - 1; i++) {
            if (endOffsets[i] != -1 && startOffsets[i + 1] != -1 && endOffsets[i] != startOffsets[i + 1]) {
                throw new PDUStitchingException("Selected units must be adjacent");
            }
        }
    }

}