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

mikexue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/eventmesh.git


The following commit(s) were added to refs/heads/master by this push:
     new 2cb5aa941 [ISSUE #4269] Used switch to replace the if-else. 
[MeshMessageProtocolAdaptor] (#4270)
2cb5aa941 is described below

commit 2cb5aa94153b9b2762b0ff46c97c19d6e9b4a4e2
Author: Sam V Jose <[email protected]>
AuthorDate: Tue Jul 25 07:58:42 2023 +0530

    [ISSUE #4269] Used switch to replace the if-else. 
[MeshMessageProtocolAdaptor] (#4270)
    
    * [ISSUE #4269] Used switch to replace the if-else. 
[MeshMessageProtocolAdaptor]
    
    * [ISSUE #4269] Used switch to replace the if-else. 
[MeshMessageProtocolAdaptor]
    
    * [ISSUE #4269] Used switch to replace the if-else. 
[MeshMessageProtocolAdaptor]
    
    * [ISSUE #4269] Used switch to replace the if-else. 
[MeshMessageProtocolAdaptor]
---
 .../meshmessage/MeshMessageProtocolAdaptor.java    | 74 +++++++++++-----------
 1 file changed, 37 insertions(+), 37 deletions(-)

diff --git 
a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/MeshMessageProtocolAdaptor.java
 
b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/MeshMessageProtocolAdaptor.java
index 73c71784a..663718e5f 100644
--- 
a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/MeshMessageProtocolAdaptor.java
+++ 
b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/MeshMessageProtocolAdaptor.java
@@ -37,12 +37,10 @@ import 
org.apache.eventmesh.protocol.meshmessage.resolver.http.SendMessageBatchV
 import 
org.apache.eventmesh.protocol.meshmessage.resolver.http.SendMessageRequestProtocolResolver;
 import 
org.apache.eventmesh.protocol.meshmessage.resolver.tcp.TcpMessageProtocolResolver;
 
-import org.apache.commons.lang3.StringUtils;
-
-import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import io.cloudevents.CloudEvent;
 
@@ -77,19 +75,19 @@ public class MeshMessageProtocolAdaptor implements 
ProtocolAdaptor<ProtocolTrans
     }
 
     private CloudEvent deserializeHttpProtocol(String requestCode,
-        org.apache.eventmesh.common.protocol.http.header.Header header,
-        Body body) throws ProtocolHandleException {
-
-        if 
(String.valueOf(RequestCode.MSG_BATCH_SEND.getRequestCode()).equals(requestCode))
 {
-            return SendMessageBatchProtocolResolver.buildEvent(header, body);
-        } else if 
(String.valueOf(RequestCode.MSG_BATCH_SEND_V2.getRequestCode()).equals(requestCode))
 {
-            return SendMessageBatchV2ProtocolResolver.buildEvent(header, body);
-        } else if 
(String.valueOf(RequestCode.MSG_SEND_SYNC.getRequestCode()).equals(requestCode))
 {
-            return SendMessageRequestProtocolResolver.buildEvent(header, body);
-        } else if 
(String.valueOf(RequestCode.MSG_SEND_ASYNC.getRequestCode()).equals(requestCode))
 {
-            return SendMessageRequestProtocolResolver.buildEvent(header, body);
-        } else {
-            throw new ProtocolHandleException(String.format("unsupported 
requestCode: %s", requestCode));
+                                               
org.apache.eventmesh.common.protocol.http.header.Header header,
+                                               Body body) throws 
ProtocolHandleException {
+
+        switch (RequestCode.valueOf(requestCode)) {
+            case MSG_BATCH_SEND:
+                return SendMessageBatchProtocolResolver.buildEvent(header, 
body);
+            case MSG_BATCH_SEND_V2:
+                return SendMessageBatchV2ProtocolResolver.buildEvent(header, 
body);
+            case MSG_SEND_SYNC:
+            case MSG_SEND_ASYNC:
+                return SendMessageRequestProtocolResolver.buildEvent(header, 
body);
+            default:
+                throw new ProtocolHandleException(String.format("unsupported 
requestCode: %s", requestCode));
         }
 
     }
@@ -110,29 +108,31 @@ public class MeshMessageProtocolAdaptor implements 
ProtocolAdaptor<ProtocolTrans
         String protocolDesc =
             cloudEvent.getExtension(Constants.PROTOCOL_DESC) == null ? null : 
cloudEvent.getExtension(Constants.PROTOCOL_DESC).toString();
 
-        if (StringUtils.equals(MeshMessageProtocolConstant.PROTOCOL_DESC_HTTP, 
protocolDesc)) {
-            HttpCommand httpCommand = new HttpCommand();
-            Body body = new Body() {
-                final Map<String, Object> map = new HashMap<>();
-
-                @Override
-                public Map<String, Object> toMap() {
-                    if (cloudEvent.getData() == null) {
+        switch (Objects.requireNonNull(protocolDesc)) {
+            case MeshMessageProtocolConstant.PROTOCOL_DESC_HTTP:
+                HttpCommand httpCommand = new HttpCommand();
+                Body body = new Body() {
+                    final Map<String, Object> map = new HashMap<>();
+
+                    @Override
+                    public Map<String, Object> toMap() {
+                        if (cloudEvent.getData() == null) {
+                            return map;
+                        }
+                        
map.put(MeshMessageProtocolConstant.PROTOCOL_KEY_CONTENT,
+                            new String(cloudEvent.getData().toBytes(), 
Constants.DEFAULT_CHARSET));
                         return map;
                     }
-                    map.put(MeshMessageProtocolConstant.PROTOCOL_KEY_CONTENT, 
new String(cloudEvent.getData().toBytes(), StandardCharsets.UTF_8));
-                    return map;
-                }
-            };
-            body.toMap();
-            httpCommand.setBody(body);
-            return httpCommand;
-        } else if 
(StringUtils.equals(MeshMessageProtocolConstant.PROTOCOL_DESC_TCP, 
protocolDesc)) {
-            return 
TcpMessageProtocolResolver.buildEventMeshMessage(cloudEvent);
-        } else if 
(StringUtils.equals(MeshMessageProtocolConstant.PROTOCOL_DESC_GRPC_CLOUD_EVENT, 
protocolDesc)) {
-            return 
GrpcEventMeshCloudEventProtocolResolver.buildEventMeshCloudEvent(cloudEvent);
-        } else {
-            throw new ProtocolHandleException(String.format("Unsupported 
protocolDesc: %s", protocolDesc));
+                };
+                body.toMap();
+                httpCommand.setBody(body);
+                return httpCommand;
+            case MeshMessageProtocolConstant.PROTOCOL_DESC_TCP:
+                return 
TcpMessageProtocolResolver.buildEventMeshMessage(cloudEvent);
+            case MeshMessageProtocolConstant.PROTOCOL_DESC_GRPC_CLOUD_EVENT:
+                return 
GrpcEventMeshCloudEventProtocolResolver.buildEventMeshCloudEvent(cloudEvent);
+            default:
+                throw new ProtocolHandleException(String.format("Unsupported 
protocolDesc: %s", protocolDesc));
         }
     }
 


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

Reply via email to