Assert和CommUtils抛出异常(自用记录)

Assert

Assert.notNull(vo, "贷款信息为空");
Assert.notBlank(vo.getLoanId(), "贷款编号为空");

Assert.notEmpty(busiLoanApplyDto.getCustId(), "客户编号为空!");
Assert.notNull(busiLoanApplyDto.getApplyTerm(), "贷款期限为空!");
Assert.isTrue(0 != busiLoanApplyDto.getApplyTerm(), "贷款期限为空!");
Assert.notNull(busiLoanApplyDto.getLoanAmount(), "贷款金额为空!");
Assert.notEmpty(busiLoanApplyDto.getLoanCode(), "贷款申请编号为空");

CommUtils(自创类)

String validate = ValidationUtils.validate(signMemberInitDto);
   if (CommUtils.isNotEmpty(validate)){
   log.error("初始化签约人信息失败,原因为{}", validate);
   throw new ClmpException("初始化签约人信息失败");
}

IInitSignMember iInitSignMember = iInitSignMemberMap.get(signMemberInitDto.getCustType());
  if (CommUtils.isEmpty(iInitSignMember)) {
   log.error("客户类型为{}时,对应的业务处理类不存在!", signMemberInitDto.getCustType());
   throw new ClmpException("没有对应业务处理类");
}
public class CommUtils {

    public static String getCorrectAddress(String address) {
        if (StringUtils.isEmpty(address) || !(address.contains("-") && address.contains("&"))) {
            return address;
        }
        String correctAddress = "";
        StringBuilder sb = new StringBuilder();
        //String address   = "湖北省&42-武汉市&4201-洪山区&420111-张家湾街道&420111004-9号&4201110041000";
        try {
            String[] split = address.split("-");
            for (String s : split) {
                sb.append(s.substring(0, s.indexOf("&"))).append("/");
            }
            correctAddress = sb.substring(0, sb.lastIndexOf("/"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return correctAddress;
    }

    /**
     * 判断对象内容是否为空
     * 对象等于null 为空
     * 对象类型支持 数组、字符串、
     *
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        } else if (obj instanceof String) {
            return ((String) obj).trim().length() == 0;
        } else if (obj instanceof CharSequence) {
            return ((CharSequence) obj).length() == 0;
        } else if (obj instanceof Collection) {
            return ((Collection) obj).isEmpty();
        } else if (obj instanceof Map) {
            return ((Map) obj).isEmpty();
        } else if (obj instanceof Set) {
            return ((Set) obj).isEmpty();
        } else {
            return false;
        }
    }

    /**
     * 判断是否不为空
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 首字母大写。
     *
     * @param str
     * @return
     */
    public static String capitalizeTheFirstLetter(String str) {
        char[] chs = str.toCharArray();
        for (int i = 0; i < chs.length; ) {
            chs[i] = Character.toUpperCase(chs[i]);
            break;
        }
        return String.valueOf(chs);
    }

    /**
     * List<Map<String, String>>根据value排序
     *
     * @param list
     * @return
     */
    public static List<Map<String, String>> sortList(List<Map<String, String>> list) {
        Collections.sort(list, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                return o1.get("countScore").compareTo(o2.get("countScore"));
            }
        });
        return list;
    }

    public static Map<String, String> getData(int num) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("countScore", String.valueOf(num));
        return map;
    }

    public static Map<String, String> getDataMap(String s) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("countScore", s);
        return map;
    }

    public static Map<String, Integer> listDataNum(List<String> list) {
        Map<String, Integer> map = new HashMap<>();
        for (String str : list) {
            //计数器,用来记录数据的个数
            Integer i = 1;
            if (map.get(str) != null) {
                i = map.get(str) + 1;
            }
            map.put(str, i);
        }
        return map;
    }

    /**
     * 去掉指定字符串的开头的指定字符
     *
     * @param str    原始字符串
     * @param delStr 要删除的字符串
     * @return
     */
    public static String trimStringStart(String str, String delStr) {
        int delStrLength = delStr.length();
        if (str.startsWith(delStr)) {
            str = str.substring(delStrLength);
        }
        if (str.endsWith(delStr)) {
            str = str.substring(0, str.length() - delStrLength);
        }
        return str;
    }

    /**
     * 去掉指定字符串的结尾的指定字符
     *
     * @param str    原始字符串
     * @param delStr 要删除的字符串
     * @return
     */
    private static String trimStringEnd(String str, String delStr) {
        int delStrLength = delStr.length();
        if (str.endsWith(delStr)) {
            str = str.substring(0, str.length() - delStrLength);
        }
        return str;
    }

    /**
     * 判断类中每个属性是否都为空
     *
     * @param o
     * @return
     */
    public static boolean allFieldIsNULL(Object o) {
        try {
            for (Field field : o.getClass().getDeclaredFields()) {
                field.setAccessible(true);

                Object object = field.get(o);
                if (object instanceof CharSequence) {
                    if (!org.springframework.util.ObjectUtils.isEmpty(object)) {
                        return false;
                    }
                } else {
                    if (null != object) {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("判断对象属性为空异常");
        }
        return true;
    }

    public static void main(String[] arg) {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list.add(getData(0));
        list.add(getData(3));
        list.add(getData(05));
        list.add(getData(6));
        list.add(getData(2));
        System.out.println("排序前" + list);
        sortList(list);
        System.out.println("排序后" + list);
        List<Map<String, String>> list2 = new ArrayList<Map<String, String>>();
        list2.add(getDataMap("hskjd"));
        list2.add(getDataMap("jhsc"));
        list2.add(getDataMap("woeuifjn"));
        list2.add(getDataMap("puch"));
        list2.add(getDataMap("wefwh"));
        System.out.println("排序前" + list2);
        sortList(list2);
        System.out.println("排序后" + list2);

        List<String> list3 = new ArrayList<>();
        list3.add("aa");
        list3.add("aa");
        list3.add("aa");
        list3.add("bb");
        list3.add("cc");
        list3.add("cc");
        list3.add("dd");
        list3.add("dd");
        listDataNum(list3);

        String s1 = trimStringStart("27382684682752635", "27382684682");
        String s = trimStringStart("sdahjhsd", "sda");
        String s2 = trimStringStart("2738268468211752635111", "27382684682");
        System.out.println("s------------:" + s);
        System.out.println("s1------------:" + s1);
        System.out.println("s2------------:" + s2);
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值