用泛型重构代码

4 篇文章 0 订阅
1 篇文章 0 订阅

思路

用泛型将相同类型中的通用方法提取出来,如多个枚举类中的getEnumByKey、getValueByKey等方法,ResultUtil中的返回方法。

枚举案例

enumInterface

public interface EFEnum<K,V> {
    K getKey();
    V getValue();
}


enumEnum

@Getter
@AllArgsConstructor
public enum OrderEnum implements EFEnum<String,String>{

    DEALING("0", "处理中"),
    SUCCESS("1", "成功"),
    FAIL("2", "失败");

    private String code;
    private String message;

    @Override
    public String getKey() {
        return this.code;
    }

    @Override
    public String getValue() {
        return this.message;
    }
}

enumUtil

public class EnumUtils {

    public static <E extends EFEnum<K, V>, K, V> V getValueByKey(Class<E> clazz, K key) {
        EFEnum<K, V> e = getEnumByKey(clazz, key);
        return e == null ? null : e.getValue();
    }

    public static <E extends EFEnum<K, V>, K, V> K getKeyByValue(Class<E> clazz, V value) {
        EFEnum<K, V> e = getEnumByValue(clazz, value);
        return e == null ? null : e.getKey();
    }

    public static <E extends EFEnum<K, V>, K, V> EFEnum<K, V> getEnumByKey(Class<E> clazz, K key) {
        EFEnum<K, V>[] enums = clazz.getEnumConstants();
        return Arrays.stream(enums)
                .filter(e -> Objects.equals(key, e.getKey()))
                .findFirst().orElse(null);
    }

    public static <E extends EFEnum<K, V>, K, V> EFEnum<K, V> getEnumByValue(Class<E> clazz, V value) {
        EFEnum<K, V>[] enums = clazz.getEnumConstants();
        return Arrays.stream(enums)
                .filter(e -> Objects.equals(value, e.getValue()))
                .findFirst().orElse(null);
    }
}

ResultUtil案例

ResultUtil

@NoArgsConstructor
public class ResultUtil {

    public static <E extends CommonResp> E result(Class<E> clazz, E e, boolean b) {
        E result = null;
        try {
            result = clazz.newInstance();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
        if (b) {
            BeanUtils.copyProperties(e, result);
            result.setRspCd(CommonRespCMCCEnum.SUCCESS.getCode());
            result.setRspInf(CommonRespCMCCEnum.SUCCESS.getMessage());
        } else {
            result.setRspCd(CommonRespCMCCEnum.SYSTEM_ERROR.getCode());
            result.setRspInf(CommonRespCMCCEnum.SYSTEM_ERROR.getMessage());
        }
        System.out.println(result.getClass().toString());
        return result;
    }

    public static <E extends CommonResp> E success(Class<E> clazz, E e) {
        E result = null;
        try {
            result = clazz.newInstance();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }

        result.setRspCd(CommonRespCMCCEnum.SUCCESS.getCode());
        result.setRspInf(CommonRespCMCCEnum.SUCCESS.getMessage());
        BeanUtils.copyProperties(e, result);

        return result;
    }

    public static <E extends CommonResp> E fail(Class<E> clazz, E e) {
        E result = null;
        try {
            result = clazz.newInstance();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }

        result.setRspCd(CommonRespCMCCEnum.SYSTEM_ERROR.getCode());
        result.setRspInf(CommonRespCMCCEnum.SYSTEM_ERROR.getMessage());

        return result;
    }

    public static void main(String[] args) {

//        RepayPlanResp resp = fun1(RepayPlanResp.class, true);
//        RepayPlanReq a = fun1(RepayPlanReq.class, true);
//        RepayApplyResp b = fun1(RepayApplyResp.class, true);

        RepayPlanDetail detail = RepayPlanDetail.builder().orgOrdNo("123").orgPlanNo("abc").lateFee(BigDecimal.valueOf(141)).build();
        List<RepayPlanDetail> list = new ArrayList<>();
        list.add(detail);
        RepayApplyResp re1 = RepayApplyResp.builder().planList(list).build();
        re1 = result(RepayApplyResp.class, re1, false);
        System.out.println(re1.toString());

    }
}

调用

return ResultUtil.fail(RepayPlanResp.class, null);
return ResultUtil.success(RepayPlanResp.class, result);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值