From a6a6de960e5d70fa0b3299117396adee320d5849 Mon Sep 17 00:00:00 2001 From: Ludy87 Date: Fri, 12 Jun 2026 12:58:14 +0200 Subject: [PATCH] Refactor switch statements to use switch expressions for improved readability and conciseness Signed-off-by: Ludy87 --- .../common/model/oauth2/Provider.java | 44 ++--- .../common/pdf/PdfMarkdownConverter.java | 3 +- .../common/service/InternalApiClient.java | 2 +- .../software/common/util/FormUtils.java | 177 ++++++++---------- .../common/util/ImageProcessingUtils.java | 24 ++- .../SPDF/config/MultipartConfiguration.java | 7 +- .../api/BookletImpositionController.java | 15 +- .../security/RemoveCertSignController.java | 2 +- .../controller/web/UploadLimitService.java | 7 +- .../security/configuration/CacheConfig.java | 2 - .../service/KeyPersistenceService.java | 4 +- 11 files changed, 132 insertions(+), 155 deletions(-) diff --git a/app/common/src/main/java/stirling/software/common/model/oauth2/Provider.java b/app/common/src/main/java/stirling/software/common/model/oauth2/Provider.java index 185956fbb5..7a431baf26 100644 --- a/app/common/src/main/java/stirling/software/common/model/oauth2/Provider.java +++ b/app/common/src/main/java/stirling/software/common/model/oauth2/Provider.java @@ -60,54 +60,40 @@ public class Provider { } private UsernameAttribute validateUsernameAttribute(UsernameAttribute usernameAttribute) { - switch (name) { - case "google" -> { - return validateGoogleUsernameAttribute(usernameAttribute); - } - case "github" -> { - return validateGitHubUsernameAttribute(usernameAttribute); - } - case "keycloak" -> { - return validateKeycloakUsernameAttribute(usernameAttribute); - } - default -> { - return usernameAttribute; - } - } + return switch (name) { + case "google" -> validateGoogleUsernameAttribute(usernameAttribute); + case "github" -> validateGitHubUsernameAttribute(usernameAttribute); + case "keycloak" -> validateKeycloakUsernameAttribute(usernameAttribute); + default -> usernameAttribute; + }; } private UsernameAttribute validateKeycloakUsernameAttribute( UsernameAttribute usernameAttribute) { - switch (usernameAttribute) { - case EMAIL, NAME, GIVEN_NAME, FAMILY_NAME, PREFERRED_USERNAME -> { - return usernameAttribute; - } + return switch (usernameAttribute) { + case EMAIL, NAME, GIVEN_NAME, FAMILY_NAME, PREFERRED_USERNAME -> usernameAttribute; default -> throw new UnsupportedClaimException( String.format(EXCEPTION_MESSAGE, usernameAttribute, clientName)); - } + }; } private UsernameAttribute validateGoogleUsernameAttribute(UsernameAttribute usernameAttribute) { - switch (usernameAttribute) { - case EMAIL, NAME, GIVEN_NAME, FAMILY_NAME -> { - return usernameAttribute; - } + return switch (usernameAttribute) { + case EMAIL, NAME, GIVEN_NAME, FAMILY_NAME -> usernameAttribute; default -> throw new UnsupportedClaimException( String.format(EXCEPTION_MESSAGE, usernameAttribute, clientName)); - } + }; } private UsernameAttribute validateGitHubUsernameAttribute(UsernameAttribute usernameAttribute) { - switch (usernameAttribute) { - case LOGIN, EMAIL, NAME -> { - return usernameAttribute; - } + return switch (usernameAttribute) { + case LOGIN, EMAIL, NAME -> usernameAttribute; default -> throw new UnsupportedClaimException( String.format(EXCEPTION_MESSAGE, usernameAttribute, clientName)); - } + }; } @Override diff --git a/app/common/src/main/java/stirling/software/common/pdf/PdfMarkdownConverter.java b/app/common/src/main/java/stirling/software/common/pdf/PdfMarkdownConverter.java index 9f3f53fd14..bad1ac356c 100644 --- a/app/common/src/main/java/stirling/software/common/pdf/PdfMarkdownConverter.java +++ b/app/common/src/main/java/stirling/software/common/pdf/PdfMarkdownConverter.java @@ -910,7 +910,8 @@ public class PdfMarkdownConverter { } // Only merge a sentence continuation between two text paragraphs, never into/out of a // table. - if (!(output.getLast() instanceof String last) || !(pageItems.getFirst() instanceof String first)) { + if (!(output.getLast() instanceof String last) + || !(pageItems.getFirst() instanceof String first)) { return; } if (!first.isEmpty() diff --git a/app/common/src/main/java/stirling/software/common/service/InternalApiClient.java b/app/common/src/main/java/stirling/software/common/service/InternalApiClient.java index 37c110c271..eb0c38d1d8 100644 --- a/app/common/src/main/java/stirling/software/common/service/InternalApiClient.java +++ b/app/common/src/main/java/stirling/software/common/service/InternalApiClient.java @@ -104,7 +104,7 @@ public class InternalApiClient { boolean hasFilePart = body.values().stream() .flatMap(java.util.List::stream) - .anyMatch(v -> v instanceof Resource); + .anyMatch(Resource.class::isInstance); if (isAiTool && !hasFilePart) { headers.setContentType(MediaType.MULTIPART_FORM_DATA); } diff --git a/app/common/src/main/java/stirling/software/common/util/FormUtils.java b/app/common/src/main/java/stirling/software/common/util/FormUtils.java index 92f9e519b6..2f6dbf1537 100644 --- a/app/common/src/main/java/stirling/software/common/util/FormUtils.java +++ b/app/common/src/main/java/stirling/software/common/util/FormUtils.java @@ -88,28 +88,16 @@ public class FormUtils { * text) */ public String detectFieldType(PDField field) { - if (field instanceof PDSignatureField) { - return FIELD_TYPE_SIGNATURE; - } - if (field instanceof PDPushButton) { - return FIELD_TYPE_BUTTON; - } - if (field instanceof PDTextField) { - return FIELD_TYPE_TEXT; - } - if (field instanceof PDCheckBox) { - return FIELD_TYPE_CHECKBOX; - } - if (field instanceof PDComboBox) { - return FIELD_TYPE_COMBOBOX; - } - if (field instanceof PDListBox) { - return FIELD_TYPE_LISTBOX; - } - if (field instanceof PDRadioButton) { - return FIELD_TYPE_RADIO; - } - return FIELD_TYPE_TEXT; + return switch (field) { + case PDSignatureField ignored -> FIELD_TYPE_SIGNATURE; + case PDPushButton ignored -> FIELD_TYPE_BUTTON; + case PDTextField ignored -> FIELD_TYPE_TEXT; + case PDCheckBox ignored -> FIELD_TYPE_CHECKBOX; + case PDComboBox ignored -> FIELD_TYPE_COMBOBOX; + case PDListBox ignored -> FIELD_TYPE_LISTBOX; + case PDRadioButton ignored -> FIELD_TYPE_RADIO; + case null, default -> FIELD_TYPE_TEXT; + }; } public List extractFormFields(PDDocument document) { @@ -583,22 +571,17 @@ public class FormUtils { continue; } String type = info.type(); - Object value; - switch (type) { - case FIELD_TYPE_CHECKBOX: - value = isChecked(info.value()) ? Boolean.TRUE : Boolean.FALSE; - break; - case FIELD_TYPE_LISTBOX: - if (info.multiSelect()) { - value = new ArrayList<>(); - } else { - value = safeDefault(info.value()); - } - break; - case FIELD_TYPE_BUTTON, FIELD_TYPE_SIGNATURE: - continue; // skip non-fillable - default: - value = safeDefault(info.value()); + Object value = + switch (type) { + case FIELD_TYPE_CHECKBOX -> + isChecked(info.value()) ? Boolean.TRUE : Boolean.FALSE; + case FIELD_TYPE_LISTBOX -> + info.multiSelect() ? new ArrayList<>() : safeDefault(info.value()); + case FIELD_TYPE_BUTTON, FIELD_TYPE_SIGNATURE -> null; + default -> safeDefault(info.value()); + }; + if (value == null) { + continue; // skip non-fillable } record.put(info.name(), value); } @@ -954,39 +937,38 @@ public class FormUtils { private void applyValueToField(PDField field, String value, boolean strict) throws IOException { try { - if (field instanceof PDTextField textField) { - setTextValue(textField, value); - } else if (field instanceof PDCheckBox checkBox) { - LinkedHashSet candidateStates = collectCheckBoxStates(checkBox); - boolean shouldCheck = shouldCheckBoxBeChecked(value, candidateStates); - try { - if (shouldCheck) { - checkBox.check(); - } else { - checkBox.unCheck(); - } - } catch (IOException checkProblem) { - log.warn( - "Failed to set checkbox state for '{}': {}", - field.getFullyQualifiedName(), - checkProblem.getMessage(), - checkProblem); - if (strict) { - throw checkProblem; + switch (field) { + case PDTextField textField -> setTextValue(textField, value); + case PDCheckBox checkBox -> { + LinkedHashSet candidateStates = collectCheckBoxStates(checkBox); + boolean shouldCheck = shouldCheckBoxBeChecked(value, candidateStates); + try { + if (shouldCheck) { + checkBox.check(); + } else { + checkBox.unCheck(); + } + } catch (IOException checkProblem) { + log.warn( + "Failed to set checkbox state for '{}': {}", + field.getFullyQualifiedName(), + checkProblem.getMessage(), + checkProblem); + if (strict) { + throw checkProblem; + } } } - } else if (field instanceof PDRadioButton radioButton) { - if (value != null && !value.isBlank()) { - radioButton.setValue(value); + case PDRadioButton radioButton -> { + if (value != null && !value.isBlank()) { + radioButton.setValue(value); + } } - } else if (field instanceof PDChoice choiceField) { - applyChoiceValue(choiceField, value); - } else if (field instanceof PDPushButton) { - log.debug("Ignore Push button"); - } else if (field instanceof PDSignatureField) { - log.debug("Skipping signature field '{}'", field.getFullyQualifiedName()); - } else { - field.setValue(value != null ? value : ""); + case PDChoice choiceField -> applyChoiceValue(choiceField, value); + case PDPushButton ignored -> log.debug("Ignore Push button"); + case PDSignatureField ignored -> + log.debug("Skipping signature field '{}'", field.getFullyQualifiedName()); + case null, default -> field.setValue(value != null ? value : ""); } } catch (Exception e) { log.warn( @@ -1306,37 +1288,42 @@ public class FormUtils { List resolveOptions(PDTerminalField field) { try { - if (field instanceof PDChoice choice) { - LinkedHashSet allowed = new LinkedHashSet<>(); - List exportValues = choice.getOptionsExportValues(); - List displayValues = choice.getOptionsDisplayValues(); + return switch (field) { + case PDChoice choice -> { + LinkedHashSet allowed = new LinkedHashSet<>(); + List exportValues = choice.getOptionsExportValues(); + List displayValues = choice.getOptionsDisplayValues(); - if (exportValues != null) { - exportValues.stream() - .filter(Objects::nonNull) - .map(String::trim) - .filter(s -> !s.isEmpty()) - .forEach(allowed::add); + if (exportValues != null) { + exportValues.stream() + .filter(Objects::nonNull) + .map(String::trim) + .filter(s -> !s.isEmpty()) + .forEach(allowed::add); + } + if (displayValues != null) { + displayValues.stream() + .filter(Objects::nonNull) + .map(String::trim) + .filter(s -> !s.isEmpty()) + .forEach(allowed::add); + } + yield new ArrayList<>(allowed); } - if (displayValues != null) { - displayValues.stream() - .filter(Objects::nonNull) - .map(String::trim) - .filter(s -> !s.isEmpty()) - .forEach(allowed::add); + case PDRadioButton radio -> { + List exports = radio.getExportValues(); + yield exports != null && !exports.isEmpty() + ? new ArrayList<>(exports) + : Collections.emptyList(); } - return new ArrayList<>(allowed); - } else if (field instanceof PDRadioButton radio) { - List exports = radio.getExportValues(); - if (exports != null && !exports.isEmpty()) { - return new ArrayList<>(exports); + case PDCheckBox checkBox -> { + List exports = checkBox.getExportValues(); + yield exports != null && !exports.isEmpty() + ? new ArrayList<>(exports) + : Collections.emptyList(); } - } else if (field instanceof PDCheckBox checkBox) { - List exports = checkBox.getExportValues(); - if (exports != null && !exports.isEmpty()) { - return new ArrayList<>(exports); - } - } + case null, default -> Collections.emptyList(); + }; } catch (Exception e) { log.debug( "Failed to resolve options for field '{}': {}", diff --git a/app/common/src/main/java/stirling/software/common/util/ImageProcessingUtils.java b/app/common/src/main/java/stirling/software/common/util/ImageProcessingUtils.java index 979ad25e99..030751f3e3 100644 --- a/app/common/src/main/java/stirling/software/common/util/ImageProcessingUtils.java +++ b/app/common/src/main/java/stirling/software/common/util/ImageProcessingUtils.java @@ -26,29 +26,27 @@ import lombok.extern.slf4j.Slf4j; public class ImageProcessingUtils { static BufferedImage convertColorType(BufferedImage sourceImage, String colorType) { - BufferedImage convertedImage; - switch (colorType) { - case "greyscale": - convertedImage = + return switch (colorType) { + case "greyscale" -> { + BufferedImage convertedImage = new BufferedImage( sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY); convertedImage.getGraphics().drawImage(sourceImage, 0, 0, null); - break; - case "blackwhite": - convertedImage = + yield convertedImage; + } + case "blackwhite" -> { + BufferedImage convertedImage = new BufferedImage( sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY); convertedImage.getGraphics().drawImage(sourceImage, 0, 0, null); - break; - default: // full color - convertedImage = sourceImage; - break; - } - return convertedImage; + yield convertedImage; + } + default -> sourceImage; + }; } public static byte[] getImageData(BufferedImage image) { diff --git a/app/core/src/main/java/stirling/software/SPDF/config/MultipartConfiguration.java b/app/core/src/main/java/stirling/software/SPDF/config/MultipartConfiguration.java index 9625f9e671..ff6d1b87bd 100644 --- a/app/core/src/main/java/stirling/software/SPDF/config/MultipartConfiguration.java +++ b/app/core/src/main/java/stirling/software/SPDF/config/MultipartConfiguration.java @@ -1,6 +1,5 @@ package stirling.software.SPDF.config; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -22,7 +21,11 @@ import stirling.software.SPDF.controller.web.UploadLimitService; @Slf4j public class MultipartConfiguration { - @Autowired private UploadLimitService uploadLimitService; + private final UploadLimitService uploadLimitService; + + public MultipartConfiguration(UploadLimitService uploadLimitService) { + this.uploadLimitService = uploadLimitService; + } /** * Creates MultipartConfigElement that respects fileUploadLimit from settings.yml or environment diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/BookletImpositionController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/BookletImpositionController.java index d1145fa815..fbf708e803 100644 --- a/app/core/src/main/java/stirling/software/SPDF/controller/api/BookletImpositionController.java +++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/BookletImpositionController.java @@ -294,24 +294,25 @@ public class BookletImpositionController { // Apply rotation if needed (rotate about origin), then translate to keep in cell switch (rot) { - case 90: + case 90 -> { cs.transform(Matrix.getRotateInstance(Math.PI / 2, 0, 0)); // After 90° CCW, the content spans x in [-r.getHeight(), 0] and y in [0, // r.getWidth()] cs.transform(Matrix.getTranslateInstance(0, -r.getWidth())); - break; - case 180: + } + case 180 -> { cs.transform(Matrix.getRotateInstance(Math.PI, 0, 0)); cs.transform(Matrix.getTranslateInstance(-r.getWidth(), -r.getHeight())); - break; - case 270: + } + case 270 -> { cs.transform(Matrix.getRotateInstance(3 * Math.PI / 2, 0, 0)); // After 270° CCW, the content spans x in [0, r.getHeight()] and y in // [-r.getWidth(), 0] cs.transform(Matrix.getTranslateInstance(-r.getHeight(), 0)); - break; - default: + } + default -> { // 0°: no-op + } } // Reuse LayerUtility passed from caller diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/api/security/RemoveCertSignController.java b/app/core/src/main/java/stirling/software/SPDF/controller/api/security/RemoveCertSignController.java index 26f4a09e89..5284e1e824 100644 --- a/app/core/src/main/java/stirling/software/SPDF/controller/api/security/RemoveCertSignController.java +++ b/app/core/src/main/java/stirling/software/SPDF/controller/api/security/RemoveCertSignController.java @@ -60,7 +60,7 @@ public class RemoveCertSignController { // Remove signature fields safely List fieldsToRemove = acroForm.getFields().stream() - .filter(field -> field instanceof PDSignatureField) + .filter(PDSignatureField.class::isInstance) .toList(); if (!fieldsToRemove.isEmpty()) { diff --git a/app/core/src/main/java/stirling/software/SPDF/controller/web/UploadLimitService.java b/app/core/src/main/java/stirling/software/SPDF/controller/web/UploadLimitService.java index a15f14af98..7065ac4d79 100644 --- a/app/core/src/main/java/stirling/software/SPDF/controller/web/UploadLimitService.java +++ b/app/core/src/main/java/stirling/software/SPDF/controller/web/UploadLimitService.java @@ -2,7 +2,6 @@ package stirling.software.SPDF.controller.web; import java.util.Locale; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; @@ -13,7 +12,11 @@ import stirling.software.common.model.ApplicationProperties; @Slf4j public class UploadLimitService { - @Autowired private ApplicationProperties applicationProperties; + private final ApplicationProperties applicationProperties; + + public UploadLimitService(ApplicationProperties applicationProperties) { + this.applicationProperties = applicationProperties; + } public long getUploadLimit() { String raw = diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/security/configuration/CacheConfig.java b/app/proprietary/src/main/java/stirling/software/proprietary/security/configuration/CacheConfig.java index 501826a084..9a6c70e105 100644 --- a/app/proprietary/src/main/java/stirling/software/proprietary/security/configuration/CacheConfig.java +++ b/app/proprietary/src/main/java/stirling/software/proprietary/security/configuration/CacheConfig.java @@ -2,7 +2,6 @@ package stirling.software.proprietary.security.configuration; import java.time.Duration; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.caffeine.CaffeineCacheManager; @@ -19,7 +18,6 @@ public class CacheConfig { private final ApplicationProperties applicationProperties; - @Autowired public CacheConfig(ApplicationProperties applicationProperties) { this.applicationProperties = applicationProperties; } diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/security/service/KeyPersistenceService.java b/app/proprietary/src/main/java/stirling/software/proprietary/security/service/KeyPersistenceService.java index d0c9f879be..767ec65d53 100644 --- a/app/proprietary/src/main/java/stirling/software/proprietary/security/service/KeyPersistenceService.java +++ b/app/proprietary/src/main/java/stirling/software/proprietary/security/service/KeyPersistenceService.java @@ -263,8 +263,8 @@ public class KeyPersistenceService implements KeyPersistenceServiceInterface { nativeCache.asMap().size()); return nativeCache.asMap().values().stream() - .filter(value -> value instanceof JwtVerificationKey) - .map(value -> (JwtVerificationKey) value) + .filter(JwtVerificationKey.class::isInstance) + .map(JwtVerificationKey.class::cast) .filter( key -> { boolean eligible = key.getCreatedAt().isBefore(cutoffDate);