zoukankan      html  css  js  c++  java
  • Java基础之断言

      断言是在Java 1.4中引入的。它能让你验证假设。如果断言失败(即返回false),就会抛出AssertionError(如果启用断言)。

    什么时候使用断言?

      断言不应该用于验证输入数据到一个public方法或命令行参数。IllegalArgumentException会是一个更好的选择。在public方法中,只用断言来检查它们根本不应该发生的情况。

    import java.util.Collection;
    import java.util.Map;
    
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.collections.MapUtils;
    import org.apache.commons.lang.StringUtils;
    
    /**
     * 断言扩展
     * 
     * @ClassName: Assert
     * @author coco.xu*/
    @SuppressWarnings("rawtypes")
    public class Assert {
    
        public static void hasText(Long text, String message) {
            if (text == null || text == 0)
                throw new IllegalArgumentException(message);
            else
                return;
        }
    
        public static void hasText(Integer text, String message) {
            if (text == null || text == 0)
                throw new IllegalArgumentException(message);
            else
                return;
        }
    
        public static void hasText(String text, String message) {
            org.springframework.util.Assert.hasText(text, message);
        }
    
        public static void notNull(Object obj, String message) {
            if (obj == null) {
                throw new IllegalArgumentException(message);
            }
        }
        
        public static void notEmpty(Collection obj, String message) {
            if (CollectionUtils.isEmpty(obj)) {
                throw new IllegalArgumentException(message);
            }
        }
        
        public static void notEmpty(Map obj, String message) {
            if (MapUtils.isEmpty(obj)) {
                throw new IllegalArgumentException(message);
            }
        }
    
        public static void hasText(String text) {
            org.springframework.util.Assert
                    .hasText(
                            text,
                            "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
        }
        
        public static void notEmpty(String text,String message)
        {
            if(StringUtils.isEmpty(text))
            {
                throw new IllegalArgumentException(message);
            }
        }
    
    }
  • 相关阅读:
    Pyhton入门 笔记 第二天 变量与运算符
    Pyhton入门 笔记 第一天 数据类型
    HTML 中的特殊字符
    Extjs--12种布局方式
    .NET easyUI tree树状结构
    SqlServer中嵌套事务使用--事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配 --根本问题
    事务回滚 DEMO
    SQL 事物回滚
    一步一步教你玩转.NET Framework的配置文件app.config
    C# tostring() 方法
  • 原文地址:https://www.cnblogs.com/cocoxu1992/p/10594256.html
Copyright © 2011-2022 走看看