From 032cf24eb5d6bae1f90cd6f380f3ff0b7f84c3ec Mon Sep 17 00:00:00 2001 From: Jakob Vogel Date: Wed, 13 Mar 2024 22:18:22 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Provides=20method=20to=20stream=20all=20ent?= =?UTF-8?q?ries=20of=20the=20archive=20=F0=9F=8E=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new method just wraps an already existing interface accepting the pointer to a consumer. The stream object allows for more readable filtering and processing. OX-10756 --- .../biz/jobs/batch/file/ArchiveImportJob.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/sirius/biz/jobs/batch/file/ArchiveImportJob.java b/src/main/java/sirius/biz/jobs/batch/file/ArchiveImportJob.java index f2426db5e..ad6937ad6 100644 --- a/src/main/java/sirius/biz/jobs/batch/file/ArchiveImportJob.java +++ b/src/main/java/sirius/biz/jobs/batch/file/ArchiveImportJob.java @@ -31,6 +31,7 @@ import java.util.Enumeration; import java.util.Optional; import java.util.function.Consumer; +import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -140,6 +141,19 @@ protected boolean containsEntries(String... fileNamesToCheck) { .allMatch(fileName -> zipEntries.stream().anyMatch(entry -> entry.getName().equals(fileName))); } + /** + * Streams all entries of the archive. + *

+ * Technically, this method just wraps the method {@link #extractAllFiles(Consumer)} into a {@link Stream}. + * + * @return a stream of all entries in the archive + */ + protected Stream streamEntries() { + Stream.Builder builder = Stream.builder(); + extractAllFiles(builder); + return builder.build(); + } + /** * Extracts all files from the archive * From 7f8fffc2dd96d1904b08aed8dd1333075b7e7710 Mon Sep 17 00:00:00 2001 From: Jakob Vogel Date: Wed, 13 Mar 2024 23:46:38 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Refactors=20code=20into=20auxiliary=20metho?= =?UTF-8?q?d=20=F0=9F=93=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new method enables streamlining of code using optionals, such as the ones returned by `fetchEntry(…)` OX-10756 --- .../biz/jobs/batch/file/ArchiveImportJob.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/sirius/biz/jobs/batch/file/ArchiveImportJob.java b/src/main/java/sirius/biz/jobs/batch/file/ArchiveImportJob.java index ad6937ad6..8f31449f6 100644 --- a/src/main/java/sirius/biz/jobs/batch/file/ArchiveImportJob.java +++ b/src/main/java/sirius/biz/jobs/batch/file/ArchiveImportJob.java @@ -20,6 +20,7 @@ import sirius.kernel.commons.Producer; import sirius.kernel.commons.Strings; import sirius.kernel.health.Exceptions; +import sirius.kernel.health.HandledException; import sirius.kernel.nls.NLS; import javax.annotation.Nullable; @@ -116,10 +117,7 @@ protected Optional fetchEntry(String fileName) throws IOException protected void handleMissingFile(String fileName, boolean isRequired) { if (isRequired) { - throw Exceptions.createHandled() - .withNLSKey("ArchiveImportJob.errorMsg.requiredFileMissing") - .set("fileName", fileName) - .handle(); + throw createMissingFileException(fileName); } else { process.log(ProcessLog.info() .withNLSKey("ArchiveImportJob.errorMsg.optionalFileMissing") @@ -128,6 +126,13 @@ protected void handleMissingFile(String fileName, boolean isRequired) { } } + protected HandledException createMissingFileException(String fileName) { + return Exceptions.createHandled() + .withNLSKey("ArchiveImportJob.errorMsg.requiredFileMissing") + .set("fileName", fileName) + .handle(); + } + /** * Checks if all given files are found in the archive. *