java枚举搭建树型结构

java枚举搭建树型结构

公共接口:

public interface BaseEnum<T> {
    T getValue();

    default String getEnumName() {
        return null;
    }
}

车辆种类 一级枚举:

package cn.enums.vehicle;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.enums.BaseEnum;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 车辆种类 一级枚举
 */
@Getter
public enum VehicleType1Enum implements BaseEnum<Integer> {
    _1(1, "危险品运输车辆", -1),
    _2(2, "应急车辆类型", 20),
    ;

    private final Integer type;
    private final String name;

    //省平台编码
    private final Integer provinceCode;

    VehicleType1Enum(Integer type, String name, Integer provinceCode) {
        this.type = type;
        this.name = name;
        this.provinceCode = provinceCode;
    }

    public static final Map<String, Integer> CAR_TYPE_SORT = Arrays.stream(VehicleType1Enum.values())
            .collect(Collectors.toMap(VehicleType1Enum::getName, VehicleType1Enum::getType));

    public static String name(Integer type){
        return Arrays.stream(VehicleType1Enum.values())
                .filter(e -> ObjectUtil.equals(e.getType(), type))
                .findFirst()
                .map(VehicleType1Enum::getName)
                .orElse(StrUtil.EMPTY);
    }
    public static Integer type(String name){
        return Arrays.stream(VehicleType1Enum.values())
                .filter(e -> ObjectUtil.equals(e.getName(), name))
                .findFirst()
                .map(VehicleType1Enum::getType)
                .orElse(null);
    }

    @Override
    public Integer getValue() {
        return this.type;
    }

    @Override
    public String getEnumName() {
        return this.name;
    }
}

车辆种类 二级枚举:

package cn.enums.vehicle;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.enums.BaseEnum;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 车辆种类 二级枚举
 */
@Getter
public enum VehicleType2Enum implements BaseEnum<Integer> {
    _11(11, "LNG", VehicleType1Enum._1.getType(), -1),
    _12(12, "CNG", VehicleType1Enum._1.getType(), -1),
    _13(13, "LPG", VehicleType1Enum._1.getType(), -1),

    _21(21, "指挥车", VehicleType1Enum._2.getType(), 200100),
    _22(22, "抢险救援车", VehicleType1Enum._2.getType(), 200200),
    _23(23, "后勤保障车", VehicleType1Enum._2.getType(), 200300),
    _24(24, "应急发电车", VehicleType1Enum._2.getType(), 200400),
    _25(25, "其他", VehicleType1Enum._2.getType(), 200500),
    ;

    private final Integer type;
    private final String name;
    private final Integer vehicleType1;

    //省平台编码
    private final Integer provinceCode;

    public static final Map<String, Integer> LEGEND_SORT = Arrays.stream(VehicleType2Enum.values())
            .collect(Collectors.toMap(VehicleType2Enum::getName, VehicleType2Enum::getType));

    VehicleType2Enum(Integer type, String name, Integer vehicleType1, Integer provinceCode) {
        this.type = type;
        this.name = name;
        this.vehicleType1 = vehicleType1;
        this.provinceCode = provinceCode;
    }

    public static String name(Integer type){
        return Arrays.stream(VehicleType2Enum.values())
                .filter(e -> ObjectUtil.equals(e.getType(), type))
                .findFirst()
                .map(VehicleType2Enum::getName)
                .orElse(StrUtil.EMPTY);
    }

    //兼容字符串
    public static String name(String type){
        return name(Convert.toInt(type));
    }

    public static Integer type(String name){
        return Arrays.stream(VehicleType2Enum.values())
                .filter(e -> ObjectUtil.equals(e.getName(), name))
                .findFirst()
                .map(VehicleType2Enum::getType)
                .orElse(null);
    }
    
    @Override
    public Integer getValue() {
        return this.type;
    }

    @Override
    public String getEnumName() {
        return this.name;
    }
}

//写个main方法试试看

package 路径.路径.demo;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import cn.enums.vehicle.VehicleType1Enum;
import cn.enums.vehicle.VehicleType2Enum;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class TestDemo {
    public static void main(String[] args) {

        List<TreeNode<Long>> voList = Arrays.stream(VehicleType1Enum.values())
                .map(e -> new TreeNode<>((long)e.getType(), 0L, e.getName(), e.getType())
                        .setExtra(MapUtil.<String, Object>builder()
                                .put("dictCode", e.getType())
                                .build()))
                .collect(Collectors.toList());

        List<TreeNode<Long>> voList2 = Arrays.stream(VehicleType2Enum.values())
                .map(e -> new TreeNode<>((long)e.getType(), (long)e.getVehicleType1(), e.getName(), e.getType())
                        .setExtra(MapUtil.<String, Object>builder()
                                .put("dictCode", e.getType())
                                .build()))
                .collect(Collectors.toList());

        voList.addAll(voList2);
        
        //车辆种类 树形结构
        List<Tree<Long>> build = TreeUtil.build(voList, 0L);

        //传给前端的 json格式 打印出来
        System.out.println(JSONUtil.toJsonPrettyStr(build));
    }
}

你可能感兴趣的:(Tree,java,开发语言)