Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edge: Simplify Logging and Implement Java 21 Switch Pattern Matching in BridgeHttpImpl #2964

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

Sn0w3y
Copy link
Contributor

@Sn0w3y Sn0w3y commented Jan 13, 2025

Overview

This pull request introduces two key enhancements to the BridgeHttpImpl class within the io.openems.edge.bridge.http package:

  1. Simplified Logging:

    • Removed non-essential info level log statements to reduce log clutter and focus on critical log messages.
  2. Modernized Control Flow with Java 21:

    • Refactored if-else constructs to leverage Java 21's enhanced switch statement with pattern matching, improving code readability and maintainability.

Detailed Changes

  1. Simplified Logging:

    • Files Affected:

      • BridgeHttpImpl.java
    • Changes Made:

      • Removed info Level Logs:
        • Eliminated the following log statements from the handleEvent method:
          this.log.info("Process for " + item.cycleEndpoint + " is still running. Task is not queued twice");
          this.log.info("Unable to re-add " + item + " to queue again.");
      • Rationale:
        • These logs were identified as non-critical and removing them helps in focusing on more important warn and error logs, enhancing log clarity.
  2. Java 21 Switch Pattern Matching:

    • Files Affected:

      • BridgeHttpImpl.java
    • Changes Made:

      • Refactored createTask(TimeEndpointCountdown endpointCountdown) Method:
        • Replaced existing if-else statements with a switch statement utilizing Java 21's pattern matching.

        • Before:

          if (nextDelay instanceof Delay.InfiniteDelay) {
              // do not queue again
              return;
          } else if (nextDelay instanceof Delay.DurationDelay durationDelay) {
              final var future = this.pool.schedule(this.createTask(endpointCountdown), durationDelay);
              endpointCountdown.setShutdownCurrentTask(() -> future.cancel(false));
          }
        • After:

          switch (nextDelay) {
              case Delay.InfiniteDelay infiniteDelay -> {
                  // do not queue again
                  return;
              }
              case Delay.DurationDelay durationDelay -> {
                  final var future = this.pool.schedule(this.createTask(endpointCountdown), durationDelay);
                  endpointCountdown.setShutdownCurrentTask(() -> future.cancel(false));
              }
              default -> {
                  this.log.warn("Unhandled Delay type: " + nextDelay.getClass().getName());
              }
          }
        • Rationale:

          • Enhanced Readability: The switch statement clearly outlines different Delay types, making the logic easier to follow.
          • Maintainability: Simplifies the addition of new Delay subclasses in the future.
          • Robustness: Introduced a default case to handle any unforeseen Delay types, ensuring the system remains stable.

Benefits

  1. Improved Logging Efficiency:

    • Reduced Log Noise: By removing non-critical info logs, the logging output is cleaner, making it easier to monitor and debug essential issues.
  2. Modernized and Cleaner Codebase:

    • Utilization of Java 21 Features: Leveraging the latest Java features enhances code quality and aligns the project with modern Java practices.
    • Enhanced Readability and Maintainability: The refactored switch statement offers a more intuitive structure, facilitating easier understanding and future modifications.
  3. Future-Proofing:

    • Scalability: The updated control flow accommodates future extensions with minimal changes.
    • Error Handling: The default case in the switch statement ensures that unexpected Delay types are logged and handled gracefully.

Conclusion

This pull request streamlines the BridgeHttpImpl class by eliminating unnecessary logging and adopting Java 21's advanced switch pattern matching. These changes enhance both the efficiency and maintainability of the codebase, positioning it for future growth and easier debugging.


Copy link

codecov bot commented Jan 13, 2025

Codecov Report

Attention: Patch coverage is 20.00000% with 4 lines in your changes missing coverage. Please review.

Additional details and impacted files
@@              Coverage Diff              @@
##             develop    #2964      +/-   ##
=============================================
- Coverage      57.08%   57.08%   -0.00%     
  Complexity      9729     9729              
=============================================
  Files           2266     2266              
  Lines          96819    96821       +2     
  Branches        7163     7162       -1     
=============================================
- Hits           55260    55258       -2     
- Misses         39498    39502       +4     
  Partials        2061     2061              

@Sn0w3y
Copy link
Contributor Author

Sn0w3y commented Jan 13, 2025

@sfeilmeier could you have a look ? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant