Skip to content

Commit 413cd0c

Browse files
balazs-szucsCopilotFrooodle
authored
refactor: replace switch statements with modern switch expressions for better readability (Stirling-Tools#4095)
Co-authored-by: Copilot <[email protected]> Co-authored-by: Anthony Stirling <[email protected]>
1 parent d01b853 commit 413cd0c

File tree

6 files changed

+80
-137
lines changed

6 files changed

+80
-137
lines changed

app/common/src/main/java/stirling/software/common/util/ImageProcessingUtils.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,16 @@ public static double extractImageOrientation(InputStream is) throws IOException
8585
return 0;
8686
}
8787
int orientationTag = directory.getInt(ExifSubIFDDirectory.TAG_ORIENTATION);
88-
switch (orientationTag) {
89-
case 1:
90-
return 0;
91-
case 6:
92-
return 90;
93-
case 3:
94-
return 180;
95-
case 8:
96-
return 270;
97-
default:
88+
return switch (orientationTag) {
89+
case 1 -> 0;
90+
case 6 -> 90;
91+
case 3 -> 180;
92+
case 8 -> 270;
93+
default -> {
9894
log.warn("Unknown orientation tag: {}", orientationTag);
99-
return 0;
100-
}
95+
yield 0;
96+
}
97+
};
10198
} catch (ImageProcessingException | MetadataException e) {
10299
return 0;
103100
}

app/common/src/main/java/stirling/software/common/util/PdfUtils.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -631,16 +631,13 @@ public boolean pageCount(PDDocument pdfDocument, int pageCount, String comparato
631631
int actualPageCount = pdfDocument.getNumberOfPages();
632632
pdfDocument.close();
633633

634-
switch (comparator.toLowerCase()) {
635-
case "greater":
636-
return actualPageCount > pageCount;
637-
case "equal":
638-
return actualPageCount == pageCount;
639-
case "less":
640-
return actualPageCount < pageCount;
641-
default:
634+
return switch (comparator.toLowerCase()) {
635+
case "greater" -> actualPageCount > pageCount;
636+
case "equal" -> actualPageCount == pageCount;
637+
case "less" -> actualPageCount < pageCount;
638+
default ->
642639
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
643-
}
640+
};
644641
}
645642

646643
public boolean pageSize(PDDocument pdfDocument, String expectedPageSize) throws IOException {
@@ -662,9 +659,15 @@ public boolean pageSize(PDDocument pdfDocument, String expectedPageSize) throws
662659
return actualPageWidth == expectedPageWidth && actualPageHeight == expectedPageHeight;
663660
}
664661

665-
/** Key for storing the dimensions of a rendered image in a map. */
666-
private record PdfRenderSettingsKey(float mediaBoxWidth, float mediaBoxHeight, int rotation) {}
662+
/**
663+
* Key for storing the dimensions of a rendered image in a map.
664+
*/
665+
private record PdfRenderSettingsKey(float mediaBoxWidth, float mediaBoxHeight, int rotation) {
666+
}
667667

668-
/** Value for storing the dimensions of a rendered image in a map. */
669-
private record PdfImageDimensionValue(int width, int height) {}
668+
/**
669+
* Value for storing the dimensions of a rendered image in a map.
670+
*/
671+
private record PdfImageDimensionValue(int width, int height) {
672+
}
670673
}

