简单树,生成工具(反射和范型的使用)

4 篇文章 0 订阅
2 篇文章 0 订阅

简单树,生成工具(反射和范型的使用)

1.新增annotation类

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeAnnotation {

    //该字段为主键id
    boolean id() default false;
    //该字段为父级主键id
    boolean pId() default false;
    //该字段为序号
    boolean order() default false;
    //该字段为子集
    boolean children() default false;

}

2.生成树工具

@AllArgsConstructor
@NoArgsConstructor
@Data
public class MyTreeUtil<T> {

    private List<T> vos;

    public void getTree(){
        try {
			
			// 新增自动排序
            sort();

            vos = assemblyTree(vos);

        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
    }

    public List<T> assemblyTree(List<T> vos) {
        List<T> dd = new ArrayList<>();
        List<T> zy = new ArrayList<>();
        try {
            for (T vo : vos) {
                Class<?> cl = vo.getClass();
                Field[] fields = cl.getDeclaredFields();
                Boolean isDD = false;
                for (Field field : fields) {
                    TreeAnnotation annotation = field.getAnnotation(TreeAnnotation.class);
                    if (null != annotation && annotation.pId()){
                        Object o = field.get(vo);
                        if (null != o){
                            String pid = (String) o;
                            if ("".equals(pid)){
                                isDD = true;
                                break;
                            }
                        }
                    }
                }
                if (isDD){
                    dd.add(vo);
                }else {
                    zy.add(vo);
                }
            }
            dd = assemblyTree(dd,zy);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return dd;
        }

    }

    public List<T> assemblyTree(List<T> dd,List<T> zy){

        try {

            Map<String, List<T>> zyMap = new LinkedHashMap<>();
            zyMap = getZyMap(zyMap,zy);
            for (T d : dd) {
                List<T> equipmentTemplateTreeVos = new ArrayList<>();
//                String id = "";
                Class<?> cl = d.getClass();
                Field[] fields = cl.getDeclaredFields();
                for (Field field : fields) {
                    field.setAccessible(true);
                    TreeAnnotation annotation = field.getAnnotation(TreeAnnotation.class);
                    if (null != annotation && annotation.pId()){
                        Object o = field.get(d);
                        if (null != o){
                            String id = (String) o;
                            equipmentTemplateTreeVos = zyMap.get(id);
                        }
                        break;
                    }
                }

                if (null != equipmentTemplateTreeVos && equipmentTemplateTreeVos.size() != 0){
                    //排序后面来整 先使用LinkedHashMap
//                    equipmentTemplateTreeVos = equipmentTemplateTreeVos.stream().sorted(Comparator.comparing(T::getFldOrder)).collect(Collectors.toList());
                    List<T> cj = zy;
                    cj.removeAll(equipmentTemplateTreeVos);
                    equipmentTemplateTreeVos = assemblyTree(equipmentTemplateTreeVos,cj);
                    for (Field field : fields) {
                        field.setAccessible(true);
                        TreeAnnotation annotation = field.getAnnotation(TreeAnnotation.class);
                        if (null != annotation && annotation.children()){
                            field.set(d,equipmentTemplateTreeVos);
                            break;
                        }
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return dd;
        }
    }

    public Map<String, List<T>> getZyMap(Map<String, List<T>> zyMap,List<T> zy){
        try {
            for (T t : zy) {
                Class<?> cl = t.getClass();
                Field[] fields = cl.getDeclaredFields();
                for (Field field : fields) {
                    field.setAccessible(true);
                    TreeAnnotation annotation = field.getAnnotation(TreeAnnotation.class);
                    if (null != annotation && annotation.pId()){
                        Object o = field.get(t);
                        if (null != o){
                            String pid = (String) o;
                            List<T> ts = zyMap.get(pid);
                            if (null == ts){
                                ts = new ArrayList<>();
                                ts.add(t);
                                zyMap.put(pid,ts);
                            }else {
                                ts.add(t);
                                zyMap.put(pid,ts);
                            }
                            break;
                        }
                    }
                }
            }
        }catch (Exception e){

        }finally {
            return zyMap;
        }
    }


}

3.树工具的使用

实体添加@TreeAnnotation注解,注意只能一个值为true

@Data
@ApiModel("零配件树返回vo")
public class SparePartsTreeVo {

    @TreeAnnotation(id = true)
    private String fldGuid;
    
    @TreeAnnotation(pId = true)
    private String fldPGuid;
    
    private String fldName;
    
    private String fldCode;
    
    @TreeAnnotation(order = true)
     private String fldOrder;
     
    private Integer fldType;
    
    private Integer fldDIsAbled;
    

    @TreeAnnotation(children = true)
    private List<SparePartsTreeVo> children;

}

调用方法

List<SparePartsTreeVo> vos = new ArrayList<>();
        MyTreeUtil<SparePartsTreeVo> sparePartsTreeVoMyTreeUtil = new MyTreeUtil<>();
        sparePartsTreeVoMyTreeUtil.setVos(vos);
        vos = sparePartsTreeVoMyTreeUtil.getTree();

-----------------------------------------新增内容---------------------------------------

4. 新增自动排序

/**
     * 排序
     */
    public void sort() throws IllegalAccessException {
        Map<String,List<T>> map = new HashMap<>();
        for (T vo : vos) {
            Class<?> cl = vo.getClass();
            Field[] fields = cl.getDeclaredFields();
            for (Field field : fields) {
                TreeAnnotation annotation = field.getAnnotation(TreeAnnotation.class);
                if (null != annotation && annotation.order()){
                    Object o = field.get(vo);
                    String order = "";
                    if (null != o){
                        order = (String) o;
                    }
                    List<T> ts = map.get(order);
                    if (null == ts){
                        ts = new ArrayList<>();
                    }
                    ts.add(vo);
                    map.put(order,ts);
                    break;
                }
            }
        }

        List<String> orders = new ArrayList<>(map.keySet());
        Collections.sort(orders);
        vos.clear();
        for (String order : orders) {
            vos.addAll(map.get(order));
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值