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;
}
至此优化结束。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiZpdmLxITMzEzN0cTMwMTMxgTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.gif)