枚举类简单示例
示例1:
package com.hqw.cmtcouponmsgsend.enums;
import lombok.Getter;
/**
* @author hqw OK
* @date 2021-06-15 12:08
* @project cmtcouponmsgsend
**/
@Getter
public enum MsgTypeTitleMessageEnum {
rightsGivenMsg("权益发放通知", "您本月的等级会员权益已经发放,请注意查看使用"),//当月权益发放通知
birthdayGiftMsg("生日礼发放通知", "有一份生日小礼已悄悄放到你卡包,请注意查看");//生日礼发放通知
private String title;
private String message;
MsgTypeTitleMessageEnum(String title, String message) {
this.title = title;
this.message = message;
}
MsgTypeTitleMessageEnum(MsgTypeTitleMessageEnum msgTypeTitleMessageEnum) {
// this(msgTypeTitleMessageEnum.title, msgTypeTitleMessageEnum.message);
// 或者
this(msgTypeTitleMessageEnum.getTitle(), msgTypeTitleMessageEnum.getMessage());
}
}
不能加@Setter注解,注意实例化的各枚举以逗号隔开而非分号。
JSONObject request = new JSONObject(true);//true属性排序 this(16, ordered);
request.put("title", MsgTypeTitleMessageEnum.valueOf(message.getMessageType()).getTitle());
request.put("message",MsgTypeTitleMessageEnum.valueOf(message.getMessageType()).getMessage());
MsgTypeTitleMessageEnum.valueOf(message.getMessageType()).getTitle();//message.getMessageType() 等于 rightsGivenMsg或者birthdayGiftMsg
示例2:
package com.qwh.util.enums;
import lombok.Getter;
@Getter
public enum StatusEnum {
/**
* 状态
*/
ONE("待解决", "1"),
TWO("已解决", "2");
private String key;
private String value;
StatusEnum(String key, String value) {
this.key = key;
this.value = value;
}
public static String getKey(String valueNew) {
StatusEnum[] statusEnums = values();//public static StatusEnum[] values()
for (StatusEnum statusEnum : statusEnums) {
if (statusEnum.getValue().equals(valueNew)) {
return statusEnum.getKey();
}
}
return null;
}
}
StatusEnum.getKey( item.getAlarmState() );
StatusEnum[] statusEnums = values();//public static StatusEnum[] values()
枚举遍历+SystemMessage工具类
package com.genertech.plm.aia.login.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
@Slf4j
@Component
public class SystemMessage {
private static Properties prop;
public static Map<String, String> map = new HashMap();
private static SystemMessage systemMessage = new SystemMessage();
static {
prop = new Properties();
InputStream inputStream = null;
try {
// 【读取资源文件
inputStream = systemMessage.getClass().getResourceAsStream("/user-details.properties");
prop.load(inputStream);
// 【遍历资源文件enumeration数据
Enumeration enumeration = prop.propertyNames();
while (enumeration != null && enumeration.hasMoreElements()) {
String username = (String) enumeration.nextElement();
String monitorRole = SystemMessage.getString(username);
log.info("user-details.properties User [{}] is in role [{}]", username, monitorRole);
map.put(username, monitorRole);
}
} catch (Exception e) {
log.warn("file not found!\n", e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private SystemMessage() {
}
/**
* get the value from the properties file.
*
* @param key the key in the properties file
*/
public static String getString(String key) {
try {
return prop.getProperty(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
/**
* 获取int类型.
*
* @param key NOTNULL
* @return
*/
public static Integer getInteger(String key) {
try {
String value = prop.getProperty(key);
return Integer.valueOf(value);
} catch (MissingResourceException e) {
return 0;
}
}
/**
* 获取int类型配置,如没有返回defaultValue.
*
* @param key NOTNULL
* @param defaultValue NULL
* @return
*/
public static Integer getInteger(String key, int defaultValue) {
try {
String value = prop.getProperty(key);
return Integer.valueOf(value);
} catch (MissingResourceException e) {
return defaultValue;
}
}
/**
* 获取boolean类型属性.
*
* @param key NOTNULL
* @return
*/
public static Boolean getBoolean(String key) {
try {
String value = prop.getProperty(key);
return Boolean.valueOf(value);
} catch (MissingResourceException e) {
return false;
}
}
}