今天聊聊枚举咋用
先定义一个枚举
代码语言:javascript复制package com.ruben.enumration;
import com.alibaba.fastjson.annotation.JSONType;
import com.fasterxml.jackson.annotation.JsonFormat;
/**
* @ClassName: GenderEnum
* @Description:
* @Date: 2020/8/18 19:03
* *
* @author: achao<achao1441470436 @ gmail.com>
* @version: 1.0
* @since: JDK 1.8
*/
@JSONType(serializeEnumAsJavaBean = true)
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum GenderEnum {
FEMALE("女", 0), MALE("男", 1);
private String name;
private Integer code;
GenderEnum(String name, Integer code) {
this.name = name;
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
然后就可以使用了
代码语言:javascript复制public static void main(String[] args) {
System.out.println(GenderEnum.MALE.getName() " " GenderEnum.MALE.getCode());
System.out.println(GenderEnum.FEMALE.getName() " " GenderEnum.FEMALE.getCode());
}
输出结果
男 1 女 0
有人可能会问,枚举用Validator
怎么校验?
其实也很简单
比如,我们接收的参数只能限制为枚举内有的0
和1
这时就自定义一个校验
代码语言:javascript复制package com.ruben.validator;
import com.ruben.enumration.GenderEnum;
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;
import javax.validation.constraints.NotNull;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* @ClassName: Gender
* @Description:
* @Date: 2020/8/18 19:25
* *
* @author: achao<achao1441470436 @ gmail.com>
* @version: 1.0
* @since: JDK 1.8
*/
@NotNull
@Documented
@Retention(RUNTIME)
@Constraint(validatedBy = {GenderValidator.class})
@ReportAsSingleViolation
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
public @interface Gender {
String message() default "性别应该为:男1 女0";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
GenderEnum[] value();
}
以及处理类
代码语言:javascript复制package com.ruben.validator;
import com.ruben.enumration.GenderEnum;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @ClassName: GenderValidator
* @Description:
* @Date: 2020/8/18 19:25
* *
* @author: achao<achao1441470436 @ gmail.com>
* @version: 1.0
* @since: JDK 1.8
*/
public class GenderValidator implements ConstraintValidator<Gender, Integer> {
private static final Set<Integer> GENDER_CONSTRAINTS = new HashSet<>();
/**
* Initializes the validator in preparation for
* calls.
* The constraint annotation for a given constraint declaration
* is passed.
* <p>
* This method is guaranteed to be called before any use of this instance for
* validation.
* <p>
* The default implementation is a no-op.
*
* @param constraintAnnotation annotation instance for a given constraint declaration
*/
@Override
public void initialize(Gender constraintAnnotation) {
GENDER_CONSTRAINTS.addAll(Arrays
.stream(constraintAnnotation.value())
.map(GenderEnum::getCode)
.collect(Collectors.toSet()));
}
/**
* Implements the validation logic.
* The state of {@code value} must not be altered.
* <p>
* This method can be accessed concurrently, thread-safety must be ensured
* by the implementation.
*
* @param value object to validate
* @param context context in which the constraint is evaluated
* @return {@code false} if {@code value} does not pass the constraint
*/
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
return GENDER_CONSTRAINTS.contains(value);
}
}
然后加上我们的注解
最后测试~