当前位置:网站首页>Enumeration general interface & enumeration usage specification

Enumeration general interface & enumeration usage specification

2022-07-07 03:25:00 InfoQ

  • The field value in the data table is the field of finite sequence , Corresponding to the specific enumeration in the program . Try to use varchar replace int( or tinyint). Beyond all doubt , Letter combinations are always better than 0、1、2、3 Such numbers are easy to recognize .
  • If the data table field has corresponding enumeration , be , The enumeration class name should be marked on the field annotation , Facilitate program traceability .
  • Enumeration generally has two parts , One is to enumerate item values , One is enumeration description . that , How are these two attributes named ? code and desc? still value and desc? still key and value? This article focuses on this problem .

Why use enumeration ?  Fields of finite sequences int or tinyint Isn't it good ?

The answer is simple :“ In enterprise application ,
Our code is first written for people to see , The second is to execute for the machine
”, Many coding specifications emphasize this .

Since it's written for people , that , Can be read 、 Easy to read 、 Understandable 、 Easy to understand is often quite important !

An architect I once knew emphasized : For enterprise application system , The current hardware and technical conditions are awesome. , Usually , We don't have to put types in pursuit of performance 、 Fields such as status are defined as numeric types ; And the writing is easy to understand 、 Maintainable system , It has become more and more important . Define these fields as string types , Use string values to describe fields , Is an important embodiment of easy to understand .

Know its reason well . Have a deep understanding of .

and , I've been practicing this .

About the benefits of enumeration , I have gone over and over again ->www.cnblogs.com/buguge/tag/ enumeration /. This article continues to .

With the continuous development and iteration of the project , More and more enumerations are defined in the project . These enumeration classes have the same structure , The only difference is the enumeration items . in addition , The use of enumerations is also varied , After all, everyone's understanding and cognition of technology and even work attitude are different , and , Occasionally, there will be NPE Come to the door .

@Getter
@AllArgsConstructor
public enum LevelEnum {
 FIRST("FIRST", " Class A "),
 SECOND("SECOND", " second level "),
 THIRD("THIRD", " Level three ");


 private String code;
 private String value;

 public static LevelEnum getBeanByCode(String code) {
 LevelEnum[] statusEnums = LevelEnum.values();
 for (LevelEnum v : statusEnums) {
 if (v.getCode().equals(code)) {
 return v;
 }
 }
 return null;
 }
}

@Getter
@AllArgsConstructor
public enum OrderStatusEnum {

 INIT("INIT", " initial "),
 ACCOUNTING("ACCOUNTING", " In bookkeeping "),
 SUCCESS("SUCCESS", " Payment succeeded "),
 FAILED("FAILED", " Payment failed ");


 private String key;
 private String description;

 public static OrderStatusEnum getBeanByCode(String code) {
 OrderStatusEnum[] values = OrderStatusEnum.values();
 for (OrderStatusEnum v : values) {
 if (v.getKey().equals(code)) {
 return v;
 }
 }
 return null;
 }
}


@AllArgsConstructor
public enum ProductEnum {
 BOSSKG("BOSS starts "),
 HUICHUXING(" Benefit travel "),
 SICHEBANGONG(" Private car office "),
 YOUFU(" Youfu "),
 UNKNOWN(" Unknown "),
 ;

 private String description;

 private String getCode(){
 return this.toString();
 }

 public String getDescription() {
 return description;
 }

 public static ProductEnum getBean(String value) {
 ProductEnum[] values = ProductEnum.values();
 for(ProductEnum temp : values){
 if(temp.getCode().equals(value)){
 return temp;
 }
 }
 return ProductEnum.UNKNOWN;
 }
}

@Getter
@AllArgsConstructor
public enum SeasonEnum {
 SPRING(1, " In the spring "),
 SUMMER(2, " In the summer "),
 AUTUMN(3, " autumn "),
 WINTER(4, " In the winter ");
 
 private int code;
 private String description;
 
public static SeasonEnum getBeanByCode(Integer code) {
 if (null == code) return null;
 SeasonEnum[] values = SeasonEnum.values();
 for (SeasonEnum temp : values) {
 if (temp.getCode() == code) {
 return temp;
 }
 }
 return null;
 }
}



Adhering to the professional quality of old drivers , I think from the perspective of team development norms , Can a unified interface be defined for these enumerations , such , Everyone is more unified when using it . below interface It's the result of me and an excellent partner in the Group , It defines the general operation of enumeration :
/**
 *  If the enumeration name is different from the actual value , Be sure to rewrite getKey Method
 *  Enumeration definition specification : Remember to capitalize enumeration names , The description should be as clear as possible , Don't misspell , Please check carefully
 *  for example :
 * MONDAY(" Monday "),
 * TUESDAY(" Tuesday ")
 *
 * @author shaozhengmao
 * @create 2021-06-21 10:18  In the morning
 */
