VehicleEnum.java 1.44 KB
package com.dituhui.pea.enums;

import cn.hutool.core.util.ObjectUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


/**
 * 交通工具:1汽车;2电动车;3自行车;4步行 默认是汽车
 *
 * @author RenPing
 * @date 2023/10/24
 */
public enum VehicleEnum {
    CAR(1, "驾车"),
    ELECTRIC_CAR(2, "电动车"),
    BICYCLE(3, "骑行"),
    WALK(4, "步行");

    private Integer code;
    private String name;

    private VehicleEnum(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public static String getNameByValue(Integer value) {
        if (Objects.isNull(value)) {
            return null;
        }

        for (VehicleEnum enums : VehicleEnum.values()) {
            if (ObjectUtil.equal(enums.getCode(), value)) {
                return enums.getName();
            }
        }

        return null;
    }

    /**
     * 根据枚举索引获取对应的枚举值
     *
     * @param code 索引值
     * @return 索引对应的枚举值
     */
    public static VehicleEnum getVehicle(Integer code) {
        return INDEX_MAP.get(code);
    }

    private static final Map<Integer, VehicleEnum> INDEX_MAP = new HashMap<>();

    static {
        for (VehicleEnum au : VehicleEnum.values()) {
            INDEX_MAP.put(au.getCode(), au);
        }
    }
}