【常用代码】-枚举、常量

常量

contants 采用接口(Interface)中变量默认为static final的特性

public interface Constants {
    String LOGIN_KEY = "OTA:VEHICLE:LOGIN";
}

public final static String OTA_TASK = "OTA_TASK";

枚举

package com.mycar.mycar.car.server.enums;

import lombok.Getter;
@Getter
public enum ViolationResultEnum {

    /** 查询成功 */
    SUCCESS(80000,"查询成功"),
    /** 没有传入城市 */
    NO_CITYNAME(70005,"没有传入城市"),
    ;

    private final Integer key;
    private final String message;
    ViolationResultEnum(Integer key,String message) {
        this.key = key;
        this.message = message;
    }
}
====== 数据同步成功失败 枚举
@Getter
public enum SyncStatusEnum {
    /**
     * 未推送
     */
    INIT(0),
    /**
     * 已推送
     */
    PUSHED(1),
    /**
     * 推送成功
     */
    SUCCESS(2),
    /**
     * 推送失败
     */
    FAIL(3),
    ;
    private final Integer status;

    SyncStatusEnum(Integer status) {
        this.status = status;
    }
}

单value 枚举

@Getter
public enum CarTypeEnum {
    /** 查询成功 */
    SMALL_CAR("02"),
    /** 查询成功 */
    BIG_NEWENERGY_CAR("51"),
    ;

    private final String key;

    CarTypeEnum(String key) {
        this.key = key;
    }
}

public enum EnumYesNo {
    YES(1, "是"),
    NO(0, "否");

    private final Integer key;
    private final String value;

    private EnumYesNo(Integer key, String value) {
        this.key = key;
        this.value = value;
    }

    public static EnumYesNo getEnumByKey(Integer key) {
        if (null == key) {
            return null;
        } else {
            EnumYesNo[] var1 = values();
            int var2 = var1.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                EnumYesNo temp = var1[var3];
                if (temp.getKey().equals(key)) {
                    return temp;
                }
            }

            return null;
        }
    }
}
posted @ 2023-04-12 14:14  爱生活(^_-)  阅读(20)  评论(0)    收藏  举报