refactor: replace switch statements with modern switch expressions for better readability (#4095)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Anthony Stirling <77850077+Frooodle@users.noreply.github.com>
This commit is contained in:
Balázs Szücs 2025-09-28 22:40:23 +02:00 committed by GitHub
parent d01b853335
commit 413cd0c697
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 80 additions and 137 deletions

View File

@ -85,19 +85,16 @@ public class ImageProcessingUtils {
return 0; return 0;
} }
int orientationTag = directory.getInt(ExifSubIFDDirectory.TAG_ORIENTATION); int orientationTag = directory.getInt(ExifSubIFDDirectory.TAG_ORIENTATION);
switch (orientationTag) { return switch (orientationTag) {
case 1: case 1 -> 0;
return 0; case 6 -> 90;
case 6: case 3 -> 180;
return 90; case 8 -> 270;
case 3: default -> {
return 180;
case 8:
return 270;
default:
log.warn("Unknown orientation tag: {}", orientationTag); log.warn("Unknown orientation tag: {}", orientationTag);
return 0; yield 0;
} }
};
} catch (ImageProcessingException | MetadataException e) { } catch (ImageProcessingException | MetadataException e) {
return 0; return 0;
} }

View File

@ -631,16 +631,13 @@ public class PdfUtils {
int actualPageCount = pdfDocument.getNumberOfPages(); int actualPageCount = pdfDocument.getNumberOfPages();
pdfDocument.close(); pdfDocument.close();
switch (comparator.toLowerCase()) { return switch (comparator.toLowerCase()) {
case "greater": case "greater" -> actualPageCount > pageCount;
return actualPageCount > pageCount; case "equal" -> actualPageCount == pageCount;
case "equal": case "less" -> actualPageCount < pageCount;
return actualPageCount == pageCount; default ->
case "less":
return actualPageCount < pageCount;
default:
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator); throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
} };
} }
public boolean pageSize(PDDocument pdfDocument, String expectedPageSize) throws IOException { public boolean pageSize(PDDocument pdfDocument, String expectedPageSize) throws IOException {
@ -662,9 +659,15 @@ public class PdfUtils {
return actualPageWidth == expectedPageWidth && actualPageHeight == expectedPageHeight; return actualPageWidth == expectedPageWidth && actualPageHeight == expectedPageHeight;
} }
/** Key for storing the dimensions of a rendered image in a map. */ /**
private record PdfRenderSettingsKey(float mediaBoxWidth, float mediaBoxHeight, int rotation) {} * Key for storing the dimensions of a rendered image in a map.
*/
private record PdfRenderSettingsKey(float mediaBoxWidth, float mediaBoxHeight, int rotation) {
}
/** Value for storing the dimensions of a rendered image in a map. */ /**
private record PdfImageDimensionValue(int width, int height) {} * Value for storing the dimensions of a rendered image in a map.
*/
private record PdfImageDimensionValue(int width, int height) {
}
} }

View File

