ali 规范
【强制】二方库里可以定义枚举类型,参数可以使用枚举类型,但是接口返回值不允许使用枚
举类型或者包含枚举类型的 POJO 对象。
【参考】枚举类名建议带上 Enum 后缀,枚举成员名称需要全大写,单词间用下划线隔开。
说明:枚举其实就是特殊的常量类,且构造方法被默认强制是私有。
正例:枚举名字为 ProcessStatusEnum 的成员名称:SUCCESS / UNKOWN_REASON。
如果变量值仅在一个范围内变化,且带有名称之外的延伸属性,定义为枚举类。下面
正例中的数字就是延伸信息,表示星期几。
正例:public Enum { MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6),
SUNDAY(7);}
code 获取枚举
public static PrizeCycleTypeEnum peekByCode(String code) {
return Arrays.stream(PrizeCycleTypeEnum.values())
.filter(e -> e.getCode().equals(code))
.findAny()
.orElse(NULL);
}
must有code
import java.lang.reflect.Field;
import java.security.InvalidParameterException;
import java.util.Objects;
//使用反射进行枚举的解析
public abstract class EnumUtils {
private EnumUtils() {
}
private static final String CODE_FIELD_NAME = "code";
private static final String LACK_MUST_FIELDS = "EnumUtils' enumClass must has `code` Integer field!";
/**
* 根据枚举类实例返回对应的 code 值。枚举的值必须为 code
*
* @param e 枚举实例
* @param <E> 枚举实例类
* @return 枚举实例的 code
*/
public static <E extends Enum> Integer getCode(E e) {
try {
return EnumUtils.getCodeThrows(e);
} catch (InvalidParameterException ignore) {
return null;
}
}
/**
* 根据枚举类实例返回对应的 code 值。枚举的值必须为 code
*
* @param e 枚举实例
* @param <E> 枚举实例类
* @return 枚举实例的 code
*/
public static <E extends Enum> Integer getCodeThrows(E e) {
Objects.requireNonNull(e, "EnumUtils' method getCode parameter `e` can't be null!");
try {
Field codeField = e.getClass().getDeclaredField(CODE_FIELD_NAME);
codeField.setAccessible(true);
return (Integer) codeField.get(e);
} catch (NoSuchFieldException | IllegalAccessException e1) {
throw new InvalidParameterException(LACK_MUST_FIELDS);
}
}
/**
* 通过枚举类型和code值转换特定的枚举实例。如果没有找到返回null
*
* @param enumClass 枚举类
* @param code 枚举实例值
* @param <E> 枚举类型
* @return 枚举实例
*/
public static <E extends Enum> E parse(Class<E> enumClass, Integer code) {
if (null == code) {
return null;
}
Field codeField;
try {
codeField = enumClass.getDeclaredField(CODE_FIELD_NAME);
codeField.setAccessible(true);
} catch (NoSuchFieldException e) {
throw new InvalidParameterException(LACK_MUST_FIELDS);
}
for (E enumConstant : enumClass.getEnumConstants()) {
try {
// get EnumField value
if (code.equals(codeField.get(enumConstant))) {
// return enum instance
return enumConstant;
}
} catch (IllegalAccessException e) {
throw new InvalidParameterException(LACK_MUST_FIELDS);
}
}
return null;
}
/**
* 根据code和期望的枚举实例是否匹配
*
* @param expect 期望的枚举实例
* @param actualCode 实际的code值
* @param <E> 枚举类型
* @return code值和枚举实例的匹配结果
*/
public static <E extends Enum> boolean isEquals(E expect, Integer actualCode) {
if (!expect.getClass().isEnum()) {
return false;
}
try {
Field codeField = expect.getClass().getDeclaredField(CODE_FIELD_NAME);
codeField.setAccessible(true);
return codeField.get(expect).equals(actualCode);
} catch (IllegalAccessException | NoSuchFieldException e) {
return false;
}
}
}
制造枚举的简易方法
@AllArgsConstructor
@Getter
// ------ 运维平台下发可能存在的任务类型 ------ //
public enum Old808OperationEnum {
COMMON_ANSWER(0x01,"平台通用应答"),
COMMON_ANSWER1(1,"平台通用应答"),
DEVICE_UPDATE(0x02,"升级消息"),
TIME_INTERVAL(0x03,"定时参数的时间间隔"),
REMOTE_PARAM_CONFIG(0x04,"远程参数配置文件消息"),
UPDATE_APPOINT_MSG(0x05,"上传指定内容消息 "),
FORMAT_DEVICE(0x06,"远程格式化设备指令"),
CONTROL_PRINT(0x07,"控制设备打印信息"),
HARD_WARE_CONFIG(0x08,"硬件配置表消息"),
ONLINE_TASK(0x09,"实时任务消息"),
WRITE_BLACK_PROTOCOL(0x0A,"写黑匣子数据协议"),
STORE_PRINT_PROTOCOL(0x0B,"存储设备打印协议"),
ORDER_TASK(0x0C,"预约任务条件协议"),
AUTHORIZE_CONTROL(0x0D,"授权控制的协议"),
EVIDENCE_CLASSIFY_MSG(0x0E,"证据分类信息的协议"),
P2_CONFIG_FILE(0x0F,"P2配置文件的协议"),
FILE_SERVER_MSG(0x10,"用于文件服务器信息"),
;
int code;
String describe;
}
使用
public enum EventType {
// invitation bonus
PERSONAL_ADD,
// register bonus
PERSONAL_UPDATE,
// promotion status changed
STAFF_ADD,
// investment gift
STAFF_UPDATE,
}
----------------------
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Event {
ParameterMappings parameterMappings() default @ParameterMappings;
ReturnMappings returnMappings() default @ReturnMappings;
EventType type();
}
----------------------
@Event(parameterMappings = @ParameterMappings(mappings = {@FieldMapping(key = "id", pathOfValue = "id", paramIndex = 0),@FieldMapping(key = "username", pathOfValue = "username", paramIndex = 1),@FieldMapping(key = "password", pathOfValue = "password", paramIndex = 2),@FieldMapping(key = "roles", pathOfValue = "roles", paramIndex = 3)}),
type = EventType.STAFF_ADD)
@RequestMapping(method = RequestMethod.POST, value = "/save")
public @ResponseBody
StatusResponse saveStaff(@RequestParam(required = false) String id,
@RequestParam(required = false) String username,
@RequestParam(required = false) String password,
@RequestParam(required = false) String roles
) throws Exception {
int code = 1;
Object data;
Long l = Long.valueOf(id);
Staff staff = new Staff();