This is an automated email from the ASF dual-hosted git repository.

He-Pin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko-http.git


The following commit(s) were added to refs/heads/main by this push:
     new 1566cffec refactor: migrate switch statements to modern switch syntax 
(Java 14+) (#1092)
1566cffec is described below

commit 1566cffec29e5815fc334db75f38830021c7afc6
Author: He-Pin(kerr) <[email protected]>
AuthorDate: Fri Jun 26 17:12:57 2026 +0800

    refactor: migrate switch statements to modern switch syntax (Java 14+) 
(#1092)
    
    * refactor: migrate switch statements to modern switch syntax (Java 14+)
    
    Motivation:
    Java 14 introduced enhanced switch statements and switch expressions
    with arrow syntax, eliminating fall-through bugs and reducing boilerplate.
    
    Modification:
    Convert switch statements to switch expressions and enhanced switch
    statements in MarshallerTest, OversizedSseStrategy, and Jackson
    marshaller classes.
    
    Result:
    Cleaner, more concise switch code with no fall-through risk.
    
    * fix: apply javafmt formatting to switch expressions
    
    Motivation:
    CI javafmt check failed on the switch expression migration PR.
    
    Modification:
    Run sbt javafmtAll to fix formatting in Jackson.java (jackson and
    jackson3 modules) and MarshallerTest.java.
    
    Result:
    javafmt CI check passes.
---
 .../http/javadsl/marshallers/jackson/Jackson.java  | 23 +++----
 .../http/javadsl/marshallers/jackson3/Jackson.java | 23 +++----
 .../pekko/http/javadsl/server/MarshallerTest.java  | 72 +++++++++-------------
 .../javadsl/settings/OversizedSseStrategy.java     | 22 +++----
 4 files changed, 57 insertions(+), 83 deletions(-)

diff --git 
a/http-marshallers-java/http-jackson/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson/Jackson.java
 
b/http-marshallers-java/http-jackson/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson/Jackson.java
index c8ae81c52..e623fb971 100644
--- 
a/http-marshallers-java/http-jackson/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson/Jackson.java
+++ 
b/http-marshallers-java/http-jackson/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson/Jackson.java
@@ -123,19 +123,14 @@ public class Jackson {
 
   private static RecyclerPool<BufferRecycler> getBufferRecyclerPool(final 
Config cfg) {
     final String poolType = cfg.getString("buffer-recycler.pool-instance");
-    switch (poolType) {
-      case "thread-local":
-        return JsonRecyclerPools.threadLocalPool();
-      case "concurrent-deque":
-        return JsonRecyclerPools.newConcurrentDequePool();
-      case "shared-concurrent-deque":
-        return JsonRecyclerPools.sharedConcurrentDequePool();
-      case "bounded":
-        return 
JsonRecyclerPools.newBoundedPool(cfg.getInt("buffer-recycler.bounded-pool-size"));
-      case "non-recycling":
-        return JsonRecyclerPools.nonRecyclingPool();
-      default:
-        throw new IllegalArgumentException("Unknown recycler-pool: " + 
poolType);
-    }
+    return switch (poolType) {
+      case "thread-local" -> JsonRecyclerPools.threadLocalPool();
+      case "concurrent-deque" -> JsonRecyclerPools.newConcurrentDequePool();
+      case "shared-concurrent-deque" -> 
JsonRecyclerPools.sharedConcurrentDequePool();
+      case "bounded" ->
+          
JsonRecyclerPools.newBoundedPool(cfg.getInt("buffer-recycler.bounded-pool-size"));
+      case "non-recycling" -> JsonRecyclerPools.nonRecyclingPool();
+      default -> throw new IllegalArgumentException("Unknown recycler-pool: " 
+ poolType);
+    };
   }
 }
diff --git 
a/http-marshallers-java/http-jackson3/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson3/Jackson.java
 
b/http-marshallers-java/http-jackson3/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson3/Jackson.java
index f57646474..dbee58222 100644
--- 
a/http-marshallers-java/http-jackson3/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson3/Jackson.java
+++ 
b/http-marshallers-java/http-jackson3/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson3/Jackson.java
@@ -129,19 +129,14 @@ public class Jackson {
 
   private static RecyclerPool<BufferRecycler> getBufferRecyclerPool(final 
Config cfg) {
     final String poolType = cfg.getString("buffer-recycler.pool-instance");
-    switch (poolType) {
-      case "thread-local":
-        return JsonRecyclerPools.threadLocalPool();
-      case "concurrent-deque":
-        return JsonRecyclerPools.newConcurrentDequePool();
-      case "shared-concurrent-deque":
-        return JsonRecyclerPools.sharedConcurrentDequePool();
-      case "bounded":
-        return 
JsonRecyclerPools.newBoundedPool(cfg.getInt("buffer-recycler.bounded-pool-size"));
-      case "non-recycling":
-        return JsonRecyclerPools.nonRecyclingPool();
-      default:
-        throw new IllegalArgumentException("Unknown recycler-pool: " + 
poolType);
-    }
+    return switch (poolType) {
+      case "thread-local" -> JsonRecyclerPools.threadLocalPool();
+      case "concurrent-deque" -> JsonRecyclerPools.newConcurrentDequePool();
+      case "shared-concurrent-deque" -> 
JsonRecyclerPools.sharedConcurrentDequePool();
+      case "bounded" ->
+          
JsonRecyclerPools.newBoundedPool(cfg.getInt("buffer-recycler.bounded-pool-size"));
+      case "non-recycling" -> JsonRecyclerPools.nonRecyclingPool();
+      default -> throw new IllegalArgumentException("Unknown recycler-pool: " 
+ poolType);
+    };
   }
 }
diff --git 
a/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/MarshallerTest.java
 
b/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/MarshallerTest.java
index ec0df9e69..b8909088b 100644
--- 
a/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/MarshallerTest.java
+++ 
b/http-tests/src/test/java/org/apache/pekko/http/javadsl/server/MarshallerTest.java
@@ -34,22 +34,15 @@ public class MarshallerTest extends JUnitJupiterRouteTest {
     final Marshaller<Integer, RequestEntity> numberAsNameMarshaller =
         Marshaller.wrapEntity(
             (Integer param) -> {
-              switch (param) {
-                case 0:
-                  return "null";
-                case 1:
-                  return "eins";
-                case 2:
-                  return "zwei";
-                case 3:
-                  return "drei";
-                case 4:
-                  return "vier";
-                case 5:
-                  return "fünf";
-                default:
-                  return "wat?";
-              }
+              return switch (param) {
+                case 0 -> "null";
+                case 1 -> "eins";
+                case 2 -> "zwei";
+                case 3 -> "drei";
+                case 4 -> "vier";
+                case 5 -> "fünf";
+                default -> "wat?";
+              };
             },
             Marshaller.stringToEntity(),
             MediaTypes.TEXT_X_SPEECH);
@@ -95,14 +88,11 @@ public class MarshallerTest extends JUnitJupiterRouteTest {
     final Marshaller<Integer, RequestEntity> numberAsJsonListMarshaller =
         Marshaller.wrapEntity(
             (Integer param) -> {
-              switch (param) {
-                case 1:
-                  return ByteString.fromString("[1]");
-                case 5:
-                  return ByteString.fromString("[1,2,3,4,5]");
-                default:
-                  return ByteString.fromString("[]");
-              }
+              return switch (param) {
+                case 1 -> ByteString.fromString("[1]");
+                case 5 -> ByteString.fromString("[1,2,3,4,5]");
+                default -> ByteString.fromString("[]");
+              };
             },
             Marshaller.byteStringToEntity(),
             MediaTypes.APPLICATION_JSON);
@@ -142,15 +132,12 @@ public class MarshallerTest extends JUnitJupiterRouteTest 
{
         Marshaller.withFixedContentType(
             MediaTypes.APPLICATION_JSON.toContentType(),
             (Integer param) -> {
-              switch (param) {
-                case 1:
-                  return 
HttpEntities.create(MediaTypes.APPLICATION_JSON.toContentType(), "[1]");
-                case 5:
-                  return HttpEntities.create(
-                      MediaTypes.APPLICATION_JSON.toContentType(), 
"[1,2,3,4,5]");
-                default:
-                  return 
HttpEntities.create(MediaTypes.APPLICATION_JSON.toContentType(), "[]");
-              }
+              return switch (param) {
+                case 1 -> 
HttpEntities.create(MediaTypes.APPLICATION_JSON.toContentType(), "[1]");
+                case 5 ->
+                    
HttpEntities.create(MediaTypes.APPLICATION_JSON.toContentType(), "[1,2,3,4,5]");
+                default -> 
HttpEntities.create(MediaTypes.APPLICATION_JSON.toContentType(), "[]");
+              };
             });
 
     final Function<Integer, Route> nummerHandler =
@@ -188,16 +175,15 @@ public class MarshallerTest extends JUnitJupiterRouteTest 
{
         Marshaller.withFixedContentType(
             MediaTypes.APPLICATION_JSON.toContentType(),
             (Integer param) -> {
-              switch (param) {
-                case 1:
-                  return HttpResponse.create()
-                      .withEntity(MediaTypes.APPLICATION_JSON.toContentType(), 
"[1]");
-                case 5:
-                  return HttpResponse.create()
-                      .withEntity(MediaTypes.APPLICATION_JSON.toContentType(), 
"[1,2,3,4,5]");
-                default:
-                  return 
HttpResponse.create().withStatus(StatusCodes.NOT_FOUND);
-              }
+              return switch (param) {
+                case 1 ->
+                    HttpResponse.create()
+                        
.withEntity(MediaTypes.APPLICATION_JSON.toContentType(), "[1]");
+                case 5 ->
+                    HttpResponse.create()
+                        
.withEntity(MediaTypes.APPLICATION_JSON.toContentType(), "[1,2,3,4,5]");
+                default -> 
HttpResponse.create().withStatus(StatusCodes.NOT_FOUND);
+              };
             });
 
     final Function<Integer, Route> nummerHandler =
diff --git 
a/http/src/main/java/org/apache/pekko/http/javadsl/settings/OversizedSseStrategy.java
 
b/http/src/main/java/org/apache/pekko/http/javadsl/settings/OversizedSseStrategy.java
index 6a636a094..28e8fed21 100644
--- 
a/http/src/main/java/org/apache/pekko/http/javadsl/settings/OversizedSseStrategy.java
+++ 
b/http/src/main/java/org/apache/pekko/http/javadsl/settings/OversizedSseStrategy.java
@@ -43,18 +43,16 @@ public enum OversizedSseStrategy {
 
   /** Convert this Java enum to the corresponding Scala enum value. */
   public org.apache.pekko.http.scaladsl.settings.OversizedSseStrategy 
asScala() {
-    switch (this) {
-      case FailStream:
-        return 
org.apache.pekko.http.scaladsl.settings.OversizedSseStrategy.FailStream$.MODULE$;
-      case LogAndSkip:
-        return 
org.apache.pekko.http.scaladsl.settings.OversizedSseStrategy.LogAndSkip$.MODULE$;
-      case Truncate:
-        return 
org.apache.pekko.http.scaladsl.settings.OversizedSseStrategy.Truncate$.MODULE$;
-      case DeadLetter:
-        return 
org.apache.pekko.http.scaladsl.settings.OversizedSseStrategy.DeadLetter$.MODULE$;
-      default:
-        throw new IllegalArgumentException("Unknown OversizedSseStrategy: " + 
this);
-    }
+    return switch (this) {
+      case FailStream ->
+          
org.apache.pekko.http.scaladsl.settings.OversizedSseStrategy.FailStream$.MODULE$;
+      case LogAndSkip ->
+          
org.apache.pekko.http.scaladsl.settings.OversizedSseStrategy.LogAndSkip$.MODULE$;
+      case Truncate ->
+          
org.apache.pekko.http.scaladsl.settings.OversizedSseStrategy.Truncate$.MODULE$;
+      case DeadLetter ->
+          
org.apache.pekko.http.scaladsl.settings.OversizedSseStrategy.DeadLetter$.MODULE$;
+    };
   }
 
   /** Convert from a Scala enum value to the corresponding Java enum value. */


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to