mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-15 02:54:06 +02:00
refactor: replace switch statements with modern switch expressions for better readability (#4095)
Co-authored-by: Copilot <[email protected]> Co-authored-by: Anthony Stirling <[email protected]>
This commit is contained in:
co-authored by
Copilot
Anthony Stirling
parent
d01b853335
commit
413cd0c697
+13
-24
@@ -205,30 +205,19 @@ public class RearrangePagesPDFController {
|
||||
private List<Integer> processSortTypes(String sortTypes, int totalPages, String pageOrder) {
|
||||
try {
|
||||
SortTypes mode = SortTypes.valueOf(sortTypes.toUpperCase());
|
||||
switch (mode) {
|
||||
case REVERSE_ORDER:
|
||||
return reverseOrder(totalPages);
|
||||
case DUPLEX_SORT:
|
||||
return duplexSort(totalPages);
|
||||
case BOOKLET_SORT:
|
||||
return bookletSort(totalPages);
|
||||
case SIDE_STITCH_BOOKLET_SORT:
|
||||
return sideStitchBooklet(totalPages);
|
||||
case ODD_EVEN_SPLIT:
|
||||
return oddEvenSplit(totalPages);
|
||||
case ODD_EVEN_MERGE:
|
||||
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");
|
||||
}
|
||||
return switch (mode) {
|
||||
case REVERSE_ORDER -> reverseOrder(totalPages);
|
||||
case DUPLEX_SORT -> duplexSort(totalPages);
|
||||
case BOOKLET_SORT -> bookletSort(totalPages);
|
||||
case SIDE_STITCH_BOOKLET_SORT -> sideStitchBooklet(totalPages);
|
||||
case ODD_EVEN_SPLIT -> oddEvenSplit(totalPages);
|
||||
case ODD_EVEN_MERGE -> oddEvenMerge(totalPages);
|
||||
case REMOVE_FIRST -> removeFirst(totalPages);
|
||||
case REMOVE_LAST -> removeLast(totalPages);
|
||||
case REMOVE_FIRST_AND_LAST -> removeFirstAndLast(totalPages);
|
||||
case DUPLICATE -> duplicate(totalPages, pageOrder);
|
||||
default -> throw new IllegalArgumentException("Unsupported custom mode");
|
||||
};
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.error("Unsupported custom mode", e);
|
||||
return null;
|
||||
|
||||
+25
-53
@@ -86,22 +86,14 @@ public class FilterController {
|
||||
// Load the PDF
|
||||
PDDocument document = pdfDocumentFactory.load(inputFile);
|
||||
int actualPageCount = document.getNumberOfPages();
|
||||
|
||||
boolean valid;
|
||||
// Perform the comparison
|
||||
switch (comparator) {
|
||||
case "Greater":
|
||||
valid = actualPageCount > pageCount;
|
||||
break;
|
||||
case "Equal":
|
||||
valid = actualPageCount == pageCount;
|
||||
break;
|
||||
case "Less":
|
||||
valid = actualPageCount < pageCount;
|
||||
break;
|
||||
default:
|
||||
boolean valid = switch (comparator) {
|
||||
case "Greater" -> actualPageCount > pageCount;
|
||||
case "Equal" -> actualPageCount == pageCount;
|
||||
case "Less" -> actualPageCount < pageCount;
|
||||
default ->
|
||||
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
|
||||
}
|
||||
};
|
||||
|
||||
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||
return null;
|
||||
@@ -130,21 +122,14 @@ public class FilterController {
|
||||
PDRectangle standardSize = PdfUtils.textToPageSize(standardPageSize);
|
||||
float standardArea = standardSize.getWidth() * standardSize.getHeight();
|
||||
|
||||
boolean valid;
|
||||
// Perform the comparison
|
||||
switch (comparator) {
|
||||
case "Greater":
|
||||
valid = actualArea > standardArea;
|
||||
break;
|
||||
case "Equal":
|
||||
valid = actualArea == standardArea;
|
||||
break;
|
||||
case "Less":
|
||||
valid = actualArea < standardArea;
|
||||
break;
|
||||
default:
|
||||
boolean valid = switch (comparator) {
|
||||
case "Greater" -> actualArea > standardArea;
|
||||
case "Equal" -> actualArea == standardArea;
|
||||
case "Less" -> actualArea < standardArea;
|
||||
default ->
|
||||
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
|
||||
}
|
||||
};
|
||||
|
||||
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||
return null;
|
||||
@@ -163,21 +148,14 @@ public class FilterController {
|
||||
// Get the file size
|
||||
long actualFileSize = inputFile.getSize();
|
||||
|
||||
boolean valid;
|
||||
// Perform the comparison
|
||||
switch (comparator) {
|
||||
case "Greater":
|
||||
valid = actualFileSize > fileSize;
|
||||
break;
|
||||
case "Equal":
|
||||
valid = actualFileSize == fileSize;
|
||||
break;
|
||||
case "Less":
|
||||
valid = actualFileSize < fileSize;
|
||||
break;
|
||||
default:
|
||||
boolean valid = switch (comparator) {
|
||||
case "Greater" -> actualFileSize > fileSize;
|
||||
case "Equal" -> actualFileSize == fileSize;
|
||||
case "Less" -> actualFileSize < fileSize;
|
||||
default ->
|
||||
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
|
||||
}
|
||||
};
|
||||
|
||||
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||
return null;
|
||||
@@ -199,21 +177,15 @@ public class FilterController {
|
||||
// Get the rotation of the first page
|
||||
PDPage firstPage = document.getPage(0);
|
||||
int actualRotation = firstPage.getRotation();
|
||||
boolean valid;
|
||||
|
||||
// Perform the comparison
|
||||
switch (comparator) {
|
||||
case "Greater":
|
||||
valid = actualRotation > rotation;
|
||||
break;
|
||||
case "Equal":
|
||||
valid = actualRotation == rotation;
|
||||
break;
|
||||
case "Less":
|
||||
valid = actualRotation < rotation;
|
||||
break;
|
||||
default:
|
||||
boolean valid = switch (comparator) {
|
||||
case "Greater" -> actualRotation > rotation;
|
||||
case "Equal" -> actualRotation == rotation;
|
||||
case "Less" -> actualRotation < rotation;
|
||||
default ->
|
||||
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
|
||||
}
|
||||
};
|
||||
|
||||
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
|
||||
return null;
|
||||
|
||||
+8
-21
@@ -170,27 +170,14 @@ public class WatermarkController {
|
||||
throws IOException {
|
||||
String resourceDir = "";
|
||||
PDFont font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
|
||||
switch (alphabet) {
|
||||
case "arabic":
|
||||
resourceDir = "static/fonts/NotoSansArabic-Regular.ttf";
|
||||
break;
|
||||
case "japanese":
|
||||
resourceDir = "static/fonts/Meiryo.ttf";
|
||||
break;
|
||||
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;
|
||||
}
|
||||
resourceDir = switch (alphabet) {
|
||||
case "arabic" -> "static/fonts/NotoSansArabic-Regular.ttf";
|
||||
case "japanese" -> "static/fonts/Meiryo.ttf";
|
||||
case "korean" -> "static/fonts/malgun.ttf";
|
||||
case "chinese" -> "static/fonts/SimSun.ttf";
|
||||
case "thai" -> "static/fonts/NotoSansThai-Regular.ttf";
|
||||
default -> "static/fonts/NotoSans-Regular.ttf";
|
||||
};
|
||||
|
||||
ClassPathResource classPathResource = new ClassPathResource(resourceDir);
|
||||
String fileExtension = resourceDir.substring(resourceDir.lastIndexOf("."));
|
||||
|
||||
+9
-14
@@ -281,21 +281,16 @@ public class GeneralWebController {
|
||||
}
|
||||
|
||||
public String getFormatFromExtension(String extension) {
|
||||
switch (extension) {
|
||||
case "ttf":
|
||||
return "truetype";
|
||||
case "woff":
|
||||
return "woff";
|
||||
case "woff2":
|
||||
return "woff2";
|
||||
case "eot":
|
||||
return "embedded-opentype";
|
||||
case "svg":
|
||||
return "svg";
|
||||
default:
|
||||
return switch (extension) {
|
||||
case "ttf" -> "truetype";
|
||||
case "woff" -> "woff";
|
||||
case "woff2" -> "woff2";
|
||||
case "eot" -> "embedded-opentype";
|
||||
case "svg" -> "svg";
|
||||
default ->
|
||||
// or throw an exception if an unexpected extension is encountered
|
||||
return "";
|
||||
}
|
||||
"";
|
||||
};
|
||||
}
|
||||
|
||||
@GetMapping("/crop")
|
||||
|
||||
Reference in New Issue
Block a user