From 82331a030dfadfc7bbcfff59418b9ee89df5ceac Mon Sep 17 00:00:00 2001 From: tschoemaker Date: Thu, 6 Jun 2024 23:50:37 +0200 Subject: [PATCH] refactor data clumps --- .../controller/api/OverlayParameters.java | 46 +++++++++++++++++++ .../controller/api/PdfOverlayController.java | 44 +++++++++--------- 2 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 src/main/java/stirling/software/SPDF/controller/api/OverlayParameters.java diff --git a/src/main/java/stirling/software/SPDF/controller/api/OverlayParameters.java b/src/main/java/stirling/software/SPDF/controller/api/OverlayParameters.java new file mode 100644 index 00000000..1bd97b8b --- /dev/null +++ b/src/main/java/stirling/software/SPDF/controller/api/OverlayParameters.java @@ -0,0 +1,46 @@ +package stirling.software.SPDF.controller.api; + +import java.io.File; +import java.util.List; + +public class OverlayParameters { + + private final int basePageCount; + private final File[] overlayFiles; + private final String mode; + private final int[] counts; + private final List tempFiles; + + public OverlayParameters( + int basePageCount, + File[] overlayFiles, + String mode, + int[] counts, + List tempFiles) { + this.basePageCount = basePageCount; + this.overlayFiles = overlayFiles; + this.mode = mode; + this.counts = counts; + this.tempFiles = tempFiles; + } + + public int getBasePageCount() { + return basePageCount; + } + + public File[] getOverlayFiles() { + return overlayFiles; + } + + public String getMode() { + return mode; + } + + public int[] getCounts() { + return counts; + } + + public List getTempFiles() { + return tempFiles; + } +} diff --git a/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java b/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java index 48c987cf..162bb4b4 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/PdfOverlayController.java @@ -58,13 +58,9 @@ public class PdfOverlayController { try (PDDocument basePdf = Loader.loadPDF(baseFile.getBytes()); Overlay overlay = new Overlay()) { - Map overlayGuide = - prepareOverlayGuide( - basePdf.getNumberOfPages(), - overlayPdfFiles, - mode, - counts, - tempFiles); + OverlayParameters overlayParameters = + new OverlayParameters(overlayPos, overlayPdfFiles, mode, counts, tempFiles); + Map overlayGuide = prepareOverlayGuide(overlayParameters); overlay.setInputPDF(basePdf); if (overlayPos == 0) { @@ -98,19 +94,17 @@ public class PdfOverlayController { } } - private Map prepareOverlayGuide( - int basePageCount, File[] overlayFiles, String mode, int[] counts, List tempFiles) - throws IOException { + private Map prepareOverlayGuide(OverlayParameters params) throws IOException { Map overlayGuide = new HashMap<>(); - switch (mode) { + switch (params.getMode()) { case "SequentialOverlay": - sequentialOverlay(overlayGuide, overlayFiles, basePageCount, tempFiles); + sequentialOverlay(overlayGuide, params); break; case "InterleavedOverlay": - interleavedOverlay(overlayGuide, overlayFiles, basePageCount); + interleavedOverlay(overlayGuide, params); break; case "FixedRepeatOverlay": - fixedRepeatOverlay(overlayGuide, overlayFiles, counts, basePageCount); + fixedRepeatOverlay(overlayGuide, params); break; default: throw new IllegalArgumentException("Invalid overlay mode"); @@ -118,14 +112,13 @@ public class PdfOverlayController { return overlayGuide; } - private void sequentialOverlay( - Map overlayGuide, - File[] overlayFiles, - int basePageCount, - List tempFiles) + private void sequentialOverlay(Map overlayGuide, OverlayParameters params) throws IOException { int overlayFileIndex = 0; int pageCountInCurrentOverlay = 0; + File[] overlayFiles = params.getOverlayFiles(); + List tempFiles = params.getTempFiles(); + int basePageCount = params.getBasePageCount(); for (int basePageIndex = 1; basePageIndex <= basePageCount; basePageIndex++) { if (pageCountInCurrentOverlay == 0 @@ -156,9 +149,11 @@ public class PdfOverlayController { } } - private void interleavedOverlay( - Map overlayGuide, File[] overlayFiles, int basePageCount) + private void interleavedOverlay(Map overlayGuide, OverlayParameters params) throws IOException { + File[] overlayFiles = params.getOverlayFiles(); + int basePageCount = params.getBasePageCount(); + for (int basePageIndex = 1; basePageIndex <= basePageCount; basePageIndex++) { File overlayFile = overlayFiles[(basePageIndex - 1) % overlayFiles.length]; @@ -172,9 +167,12 @@ public class PdfOverlayController { } } - private void fixedRepeatOverlay( - Map overlayGuide, File[] overlayFiles, int[] counts, int basePageCount) + private void fixedRepeatOverlay(Map overlayGuide, OverlayParameters params) throws IOException { + File[] overlayFiles = params.getOverlayFiles(); + int[] counts = params.getCounts(); + int basePageCount = params.getBasePageCount(); + if (overlayFiles.length != counts.length) { throw new IllegalArgumentException( "Counts array length must match the number of overlay files");