1.首先認識一下EnumMap
import cn.chinaunicom.consts.FirstEnum;
import cn.chinaunicom.consts.TEnum;
import cn.chinaunicom.pojo.vo.one.v3.Son;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
/**
* @author chenzhen
* Created by chenzhen on 2018/11/26.
*/
public class EnumTest {
@Test
public void testEnum() {
List enumList = Lists.newArrayList();
enumList.addAll(Lists.newArrayList(FirstEnum.values()));
}
static class TEnumMap {
private EnumMap<FirstEnum, Map<String, String>> enumMap = new EnumMap<>(FirstEnum.class);
{
Map<String, String> map1 = Maps.newHashMap();
map1.put("服務1", "ServerOne");
map1.put("服務2", "ServerTwo");
map1.put("服務3", "ServerThree");
enumMap.put(FirstEnum.APP_SERVICE, map1);
Map<String, String> map2 = Maps.newHashMap();
map2.put("資産1", "AssertOne");
map2.put("資産2", "AssertTwo");
map2.put("資産3", "AssertThree");
enumMap.put(FirstEnum.ASSERT, map2);
}
}
@Test
public void testTEnumMap() {
TEnumMap tEnum = new TEnumMap();
for (Map.Entry<FirstEnum, Map<String, String>> e : tEnum.enumMap.entrySet()) {
System.out.println(e.getKey());
for (Map.Entry<String, String> m : e.getValue().entrySet()) {
System.out.print(m.getKey() + "\t" + m.getValue());
}
System.out.println();
}
}
}
2.貼上需要優化的代碼片段
private ArrayList<Son> getSons(FirstEnum firstEnum) {
switch (firstEnum){
case APP_SERVICE:{
ArrayList<Son> sonArrayList = Lists.newArrayList();
//app_service
for(AppServiceEnum appServiceEnum : AppServiceEnum.values()){
Son son = Son.builder()
.id(appServiceEnum.getDescription())
.number(TypeNumberCache.getKey(appServiceEnum.getIdentification()))
.healthy(TypeNumberCache.getKey(appServiceEnum.getIdentification()))
.unHealthy(0)
.build();
sonArrayList.add(son);
}
return sonArrayList;
}
case ASSERT:{
ArrayList<Son> sonArrayList = Lists.newArrayList();
//app_service
for(AssertEnum assertEnum : AssertEnum.values()){
Son son = Son.builder()
.id(assertEnum.getDescription())
.number(TypeNumberCache.getKey(assertEnum.getIdentification()))
.healthy(TypeNumberCache.getKey(assertEnum.getIdentification()))
.unHealthy(0)
.build();
sonArrayList.add(son);
}
return sonArrayList;
}
case INSTANCE_NODE:{
ArrayList<Son> sonArrayList = Lists.newArrayList();
//app_service
for(InstanceNodeEnum instanceNode : InstanceNodeEnum.values()){
Son son = Son.builder()
.id(instanceNode.getDescription())
.number(TypeNumberCache.getKey(instanceNode.getIdentification()))
.healthy(TypeNumberCache.getKey(instanceNode.getIdentification()))
.unHealthy(0)
.build();
sonArrayList.add(son);
}
return sonArrayList;
}
case CONFIG_INFO:{
ArrayList<Son> sonArrayList = Lists.newArrayList();
//config_info
for(ConfigInfoEnum configInfoEnum : ConfigInfoEnum.values()){
Son son = Son.builder()
.id(configInfoEnum.getDescription())
.number(TypeNumberCache.getKey(configInfoEnum.getIdentification()))
.healthy(TypeNumberCache.getKey(configInfoEnum.getIdentification()))
.unHealthy(0)
.build();
sonArrayList.add(son);
}
return sonArrayList;
}
case CLUSTER:{
ArrayList<Son> sonArrayList = Lists.newArrayList();
for(ClusterEnum clusterEnum : ClusterEnum.values()){
Son son = Son.builder()
.id(clusterEnum.getDescription())
.number(TypeNumberCache.getKey(clusterEnum.getIdentification()))
.healthy(TypeNumberCache.getKey(clusterEnum.getIdentification()))
.unHealthy(0)
.build();
sonArrayList.add(son);
}
return sonArrayList;
}
default:{
}
}
return Lists.newArrayList();
}
3.上面代碼涉及到的枚舉類(FirstEnum,AppServiceEnum,AssertEnum,ClusterEnum,ConfigInfoEnum,InstanceNodeEnum)
/**
* v3支撐
* @author chenzhen
* Created by chenzhen on 2018/11/19.
*/
public enum FirstEnum {
/**
* 首層展示支援,V3版本
*/
APP_SERVICE(1,"服務","app_server"),
ASSERT(2,"資産","Asset"),
INSTANCE_NODE(3,"節點","Instancenode"),
CLUSTER(4,"叢集","app_server_cluster"),
CONFIG_INFO(5,"配置","Configinfo");
int code;
String description;
String identification;
FirstEnum(int code, String description, String identification) {
this.code = code;
this.description = description;
this.identification = identification;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIdentification() {
return identification;
}
public void setIdentification(String identification) {
this.identification = identification;
}
}
/**
* v3支撐
* @author chenzhen
* Created by chenzhen on 2018/11/19.
*/
public enum AppServiceEnum {
/**
* 服務類型枚舉
*/
TuxedoService(1,"tuxedo服務", "tuxedo_app_server"),
MicroServiceRestful(2,"微服務接口服務", "microServiceRestfull_app_server"),
WeblogicService(3,"weblogic服務", "weblogic_app_server");
int code;
String description;
String identification;
AppServiceEnum(int id, String description, String identification) {
this.code = id;
this.description = description;
this.identification = identification;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIdentification() {
return identification;
}
public void setIdentification(String identification) {
this.identification = identification;
}
}
/**
* v3支撐
* @author chenzhen
* Created by chenzhen on 2018/11/19.
*/
public enum AssertEnum {
/**
* 資産類型枚舉
*/
Server(1,"伺服器", "Server");
int code;
String description;
String identification;
AssertEnum(int code, String description, String identification) {
this.code = code;
this.description = description;
this.identification = identification;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIdentification() {
return identification;
}
public void setIdentification(String identification) {
this.identification = identification;
}
}
/**
* @author chenzhen
* Created by chenzhen on 2018/11/21.
*/
public enum ClusterEnum {
/**
* 叢集枚舉
*/
TUXEDO_CLUSTER(1,"Tuxedo_app_group", "App_server_cluster_tuxedo"),
WeblogicCluster(2,"weblogic_app_group", "App_server_cluster_weblogic");
int code;
String description;
String identification;
ClusterEnum(int code, String description, String identification) {
this.code = code;
this.description = description;
this.identification = identification;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIdentification() {
return identification;
}
public void setIdentification(String identification) {
this.identification = identification;
}
}
/**
* @author chenzhen
* Created by chenzhen on 2018/11/20.
*/
public enum ConfigInfoEnum {
/**
* 配置類型枚舉
*/
JDBC(1,"JDBC連接配接配置", "JDBC"),
MARATHON_APP(2,"marathon_app節點定義", "manathon"),
//NetworkPoint("Network point", "Network point", 22),
//Office("Office", "Office", 23),
ORACLE_STORE(3,"Oracle存儲", "Oracle_store"),
ORACLE_RAC(4,"Oracle_rac", "Oracle_rac"),
//Room("Room", "Room", 26),
//ServiceArea("service_area", "service_area", 27),
//Supplier("Supplier", "Supplier", 28),
//SupplierContact("SupplierContact", "SupplierContact", 29),
//Users("Users", "Users", 30),
//Workplace("Workplace", "Workplace", 40),
WTC(5,"WTC", "WTC"),
WTC_EXPORTS(6,"wtc_exports", "wtc_exports"),
WTC_IMPORTS(7,"wtc_imports", "wtc_imports"),
WTC_LOCAL(8,"wtc_local", "wtc_local"),
WTC_REMOTE(9,"wtc_remote", "wtc_remote"),
//Employee("人力資源", "Employee", 46),
APPLICATION(10,"應用服務", "Application"),
APP_CLUSTER(11,"應用叢集", "app_cluster");
int id;
String description;
String identification;
ConfigInfoEnum(int id, String description, String identification) {
this.id = id;
this.description = description;
this.identification = identification;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIdentification() {
return identification;
}
public void setIdentification(String identification) {
this.identification = identification;
}
}
/**
* @author chenzhen
* Created by chenzhen on 2018/11/19.
*/
public enum InstanceNodeEnum {
/**
* 執行個體節點枚舉
*/
F5Info(1,"F5負載均衡節點", "F5Info"),
Hbase(2,"Hbase節點", "Hbaseserver"),
HDFS(3,"HDFS節點", "HDFS"),
Kafka(4,"Kafka節點", "Kafka"),
Marath(5,"marath_lb負載均衡", "marath"),
Mysql(6,"Mysql節點", "Mysqlserver"),
OracleDb(7,"Oracle資料庫節點", "Oracledb"),
Redis(8,"redis節點", "redis"),
Tomcat(9,"Tomcat節點", "Tomcatserver"),
Tuxedo(10,"Tuxedo服務節點", "Tuxedoserver"),
WeblogicConsle(11,"weblogic控制台節點", "weblogicconsle"),
Weblogic(12,"weblogic節點", "weblogic"),
Zookeeper(13,"Zookeeper節點", "ZooKeeper"),
OtherSoftware(14,"其他軟體節點", "Othersoftware");
int code;
String description;
String identification;
InstanceNodeEnum(int code,String description,String identification) {
this.code = code;
this.identification = identification;
this.description = description;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIdentification() {
return identification;
}
public void setIdentification(String identification) {
this.identification = identification;
}
}
4.從上面看這個代碼的異常的臃腫。
5.接下來我們來借助EnumMap來優化它。
-
精簡枚舉類
FirstEnum不變
/**
* v3支撐
* @author chenzhen
* Created by chenzhen on 2018/11/19.
*/
public enum FirstEnum {
/**
* 首層展示支援,V3版本
*/
APP_SERVICE(1,"服務","app_server"),
ASSERT(2,"資産","Asset"),
INSTANCE_NODE(3,"節點","Instancenode"),
//CLUSTER(4,"叢集","app_server_cluster"),
CONFIG_INFO(5,"配置","Configinfo");
int code;
String description;
String identification;
FirstEnum(int code, String description, String identification) {
this.code = code;
this.description = description;
this.identification = identification;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIdentification() {
return identification;
}
public void setIdentification(String identification) {
this.identification = identification;
}
}
- 添加一個EnumMap類
import com.google.common.collect.Maps;
import java.util.EnumMap;
import java.util.Map;
/**
* @author chenzhen
* Created by chenzhen on 2018/11/29.
*/
public class TypeEnumMap {
public static EnumMap<FirstEnum, Map<String, String>> typeEnumMap = new EnumMap<>(FirstEnum.class);
static {
Map<String, String> map1 = Maps.newHashMap();
map1.put("tuxedo_app_server", "Tuxedo服務");
map1.put("microServiceRestfull_app_server", "微服務接口服務");
map1.put("weblogic_app_server", "WebLogic服務");
typeEnumMap.put(FirstEnum.APP_SERVICE, map1);
Map<String, String> map2 = Maps.newHashMap();
map2.put("Server", "伺服器");
typeEnumMap.put(FirstEnum.ASSERT, map2);
Map<String, String> map3 = Maps.newHashMap();
map3.put("JDBC", "JDBC連接配接配置");
map3.put("manathon", "marathon_app節點定義");
map3.put("Oracle_store", "Oracle存儲");
map3.put("Oracle_rac", "Oracle_rac");
map3.put("WTC", "WTC");
map3.put("wtc_exports", "wtc_exports");
map3.put("wtc_imports", "wtc_imports");
map3.put("wtc_local", "wtc_local");
map3.put("wtc_remote", "wtc_remote");
map3.put("Application", "應用服務");
map3.put("app_cluster", "應用叢集");
map3.put("App_server_cluster_tuxedo","Tuxedo叢集");
map3.put("App_server_cluster_weblogic","WebLogic叢集");
typeEnumMap.put(FirstEnum.CONFIG_INFO,map3);
Map<String,String> map4 = Maps.newHashMap();
map4.put("HDFS","HDFS節點");
map4.put("Tuxedoserver","Tuxedo服務節點");
map4.put("weblogicconsle","weblogic控制台節點");
map4.put("weblogic","weblogic節點");
map4.put("ZooKeeper","Zookeeper節點");
map4.put("F5Info","F5負載均衡節點");
map4.put("Hbaseserver","HBase節點");
map4.put("Kafka","Kafka節點");
map4.put("marath","marathon_lb負載均衡節點");
map4.put("Oracledb","Oracle資料庫節點");
map4.put("Mysqlserver","Mysql節點");
map4.put("redis","Redis節點");
map4.put("Tomcatserver","Tomcat節點");
map4.put("Othersoftware","其他軟體節點");
typeEnumMap.put(FirstEnum.INSTANCE_NODE,map4);
}
}
- 修改原來備援醜陋可擴充行差的switchcase
/**
* 擷取son的list
*
* @param firstEnum 枚舉元素
* @return sonArrayList
*/
private ArrayList<SubAnalysisResult> getSons(FirstEnum firstEnum) {
ArrayList<SubAnalysisResult> sonArrayList = Lists.newArrayList();
Map<String, String> stringStringMap = TypeEnumMap.typeEnumMap.get(firstEnum);
for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
SubAnalysisResult subAnalysisResult = SubAnalysisResult.builder()
.id(entry.getValue())
.number(TypeNumberCache.getKey(entry.getKey()))
.healthy(TypeNumberCache.getKey(entry.getKey()))
.unHealthy(0)
.build();
sonArrayList.add(subAnalysisResult);
}
return sonArrayList;
}
至此優化結束。