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

lidongdai pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new c8ab27a  Optimization of enumerated values method (#2677)
c8ab27a is described below

commit c8ab27acb0cd6d0b4e251a2d4cf7e4a7aa33ae89
Author: CalvinKirs <[email protected]>
AuthorDate: Sun May 31 10:29:46 2020 +0800

    Optimization of enumerated values method (#2677)
    
    * The value method of enum is a deep copy. If there are many parameters, it 
is not recommended to use the value method. Specifically see jmh results
    
    * beautiful misunderstanding
    
    Co-authored-by: dailidong <[email protected]>
---
 .../dolphinscheduler/common/enums/CommandType.java   | 20 +++++++++++++++-----
 .../apache/dolphinscheduler/common/enums/DbType.java | 16 ++++++++++++----
 .../common/enums/ExecutionStatus.java                | 17 ++++++++++++-----
 3 files changed, 39 insertions(+), 14 deletions(-)

diff --git 
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/CommandType.java
 
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/CommandType.java
index 56fdd07..9682016 100644
--- 
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/CommandType.java
+++ 
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/CommandType.java
@@ -18,6 +18,9 @@ package org.apache.dolphinscheduler.common.enums;
 
 import com.baomidou.mybatisplus.annotation.EnumValue;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * command types
  */
@@ -66,11 +69,18 @@ public enum CommandType {
         return descp;
     }
 
-    public static CommandType of(Integer status){
-        for(CommandType cmdType : values()){
-            if(cmdType.getCode() == status){
-                return cmdType;
-            }
+    private static final Map<Integer, CommandType> COMMAND_TYPE_MAP = new 
HashMap<>();
+
+    static {
+        for (CommandType commandType : CommandType.values()) {
+            COMMAND_TYPE_MAP.put(commandType.code,commandType);
+        }
+    }
+
+
+    public static CommandType of(Integer status) {
+        if (COMMAND_TYPE_MAP.containsKey(status)) {
+            return COMMAND_TYPE_MAP.get(status);
         }
         throw new IllegalArgumentException("invalid status : " + status);
     }
diff --git 
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/DbType.java
 
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/DbType.java
index cc3a295..1d28a75 100644
--- 
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/DbType.java
+++ 
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/DbType.java
@@ -18,6 +18,8 @@ package org.apache.dolphinscheduler.common.enums;
 
 import com.baomidou.mybatisplus.annotation.EnumValue;
 
+import java.util.HashMap;
+
 /**
  * data base types
  */
@@ -59,11 +61,17 @@ public enum DbType {
     }
 
 
+    private static HashMap<Integer, DbType> DB_TYPE_MAP =new HashMap<>();
+
+    static {
+        for (DbType dbType:DbType.values()){
+            DB_TYPE_MAP.put(dbType.getCode(),dbType);
+        }
+    }
+
     public static DbType of(int type){
-        for(DbType ty : values()){
-            if(ty.getCode() == type){
-                return ty;
-            }
+        if(DB_TYPE_MAP.containsKey(type)){
+            return DB_TYPE_MAP.get(type);
         }
         throw new IllegalArgumentException("invalid type : " + type);
     }
diff --git 
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/ExecutionStatus.java
 
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/ExecutionStatus.java
index ce141d0..0f8c9f5 100644
--- 
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/ExecutionStatus.java
+++ 
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/ExecutionStatus.java
@@ -19,6 +19,8 @@ package org.apache.dolphinscheduler.common.enums;
 
 import com.baomidou.mybatisplus.annotation.EnumValue;
 
+import java.util.HashMap;
+
 /**
  * running status for workflow and task nodes
  *
@@ -62,6 +64,13 @@ public enum ExecutionStatus {
     private final int code;
     private final String descp;
 
+    private static HashMap<Integer, ExecutionStatus> EXECUTION_STATUS_MAP=new 
HashMap<>();
+
+    static {
+       for (ExecutionStatus executionStatus:ExecutionStatus.values()){
+           EXECUTION_STATUS_MAP.put(executionStatus.code,executionStatus);
+       }
+    }
 
  /**
   * status is success
@@ -130,11 +139,9 @@ public enum ExecutionStatus {
     }
 
     public static ExecutionStatus of(int status){
-        for(ExecutionStatus es : values()){
-            if(es.getCode() == status){
-                return es;
-            }
-        }
+       if(EXECUTION_STATUS_MAP.containsKey(status)){
+           return EXECUTION_STATUS_MAP.get(status);
+       }
         throw new IllegalArgumentException("invalid status : " + status);
     }
 }

Reply via email to