public interface EnumAbility<T> {

 /**
 *  Return the actual value of enumeration
 * @return
 */
 T getCode();

 /**
 *  Return enumeration description
 *
 * @return  Enumeration description
 */
 String getDescription();

 /**
 *  Compare whether the current enumeration object is consistent with the passed in enumeration value (String Types ignore case )
 *  Whether the current enumeration item matches the value passed in from the far end ( such as : Field value of database 、rpc The parameter value passed in )
 *
 * @param enumCode  enumeration code
 * @return  match
 */
 default boolean codeEquals(T enumCode) {
 if (enumCode == null) return false;
 if (enumCode instanceof String) {
 return ((String) enumCode).equalsIgnoreCase((String) getCode());
 } else {
 return Objects.equals(this.getCode(), enumCode);
 }
 }

 /**
 *  Compare whether the two enumeration items are identical (==)
 *
 * @param anotherEnum  enumeration
 * @return  Are they the same?
 */
 default boolean equals(EnumAbility<T> anotherEnum) {
 return this == anotherEnum;
 }
}

meanwhile , A tool method is also defined , Used to base on code To get the corresponding enumeration item :

such , The enumeration in the above example will be simplified to :
@Getter
@AllArgsConstructor
public enum LevelEnum implements EnumAbility<String> {

 FIRST(&quot;FIRST&quot;, &quot; Class A &quot;),
 SECOND(&quot;SECOND&quot;, &quot; second level &quot;),
 THIRD(&quot;THIRD&quot;, &quot; Level three &quot;);


 private String code;
 private String value;

 @Override
 public String getDescription() {
 return value;
 }

 /**
 * 2021-12-18 23:00 zhanggz: There is ambiguity in this method , Please use {@link #getDescription()}
 * @return
 */
 @Deprecated
 public String getValue() {
 return value;
 }

 public static LevelEnum getBeanByCode(String code) {
 return (LevelEnum) EnumAbilityUtil.getEnumByCode(LevelEnum.class, code);
 }
}

@Getter
@AllArgsConstructor
public enum OrderStatusEnum implements EnumAbility<String> {

 INIT(&quot;INIT&quot;, &quot; initial &quot;),
 ACCOUNTING(&quot;ACCOUNTING&quot;, &quot; In bookkeeping &quot;),
 SUCCESS(&quot;SUCCESS&quot;, &quot; Payment succeeded &quot;),
 FAILED(&quot;FAILED&quot;, &quot; Payment failed &quot;);


 private String key;
 private String description;
 

 @Override
 public String getCode() {
 return key;
 }
 
 /**
 * 2021-12-18 23:00 zhanggz: There is ambiguity in this method , Please use {@link #getCode()}
 * @return
 */
 @Deprecated
 public String getKey() {
 return key;
 }

 public static OrderStatusEnum getBeanByCode(String code) {
 return (OrderStatusEnum) EnumAbilityUtil.getEnumByCode(OrderStatusEnum.class, code);
 }
}

@AllArgsConstructor
public enum ProductEnum implements EnumAbility<String> {
 BOSSKG(&quot;BOSS starts &quot;),
 HUICHUXING(&quot; Benefit travel &quot;),
 SICHEBANGONG(&quot; Private car office &quot;),
 YOUFU(&quot; Youfu &quot;),
 UNKNOWN(&quot; Unknown &quot;),
 ;
 
 private String description;

 @Override
 private String getCode(){
 return this.toString();
 }

 @Override
 public String getDescription() {
 return description;
 }
 
 public static ProductEnum getBean(String code) {
 return (ProductEnum) EnumAbilityUtil.getEnumByCode(ProductEnum.class, code);
 }
}

@Getter
@AllArgsConstructor
public enum SeasonEnum implements EnumAbility<Integer> {
 SPRING(1, &quot; In the spring &quot;),
 SUMMER(2, &quot; In the summer &quot;),
 AUTUMN(3, &quot; autumn &quot;),
 WINTER(4, &quot; In the winter &quot;);
 private Integer code;
 private String description;

 public static SeasonEnum getBeanByCode(Integer code) {
 if (null == code) return null;
 return (SeasonEnum) EnumAbilityUtil.getEnumByCode(SeasonEnum.class, code);
 }
}

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062018170449.html