是否属于
public class EnumUtil {
/**
* 判断数值是否属于枚举类的值
* @param key
* @return
*/
public static boolean isInclude(int key){
boolean include = false;
for (SwitchStatus e: SwitchStatus.values()){
if(e.getKey()==key){
include = true;
break;
}
}
return include;
}
}
代码测试
class Test{
public static void main(String[] args) {
CustType mtype = CustType.getMemberType(1);
System.out.println(mtype);
// 输出结果: STUDENT
System.out.println(mtype.getDesc());
// 输出结果: 学生
String sncode = mtype.getPrefix();
System.out.println(sncode);
// 输出结果: S
}
package com.toltech.appstore.persistence.entity.Test;
import java.util.Arrays;
import java.util.List;
/**
* Created by wanggs on 2017/8/22.
*/
public class EnumTest {
public static void main(String[] args) {
List<Integer> USER_TYPES = Arrays.asList(CustType.STUDENT.getValue(), CustType.TEACHER.getValue(), CustType.PARENTS.getValue());
System.out.println(USER_TYPES.contains(6));
// false
}
}
enum CustType {
/**
* 学生
*/
STUDENT(1, "S", "学生"),
/**
* 教师
*/
TEACHER(2, "T", "教师"),
/**
* 家长
*/
PARENTS(3, "P", "家长");
private int value;
private String prefix;
private String desc;
private CustType(int value, String prefix, String desc){
this.value = value;
this.prefix = prefix;
this.desc = desc;
}
public int getValue(){
return value;
}
public String getPrefix() {
return prefix;
}
public String getDesc(){
return desc;
}
public static CustType getMemberType(int value){
for(CustType mtype : CustType.values()){
if(value == mtype.getValue()){
return mtype;
}
}
return null;
}
}
Demo
public class Test {
public static void main(String[] args) {
ResultBean<AuthzBean> result = null;
AuthzBean authz = null;
result = new ResultBean<AuthzBean>(ResultStatu.X1000, authz, "用户名为空");
System.out.println(result.isSuccess());
// false
System.out.println(ResultStatu.X1000.isSuccess());
// false
JSONObject object = (JSONObject) JSONObject.toJSON(result);
System.out.println(object);
// {"message":"用户名为空","status":1000,"success":false}
}
}
pojo
public class ResultBean<T> implements Serializable {
private static final long serialVersionUID = 1L;
private boolean success;
private Integer status;
private String message;
private T data;
public ResultBean(ResultStatu statu, T data){
this.success = statu.isSuccess();
this.status = statu.getStatus();
this.message = statu.getMessage();
this.data = data;
}
public ResultBean(ResultStatu statu, T data, String message){
this.success = statu.isSuccess();
this.status = statu.getStatus();
if(StringUtils.isNotBlank(message)){
this.message = message;
} else {
this.message = statu.getMessage();
}
this.data = data;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
public class AuthzBean implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String account;
private CustType mtype;
private String token;
private List<Integer> parentIds;
private Integer childId;
private Integer areaId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public CustType getMtype() {
return mtype;
}
public void setMtype(CustType mtype) {
this.mtype = mtype;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public List<Integer> getParentIds() {
return parentIds;
}
public void setParentIds(List<Integer> parentIds) {
this.parentIds = parentIds;
}
public Integer getChildId() {
return childId;
}
public void setChildId(Integer childId) {
this.childId = childId;
}
public Integer getAreaId() {
return areaId;
}
public void setAreaId(Integer areaId) {
this.areaId = areaId;
}
}
枚举
public enum ResultStatu {
/**
* 业务处理成功
*/
X0000(true, 0, "业务处理成功"),
/**
* 参数不能为空
*/
X1000(false, 1000, "参数不能为空"),
/**
* 参数不正确
*/
X1001(false, 1001, "参数不正确"),
/**
* 依赖系统接口出错
*/
X1002(false, 1002, "依赖系统接口出错"),
/**
* 数据存在重复
*/
X1003(false, 1003, "数据存在重复");
private boolean success;
private int status;
private String message;
private ResultStatu(boolean success, int status, String message){
this.success = success;
this.status = status;
this.message = message;
}
public boolean isSuccess() {
return success;
}
public int getStatus() {
return status;
}
public String getMessage() {
return message;
}
}
enums 工具类
public class EnumUtil {
public static <T extends CodeEnum> T getByCode(Integer code, Class<T> enumClass) {
for (T each: enumClass.getEnumConstants()) {
if (code.equals(each.getCode())) {
return each;
}
}
return null;
}
}
public interface CodeEnum {
Integer getCode();
}