@ -205,30 +205,19 @@ public class RearrangePagesPDFController {
private List<Integer> processSortTypes(String sortTypes, int totalPages, String pageOrder) { private List<Integer> processSortTypes(String sortTypes, int totalPages, String pageOrder) {
try { try {
SortTypes mode = SortTypes.valueOf(sortTypes.toUpperCase()); SortTypes mode = SortTypes.valueOf(sortTypes.toUpperCase());
switch (mode) { return switch (mode) {
case REVERSE_ORDER: case REVERSE_ORDER -> reverseOrder(totalPages);
return reverseOrder(totalPages); case DUPLEX_SORT -> duplexSort(totalPages);
case DUPLEX_SORT: case BOOKLET_SORT -> bookletSort(totalPages);
return duplexSort(totalPages); case SIDE_STITCH_BOOKLET_SORT -> sideStitchBooklet(totalPages);
case BOOKLET_SORT: case ODD_EVEN_SPLIT -> oddEvenSplit(totalPages);
return bookletSort(totalPages); case ODD_EVEN_MERGE -> oddEvenMerge(totalPages);
case SIDE_STITCH_BOOKLET_SORT: case REMOVE_FIRST -> removeFirst(totalPages);
return sideStitchBooklet(totalPages); case REMOVE_LAST -> removeLast(totalPages);
case ODD_EVEN_SPLIT: case REMOVE_FIRST_AND_LAST -> removeFirstAndLast(totalPages);
return oddEvenSplit(totalPages); case DUPLICATE -> duplicate(totalPages, pageOrder);
case ODD_EVEN_MERGE: default -> throw new IllegalArgumentException("Unsupported custom mode");
return oddEvenMerge(totalPages); };
case REMOVE_FIRST:
return removeFirst(totalPages);
case REMOVE_LAST:
return removeLast(totalPages);
case REMOVE_FIRST_AND_LAST:
return removeFirstAndLast(totalPages);
case DUPLICATE:
return duplicate(totalPages, pageOrder);
default:
throw new IllegalArgumentException("Unsupported custom mode");
}
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
log.error("Unsupported custom mode", e); log.error("Unsupported custom mode", e);
return null; return null;

View File

@ -86,22 +86,14 @@ public class FilterController {
// Load the PDF // Load the PDF
PDDocument document = pdfDocumentFactory.load(inputFile); PDDocument document = pdfDocumentFactory.load(inputFile);
int actualPageCount = document.getNumberOfPages(); int actualPageCount = document.getNumberOfPages();
boolean valid;
// Perform the comparison // Perform the comparison
switch (comparator) { boolean valid = switch (comparator) {
case "Greater": case "Greater" -> actualPageCount > pageCount;
valid = actualPageCount > pageCount; case "Equal" -> actualPageCount == pageCount;
break; case "Less" -> actualPageCount < pageCount;
case "Equal": default ->
valid = actualPageCount == pageCount;
break;
case "Less":
valid = actualPageCount < pageCount;
break;
default:
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator); throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
} };
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile); if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
return null; return null;
@ -130,21 +122,14 @@ public class FilterController {
PDRectangle standardSize = PdfUtils.textToPageSize(standardPageSize); PDRectangle standardSize = PdfUtils.textToPageSize(standardPageSize);
float standardArea = standardSize.getWidth() * standardSize.getHeight(); float standardArea = standardSize.getWidth() * standardSize.getHeight();
boolean valid;
// Perform the comparison // Perform the comparison
switch (comparator) { boolean valid = switch (comparator) {
case "Greater": case "Greater" -> actualArea > standardArea;
valid = actualArea > standardArea; case "Equal" -> actualArea == standardArea;
break; case "Less" -> actualArea < standardArea;
case "Equal": default ->
valid = actualArea == standardArea;
break;
case "Less":
valid = actualArea < standardArea;
break;
default:
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator); throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
} };
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile); if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
return null; return null;
@ -163,21 +148,14 @@ public class FilterController {
// Get the file size // Get the file size
long actualFileSize = inputFile.getSize(); long actualFileSize = inputFile.getSize();
boolean valid;
// Perform the comparison // Perform the comparison
switch (comparator) { boolean valid = switch (comparator) {
case "Greater": case "Greater" -> actualFileSize > fileSize;
valid = actualFileSize > fileSize; case "Equal" -> actualFileSize == fileSize;
break; case "Less" -> actualFileSize < fileSize;
case "Equal": default ->
valid = actualFileSize == fileSize;
break;
case "Less":
valid = actualFileSize < fileSize;
break;
default:
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator); throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
} };
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile); if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
return null; return null;
@ -199,21 +177,15 @@ public class FilterController {
// Get the rotation of the first page // Get the rotation of the first page
PDPage firstPage = document.getPage(0); PDPage firstPage = document.getPage(0);
int actualRotation = firstPage.getRotation(); int actualRotation = firstPage.getRotation();
boolean valid;
// Perform the comparison // Perform the comparison
switch (comparator) { boolean valid = switch (comparator) {
case "Greater": case "Greater" -> actualRotation > rotation;
valid = actualRotation > rotation; case "Equal" -> actualRotation == rotation;
break; case "Less" -> actualRotation < rotation;
case "Equal": default ->
valid = actualRotation == rotation;
break;
case "Less":
valid = actualRotation < rotation;
break;
default:
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator); throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
} };
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile); if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
return null; return null;

View File

@ -170,27 +170,14 @@ public class WatermarkController {
throws IOException { throws IOException {
String resourceDir = ""; String resourceDir = "";
PDFont font = new PDType1Font(Standard14Fonts.FontName.HELVETICA); PDFont font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
switch (alphabet) { resourceDir = switch (alphabet) {
case "arabic": case "arabic" -> "static/fonts/NotoSansArabic-Regular.ttf";
resourceDir = "static/fonts/NotoSansArabic-Regular.ttf"; case "japanese" -> "static/fonts/Meiryo.ttf";
break; case "korean" -> "static/fonts/malgun.ttf";
case "japanese": case "chinese" -> "static/fonts/SimSun.ttf";
resourceDir = "static/fonts/Meiryo.ttf"; case "thai" -> "static/fonts/NotoSansThai-Regular.ttf";
break; default -> "static/fonts/NotoSans-Regular.ttf";
case "korean": };
resourceDir = "static/fonts/malgun.ttf";
break;
case "chinese":
resourceDir = "static/fonts/SimSun.ttf";
break;
case "thai":
resourceDir = "static/fonts/NotoSansThai-Regular.ttf";
break;
case "roman":
default:
resourceDir = "static/fonts/NotoSans-Regular.ttf";
break;
}
ClassPathResource classPathResource = new ClassPathResource(resourceDir); ClassPathResource classPathResource = new ClassPathResource(resourceDir);
String fileExtension = resourceDir.substring(resourceDir.lastIndexOf(".")); String fileExtension = resourceDir.substring(resourceDir.lastIndexOf("."));

View File

@ -281,21 +281,16 @@ public class GeneralWebController {
} }
public String getFormatFromExtension(String extension) { public String getFormatFromExtension(String extension) {
switch (extension) { return switch (extension) {
case "ttf": case "ttf" -> "truetype";
return "truetype"; case "woff" -> "woff";
case "woff": case "woff2" -> "woff2";
return "woff"; case "eot" -> "embedded-opentype";
case "woff2": case "svg" -> "svg";
return "woff2"; default ->
case "eot":
return "embedded-opentype";
case "svg":
return "svg";
default:
// or throw an exception if an unexpected extension is encountered // or throw an exception if an unexpected extension is encountered
return ""; "";
} };
} }
@GetMapping("/crop") @GetMapping("/crop")