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 <dailidong66@gmail.com>
This commit is contained in:
parent
1513aae3ce
commit
c8ab27acb0
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue