判断参数是否符合要求的一个类

简介: import java.util.Collection; public class Args { public static void check(final boolean expression, final String message) { if (!ex...
import java.util.Collection;

public class Args {

    public static void check(final boolean expression, final String message) {
        if (!expression) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void check(final boolean expression, final String message, final Object... args) {
        if (!expression) {
            throw new IllegalArgumentException(String.format(message, args));
        }
    }

    public static <T> T notNull(final T argument, final String name) {
        if (argument == null) {
            throw new IllegalArgumentException(name + " may not be null");
        }
        return argument;
    }

    public static <T extends CharSequence> T notEmpty(final T argument, final String name) {
        if (argument == null) {
            throw new IllegalArgumentException(name + " may not be null");
        }
        if (TextUtils.isEmpty(argument)) {
            throw new IllegalArgumentException(name + " may not be empty");
        }
        return argument;
    }

    public static <T extends CharSequence> T notBlank(final T argument, final String name) {
        if (argument == null) {
            throw new IllegalArgumentException(name + " may not be null");
        }
        if (TextUtils.isBlank(argument)) {
            throw new IllegalArgumentException(name + " may not be blank");
        }
        return argument;
    }

    public static <E, T extends Collection<E>> T notEmpty(final T argument, final String name) {
        if (argument == null) {
            throw new IllegalArgumentException(name + " may not be null");
        }
        if (argument.isEmpty()) {
            throw new IllegalArgumentException(name + " may not be empty");
        }
        return argument;
    }

    public static int positive(final int n, final String name) {
        if (n <= 0) {
            throw new IllegalArgumentException(name + " may not be negative or zero");
        }
        return n;
    }

    public static long positive(final long n, final String name) {
        if (n <= 0) {
            throw new IllegalArgumentException(name + " may not be negative or zero");
        }
        return n;
    }

    public static int notNegative(final int n, final String name) {
        if (n < 0) {
            throw new IllegalArgumentException(name + " may not be negative");
        }
        return n;
    }

    public static long notNegative(final long n, final String name) {
        if (n < 0) {
            throw new IllegalArgumentException(name + " may not be negative");
        }
        return n;
    }

}
相关文章
|
2月前
阿里云RPA元素出现后,有个返回结果 ,需要拿这个结果再去做判断吗?这个判断的操作 如何 处理
【2月更文挑战第8天】阿里云RPA元素出现后,有个返回结果 ,需要拿这个结果再去做判断吗?这个判断的操作 如何 处理
39 3
|
5月前
|
JSON 小程序 JavaScript
小程序根据返回值是否为空判断标签是否显示
小程序根据返回值是否为空判断标签是否显示
37 0
|
16天前
|
JSON Java API
java 写一个循环不断请求接口A判断返回值是否符合条件,不符合等待30秒继续请求判断
java 写一个循环不断请求接口A判断返回值是否符合条件,不符合等待30秒继续请求判断
|
2月前
|
小程序 区块链
血常规常见判断参数
血常规常见判断参数
14 0
|
8月前
默认移动构造、默认移动赋值自动生成的条件
默认移动构造、默认移动赋值自动生成的条件
100 0
|
4月前
自定义封装一个方法让这个方法可以判断所有的数据类型并返回
自定义封装一个方法让这个方法可以判断所有的数据类型并返回
15 0
|
8月前
判断两棵树是否完全一致
给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。
87 0
|
Java Spring
自定义注解判断参数为空
使用Spring的 @Valid和@Validated不好嘛,干嘛要自己造轮子呢.......