diff options
| author | Paul Buetow <paul@buetow.org> | 2025-06-20 20:04:02 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-06-20 20:04:02 +0300 |
| commit | f0b58321ae53f330da86c392661354b87bd9a412 (patch) | |
| tree | c0be72f57094d13cf8bcb53567614258a6eb43fa /src/main/java/exceptions | |
| parent | c3b95267b24d843897b04d6d6a16f62dc8cf1ed2 (diff) | |
Modernize codebase to use Java 21 features
- 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 <noreply@anthropic.com>
Diffstat (limited to 'src/main/java/exceptions')
| -rw-r--r-- | src/main/java/exceptions/VSErrorHandler.java | 45 |
1 files changed, 16 insertions, 29 deletions
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 |
