From f0b58321ae53f330da86c392661354b87bd9a412 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 20 Jun 2025 20:04:02 +0300 Subject: Modernize codebase to use Java 21 features MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Convert VS3Tupel and VSLamportTime to records for immutability - Use switch expressions with pattern matching in VSTimestampTriggeredEvent - Modernize exception handling with pattern matching in VSErrorHandler - Replace anonymous ActionListener with lambda in VSAboutFrame - Use formatted strings instead of concatenation in VSDummyProtocol - Add sealed hierarchy VSEventType for exhaustive pattern matching - Create VSSimulationConfig record for configuration management - Maintain backward compatibility with deprecated methods All 132 unit tests pass successfully with Java 21 features. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/main/java/exceptions/VSErrorHandler.java | 45 ++++++++++------------------ 1 file changed, 16 insertions(+), 29 deletions(-) (limited to 'src/main/java/exceptions') diff --git a/src/main/java/exceptions/VSErrorHandler.java b/src/main/java/exceptions/VSErrorHandler.java index 59085f0..3d2e2ca 100644 --- a/src/main/java/exceptions/VSErrorHandler.java +++ b/src/main/java/exceptions/VSErrorHandler.java @@ -84,22 +84,16 @@ public final class VSErrorHandler { * @param exception the exception to display */ private static void showErrorDialog(Exception exception) { - String title = "Simulator Error"; - String message = exception.getMessage(); - - if (exception instanceof VSSimulatorException) { - // Use more specific title for our exceptions - if (exception instanceof VSConfigurationException) { - title = "Configuration Error"; - } else if (exception instanceof VSProcessException) { - title = "Process Error"; - } else if (exception instanceof VSProtocolException) { - title = "Protocol Error"; - } else if (exception instanceof VSSerializationException) { - title = "Save/Load Error"; - } - } + String title = switch (exception) { + case VSConfigurationException e -> "Configuration Error"; + case VSProcessException e -> "Process Error"; + case VSProtocolException e -> "Protocol Error"; + case VSSerializationException e -> "Save/Load Error"; + case VSSimulatorException e -> "Simulator Error"; + default -> "Simulator Error"; + }; + String message = exception.getMessage(); if (message == null || message.isEmpty()) { message = "An unexpected error occurred: " + exception.getClass().getSimpleName(); } @@ -130,19 +124,12 @@ public final class VSErrorHandler { * @return a VSSimulatorException wrapping the original exception */ public static VSSimulatorException wrap(Exception e, String context) { - if (e instanceof VSSimulatorException) { - return (VSSimulatorException) e; - } - - // Wrap common exceptions with more specific types - if (e instanceof java.io.IOException) { - return new VSSerializationException(context, e); - } else if (e instanceof NumberFormatException) { - return new VSConfigurationException("Invalid number format in " + context, e); - } else if (e instanceof NullPointerException) { - return new VSSimulatorException("Null value encountered in " + context, e); - } else { - return new VSSimulatorException(context + ": " + e.getMessage(), e); - } + return switch (e) { + case VSSimulatorException vse -> vse; + case java.io.IOException ioe -> new VSSerializationException(context, ioe); + case NumberFormatException nfe -> new VSConfigurationException("Invalid number format in " + context, nfe); + case NullPointerException npe -> new VSSimulatorException("Null value encountered in " + context, npe); + default -> new VSSimulatorException(context + ": " + e.getMessage(), e); + }; } } \ No newline at end of file -- cgit v1.2.3