前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Java工具集-断言校验工具

Java工具集-断言校验工具

作者头像
cwl_java
发布2019-10-26 21:40:29
发布2019-10-26 21:40:29
1.1K00
代码可运行
举报
文章被收录于专栏:cwl_Javacwl_Java
运行总次数:0
代码可运行

简单工具类

写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦 网上好了一些工具类例如commom.lang3或者hutool或者Jodd这样的开源工具,但是 发现他们之中虽然设计不错,但是如果我想要使用,就必须要引入依赖并且去维护依赖,有些 甚至会有存在版本编译不通过问题,故此想要写作一个每个类都可以作为独立工具类使用 每个使用者只需要复制该类,到任何项目当中都可以使用,所以需要尊从以下两个原则才能 做到.在此诚邀各位大佬参与.可以把各自用过的工具,整合成只依赖JDK,每个类都能够单独 使用的工具.每个人当遇到业务需求需要使用的时候,只需要到这里单独拷贝一个即可使用. 抛弃传统的需要引入依赖的烦恼.让大家一起来解决你所面临的业务问题吧!

介绍

遵从两大原则

  • 1.绝不依赖JDK以外的源码
  • 2.牺牲代码复用性,每个类都必须是单独的组件,绝不互相引用,做到完全解耦
代码语言:javascript
代码运行次数:0
复制
package *;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

/**
 * @program: simple_tools
 * @description: 断言类, 类似于阿里的Assert
 * @author: ChenWenLong
 * @create: 2019-06-04 15:22
 **/
public class ValidateUtil {

    public ValidateUtil() {
        super();
    }

    /**
     * 功能描述:
     * 〈校验结果是否为true〉
     *
     * @params : [expression, message, value]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:27
     */
    public static void isTrue(boolean expression, String message, Object value) {
        if (expression == false) {
            throw new IllegalArgumentException(message + value);
        }
    }

    /**
     * 功能描述:
     * 〈校验结果是否为true〉
     *
     * @params : [expression, message, value]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:27
     */
    public static void isTrue(boolean expression, String message, long value) {
        if (expression == false) {
            throw new IllegalArgumentException(message + value);
        }
    }

    /**
     * 功能描述:
     * 〈校验结果是否为true〉
     *
     * @params : [expression, message, value]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:27
     */
    public static void isTrue(boolean expression, String message, double value) {
        if (expression == false) {
            throw new IllegalArgumentException(message + value);
        }
    }

