EnvEnum.java
2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.essa.framework;
/**
* Created by weicheng on 2018/10/25.
*/
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
public enum EnvEnum {
SIT("sit","SIT"),
DIT("dit","DIT"),
HOTFIX("hotfix","HOTFIX"),
UAT("uat","UAT"),
EPD("epd","EPD");
/**
* 原子引用(全部)
*/
private static AtomicReference<ConcurrentHashMap<String, EnvEnum>> mapAllRef = new AtomicReference<>();
private static AtomicReference<ConcurrentHashMap<String, EnvEnum>> mapPartRef = new AtomicReference<>();
/**
* 根据code获取 EnvEnum
*
* @param code
* @return
*/
public static EnvEnum fromCode(String code) {
buildConcurrentHashMap();
return mapAllRef.get().get(code);
}
/**
* 如果map是空,就会构建Map
*/
private static void buildConcurrentHashMap() {
//如果所有枚举的原子引用是空对象,就创建一个ConcurrentHashMap给原子引用
mapAllRef.compareAndSet(null, new ConcurrentHashMap<>());
//获取原子引用的currentHashMap对象
ConcurrentHashMap<String, EnvEnum> concurrentHashAllMap = mapAllRef.get();
//如果ConcurrentHashMap里面没有一个枚举对象,需要初次化
if (concurrentHashAllMap.isEmpty()) {
for (EnvEnum e : EnvEnum.values()) {
concurrentHashAllMap.put(e.getCode(), e);
}
mapAllRef.set(concurrentHashAllMap);
}
}
/**
* 枚举代码
*/
private String code;
/**
* 枚举描述
*/
private String desc;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
EnvEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}
public static List<EnvEnum> getAllEnum() {
buildConcurrentHashMap();
return (List<EnvEnum>) mapAllRef.get().values();
}
/**
* 只返回常用枚举
*
* @return
*/
public static List<EnvEnum> getEnumByDefault() {
buildConcurrentHashMap();
return (List<EnvEnum>) mapPartRef.get().values();
}
}