diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/DataEditorForm.java b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/DataEditorForm.java index d47cbab3f18..4dfe2456908 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/DataEditorForm.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/DataEditorForm.java @@ -685,6 +685,8 @@ public List> getSelectedMedia() { /** * Checks and returns if consecutive physical divisions in one structure element are selected or not. + * + *

Note: This method is called potentially thousands of times when rendering large galleries.

*/ public boolean consecutivePagesSelected() { if (selectedMedia.isEmpty()) { @@ -692,8 +694,24 @@ public boolean consecutivePagesSelected() { } int maxOrder = selectedMedia.stream().mapToInt(m -> m.getLeft().getOrder()).max().orElseThrow(NoSuchElementException::new); int minOrder = selectedMedia.stream().mapToInt(m -> m.getLeft().getOrder()).min().orElseThrow(NoSuchElementException::new); - return selectedMedia.size() - 1 == maxOrder - minOrder - && selectedMedia.stream().map(Pair::getValue).distinct().count() == 1; + + // Check whether the set of selected media all belong to the same logical division, otherwise the selection + // is not consecutive. However, do not use stream().distinct(), which will do pairwise comparisons, which is + // slow for large amounts of selected images. Instead, just check whether the first logical division matches + // all others in a simple loop. + Boolean theSameLogicalDivisions = true; + LogicalDivision firstSelectedMediaLogicalDivison = null; + for (Pair pair : selectedMedia) { + if (Objects.isNull(firstSelectedMediaLogicalDivison)) { + firstSelectedMediaLogicalDivison = pair.getRight(); + } else { + if (!Objects.equals(firstSelectedMediaLogicalDivison, pair.getRight())) { + theSameLogicalDivisions = false; + break; + } + } + } + return selectedMedia.size() - 1 == maxOrder - minOrder && theSameLogicalDivisions; } void setSelectedMedia(List> media) {