    /**
     * 功能描述:
     * 〈校验结果是否为true〉
     *
     * @params : [expression, message, value]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:27
     */
    public static void isTrue(boolean expression, String message) {
        if (expression == false) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 功能描述:
     * 〈校验结果是否为true〉
     *
     * @params : [expression, message, value]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:27
     */
    public static void isTrue(boolean expression) {
        if (expression == false) {
            throw new IllegalArgumentException("The validated expression is false");
        }
    }

    /**
     * 功能描述:
     * 〈校验结果是否为true〉
     *
     * @params : [expression, message, value]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:27
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 功能描述:
     * 〈校验对象object是否为空〉
     *
     * @params : [object]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:29
     */
    public static void notNull(Object object) {
        if (object == null) {
            throw new IllegalArgumentException("The validated object is null");
        }
    }

    /**
     * 功能描述:
     * 〈校验数组是否为空〉
     *
     * @params : [array, message]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:30
     */
    public static void notEmpty(Object[] array, String message) {
        if (array == null || array.length == 0) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 功能描述:
     * 〈校验数组是否为空〉
     *
     * @params : [array, message]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:30
     */
    public static void notEmpty(Object[] array) {
        if (array == null || array.length == 0) {
            throw new IllegalArgumentException("The validated array is empty");
        }
    }

    /**
     * 功能描述:
     * 〈校验集合是否为空〉
     *
     * @params : [collection, message]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:32
     */
    public static void notEmpty(Collection collection, String message) {
        if (collection == null || collection.size() == 0) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 功能描述:
     * 〈校验集合是否为空〉
     *
     * @params : [collection]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:32
     */
    public static void notEmpty(Collection collection) {
        if (collection == null || collection.size() == 0) {
            throw new IllegalArgumentException("The validated collection is empty");
        }
    }

    /**
     * 功能描述:
     * 〈校验map集合是否为空〉
     *
     * @params : [map, message]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:33
     */
    public static void notEmpty(Map map, String message) {
        if (map == null || map.size() == 0) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 功能描述:
     * 〈校验map集合是否为空〉
     *
     * @params : [map]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:33
     */
    public static void notEmpty(Map map) {
        if (map == null || map.size() == 0) {
            throw new IllegalArgumentException("The validated map is empty");
        }
    }

    /**
     * 功能描述:
     * 〈校验字符串是否为空〉
     *
     * @params : [string, message]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:33
     */
    public static void notEmpty(String string, String message) {
        if (string == null || string.length() == 0) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 功能描述:
     * 〈校验字符串是否为空〉
     *
     * @params : [string]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:34
     */
    public static void notEmpty(String string) {
        if (string == null || string.length() == 0) {
            throw new IllegalArgumentException("The validated string is empty");
        }
    }

    /**
     * 功能描述:
     * 〈校验数组array的每一个元素都不为空〉
     *
     * @params : [array, message]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:35
     */
    public static void noNullElements(Object[] array, String message) {
        notNull(array);
        for (int i = 0; i < array.length; i++) {
            if (array[i] == null) {
                throw new IllegalArgumentException(message);
            }
        }
    }

    /**
     * 功能描述:
     * 〈校验数组的每一个元素都不为空〉
     *
     * @params : [array]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:36
     */
    public static void noNullElements(Object[] array) {
        notNull(array);
        for (int i = 0; i < array.length; i++) {
            if (array[i] == null) {
                throw new IllegalArgumentException("The validated array contains null element at index: " + i);
            }
        }
    }

    /**
     * 功能描述:
     * 〈校验集合的每个元素都不为空〉
     *
     * @params : [collection, message]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:36
     */
    public static void noNullElements(Collection collection, String message) {
        notNull(collection);
        for (Iterator it = collection.iterator(); it.hasNext();) {
            if (it.next() == null) {
                throw new IllegalArgumentException(message);
            }
        }
    }

    /**
     * 功能描述:
     * 〈校验集合中的每个元素都不为空〉
     *
     * @params : [collection]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:37
     */
    public static void noNullElements(Collection collection) {
        notNull(collection);
        int i = 0;
        for (Iterator it = collection.iterator(); it.hasNext(); i++) {
            if (it.next() == null) {
                throw new IllegalArgumentException("The validated collection contains null element at index: " + i);
            }
        }
    }

    /**
     * 功能描述:
     * 〈校验集合中的每个元素都是clazz类型〉
     *
     * @params : [collection, clazz, message]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:38
     */
    public static void allElementsOfType(Collection collection, Class clazz, String message) {
        notNull(collection);
        notNull(clazz);
        for (Iterator it = collection.iterator(); it.hasNext(); ) {
            if (clazz.isInstance(it.next()) == false) {
                throw new IllegalArgumentException(message);
            }
        }
    }

    /**
     * 功能描述:
     * 〈校验集合中的每个元素都是clazz的类型〉
     *
     * @params : [collection, clazz]
     * @return : void
     * @author : cwl
     * @date : 2019/6/4 15:38
     */
    public static void allElementsOfType(Collection collection, Class clazz) {
        notNull(collection);
        notNull(clazz);
        int i = 0;
        for (Iterator it = collection.iterator(); it.hasNext(); i++) {
            if (clazz.isInstance(it.next()) == false) {
                throw new IllegalArgumentException("The validated collection contains an element not of type "
                        + clazz.getName() + " at index: " + i);
            }
        }
    }


}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/10/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简单工具类
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档