zoukankan      html  css  js  c++  java
  • 判断参数是否符合要求的一个类

    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;
        }
    
    }
  • 相关阅读:
    JPA拼接子查询语句
    List<String> 转json(存入数据库),json转回List<String>
    spring里restTemplate向目的URL发送post请求
    js里Date时间格式的常用转换-------(GMT转成yyyy-MM-dd)--------(根据日期获得星期几)
    zip解压的mysql安装步骤
    mysql_把符合条件的某列的多条数据合并为一条
    Spring boot全局捕获异常处理!
    spring boot 整合springmvc 各种请求?
    spring boot学习教程
    复选框读取数据,分割字符串转换成数组,显示复选框选中。
  • 原文地址:https://www.cnblogs.com/softidea/p/3334379.html
Copyright © 2011-2022 走看看