app/core/src/main/java/stirling/software/SPDF/controller/api/RearrangePagesPDFController.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -205,30 +205,19 @@ private List<Integer> duplicate(int totalPages, String pageOrder) {
205205
private List<Integer> processSortTypes(String sortTypes, int totalPages, String pageOrder) {
206206
try {
207207
SortTypes mode = SortTypes.valueOf(sortTypes.toUpperCase());
208-
switch (mode) {
209-
case REVERSE_ORDER:
210-
return reverseOrder(totalPages);
211-
case DUPLEX_SORT:
212-
return duplexSort(totalPages);
213-
case BOOKLET_SORT:
214-
return bookletSort(totalPages);
215-
case SIDE_STITCH_BOOKLET_SORT:
216-
return sideStitchBooklet(totalPages);
217-
case ODD_EVEN_SPLIT:
218-
return oddEvenSplit(totalPages);
219-
case ODD_EVEN_MERGE:
220-
return oddEvenMerge(totalPages);
221-
case REMOVE_FIRST:
222-
return removeFirst(totalPages);
223-
case REMOVE_LAST:
224-
return removeLast(totalPages);
225-
case REMOVE_FIRST_AND_LAST:
226-
return removeFirstAndLast(totalPages);
227-
case DUPLICATE:
228-
return duplicate(totalPages, pageOrder);
229-
default:
230-
throw new IllegalArgumentException("Unsupported custom mode");
231-
}
208+
return switch (mode) {
209+
case REVERSE_ORDER -> reverseOrder(totalPages);
210+
case DUPLEX_SORT -> duplexSort(totalPages);
211+
case BOOKLET_SORT -> bookletSort(totalPages);
212+
case SIDE_STITCH_BOOKLET_SORT -> sideStitchBooklet(totalPages);
213+
case ODD_EVEN_SPLIT -> oddEvenSplit(totalPages);
214+
case ODD_EVEN_MERGE -> oddEvenMerge(totalPages);
215+
case REMOVE_FIRST -> removeFirst(totalPages);
216+
case REMOVE_LAST -> removeLast(totalPages);
217+
case REMOVE_FIRST_AND_LAST -> removeFirstAndLast(totalPages);
218+
case DUPLICATE -> duplicate(totalPages, pageOrder);
219+
default -> throw new IllegalArgumentException("Unsupported custom mode");
220+
};
232221
} catch (IllegalArgumentException e) {
233222
log.error("Unsupported custom mode", e);
234223
return null;

app/core/src/main/java/stirling/software/SPDF/controller/api/filters/FilterController.java

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,14 @@ public ResponseEntity<byte[]> pageCount(@ModelAttribute PDFComparisonAndCount re
8686
// Load the PDF
8787
PDDocument document = pdfDocumentFactory.load(inputFile);
8888
int actualPageCount = document.getNumberOfPages();
89-
90-
boolean valid;
9189
// Perform the comparison
92-
switch (comparator) {
93-
case "Greater":
94-
valid = actualPageCount > pageCount;
95-
break;
96-
case "Equal":
97-
valid = actualPageCount == pageCount;
98-
break;
99-
case "Less":
100-
valid = actualPageCount < pageCount;
101-
break;
102-
default:
90+
boolean valid = switch (comparator) {
91+
case "Greater" -> actualPageCount > pageCount;
92+
case "Equal" -> actualPageCount == pageCount;
93+
case "Less" -> actualPageCount < pageCount;
94+
default ->
10395
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
104-
}
96+
};
10597

10698
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
10799
return null;
@@ -130,21 +122,14 @@ public ResponseEntity<byte[]> pageSize(@ModelAttribute PageSizeRequest request)
130122
PDRectangle standardSize = PdfUtils.textToPageSize(standardPageSize);
131123
float standardArea = standardSize.getWidth() * standardSize.getHeight();
132124

133-
boolean valid;
134125
// Perform the comparison
135-
switch (comparator) {
136-
case "Greater":
137-
valid = actualArea > standardArea;
138-
break;
139-
case "Equal":
140-
valid = actualArea == standardArea;
141-
break;
142-
case "Less":
143-
valid = actualArea < standardArea;
144-
break;
145-
default:
126+
boolean valid = switch (comparator) {
127+
case "Greater" -> actualArea > standardArea;
128+
case "Equal" -> actualArea == standardArea;
129+
case "Less" -> actualArea < standardArea;
130+
default ->
146131
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
147-
}
132+
};
148133

149134
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
150135
return null;
@@ -163,21 +148,14 @@ public ResponseEntity<byte[]> fileSize(@ModelAttribute FileSizeRequest request)
163148
// Get the file size
164149
long actualFileSize = inputFile.getSize();
165150

166-
boolean valid;
167151
// Perform the comparison
168-
switch (comparator) {
169-
case "Greater":
170-
valid = actualFileSize > fileSize;
171-
break;
172-
case "Equal":
173-
valid = actualFileSize == fileSize;
174-
break;
175-
case "Less":
176-
valid = actualFileSize < fileSize;
177-
break;
178-
default:
152+
boolean valid = switch (comparator) {
153+
case "Greater" -> actualFileSize > fileSize;
154+
case "Equal" -> actualFileSize == fileSize;
155+
case "Less" -> actualFileSize < fileSize;
156+
default ->
179157
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
180-
}
158+
};
181159

182160
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
183161
return null;
@@ -199,21 +177,15 @@ public ResponseEntity<byte[]> pageRotation(@ModelAttribute PageRotationRequest r
199177
// Get the rotation of the first page
200178
PDPage firstPage = document.getPage(0);
201179
int actualRotation = firstPage.getRotation();
202-
boolean valid;
180+
203181
// Perform the comparison
204-
switch (comparator) {
205-
case "Greater":
206-
valid = actualRotation > rotation;
207-
break;
208-
case "Equal":
209-
valid = actualRotation == rotation;
210-
break;
211-
case "Less":
212-
valid = actualRotation < rotation;
213-
break;
214-
default:
182+
boolean valid = switch (comparator) {
183+
case "Greater" -> actualRotation > rotation;
184+
case "Equal" -> actualRotation == rotation;
185+
case "Less" -> actualRotation < rotation;
186+
default ->
215187
throw ExceptionUtils.createInvalidArgumentException("comparator", comparator);
216-
}
188+
};
217189

218190
if (valid) return WebResponseUtils.multiPartFileToWebResponse(inputFile);
219191
return null;

app/core/src/main/java/stirling/software/SPDF/controller/api/security/WatermarkController.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -170,27 +170,14 @@ private void addTextWatermark(
170170
throws IOException {
171171
String resourceDir = "";
172172
PDFont font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
173-
switch (alphabet) {
174-
case "arabic":
175-
resourceDir = "static/fonts/NotoSansArabic-Regular.ttf";
176-
break;
177-
case "japanese":
178-
resourceDir = "static/fonts/Meiryo.ttf";
179-
break;
180-
case "korean":
181-
resourceDir = "static/fonts/malgun.ttf";
182-
break;
183-
case "chinese":
184-
resourceDir = "static/fonts/SimSun.ttf";
185-
break;
186-
case "thai":
187-
resourceDir = "static/fonts/NotoSansThai-Regular.ttf";
188-
break;
189-
case "roman":
190-
default:
191-
resourceDir = "static/fonts/NotoSans-Regular.ttf";
192-
break;
193-
}
173+
resourceDir = switch (alphabet) {
174+
case "arabic" -> "static/fonts/NotoSansArabic-Regular.ttf";
175+
case "japanese" -> "static/fonts/Meiryo.ttf";
176+
case "korean" -> "static/fonts/malgun.ttf";
177+
case "chinese" -> "static/fonts/SimSun.ttf";
178+
case "thai" -> "static/fonts/NotoSansThai-Regular.ttf";
179+
default -> "static/fonts/NotoSans-Regular.ttf";
180+
};
194181

195182
ClassPathResource classPathResource = new ClassPathResource(resourceDir);
196183
String fileExtension = resourceDir.substring(resourceDir.lastIndexOf("."));

app/core/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,16 @@ private List<FontResource> getFontNamesFromLocation(String locationPattern) {
281281
}
282282

283283
public String getFormatFromExtension(String extension) {
284-
switch (extension) {
285-
case "ttf":
286-
return "truetype";
287-
case "woff":
288-
return "woff";
289-
case "woff2":
290-
return "woff2";
291-
case "eot":
292-
return "embedded-opentype";
293-
case "svg":
294-
return "svg";
295-
default:
284+
return switch (extension) {
285+
case "ttf" -> "truetype";
286+
case "woff" -> "woff";
287+
case "woff2" -> "woff2";
288+
case "eot" -> "embedded-opentype";
289+
case "svg" -> "svg";
290+
default ->
296291
// or throw an exception if an unexpected extension is encountered
297-
return "";
298-
}
292+
"";
293+
};
299294
}
300295

301296
@GetMapping("/crop")

0 commit comments

Comments
 (0)