zoukankan      html  css  js  c++  java
  • 判空工具类——Java

    代码

    import java.util.Collection;
    
    /**
     * @author Yawei Xi
     * @date 2017-11-29
     */
    public class EmptyUtil {
    
        /**
         * 判断字符串是否为空
         * PS:
         * 为空的条件:
         * 1. String对象为空
         * 2. 没有任何字符的字符串
         *
         * @param str 需要判断的字符串
         * @return 为空(true), 非空(false)
         */
        public static boolean isEmpty(String str) {
            return null == str || "".equals(str);
        }
    
        /**
         * 判断字符串是否为空
         * PS:
         * 为空的条件:
         * 1. String对象为空
         * 2. 没有任何字符的字符串
         *
         * @param str       需要判断的字符串
         * @param isTrimmed 判断前是否去掉字符串前后的空格:是(true), 否(false)
         * @return 为空(true), 非空(false)
         */
        public static boolean isEmpty(String str, boolean isTrimmed) {
            return isTrimmed ? null == str || "".equals(str.trim()) : null == str || "".equals(str);
        }
    
        /**
         * 判断对象是否为空
         *
         * @param obj 需要进行判断的对象
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(Object obj) {
            return null == obj || "".equals(obj);
        }
    
        /**
         * 判断集合是否为空
         * PS:
         * 集合为空的条件:
         * 1. 集合对象为null
         * 2. 集合中没有元素
         *
         * @param collection 需要进行判断的集合
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(Collection<?> collection) {
            return null == collection || collection.size() == 0;
        }
    
        /**
         * 判断对象数组是否为空
         * PS:
         * 对象数组为空的条件:
         * 1. 对象数组为null
         * 2. 对象数组中没有元素
         *
         * @param array 需要进行判断的对象数组
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(Object[] array) {
            return null == array || array.length == 0;
        }
    
        /**
         * 判断数组是否为空
         * PS:
         * 数组为空的条件:
         * 1. 数组为null
         * 2. 数组中没有元素
         *
         * @param array 需要进行判断的数组
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(long[] array) {
            return array == null || array.length == 0;
        }
    
        /**
         * 判断数组是否为空
         * PS:
         * 数组为空的条件:
         * 1. 数组为null
         * 2. 数组中没有元素
         *
         * @param array 需要进行判断的数组
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(int[] array) {
            return array == null || array.length == 0;
        }
    
        /**
         * 判断数组是否为空
         * PS:
         * 数组为空的条件:
         * 1. 数组为null
         * 2. 数组中没有元素
         *
         * @param array 需要进行判断的数组
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(short[] array) {
            return array == null || array.length == 0;
        }
    
        /**
         * 判断数组是否为空
         * PS:
         * 数组为空的条件:
         * 1. 数组为null
         * 2. 数组中没有元素
         *
         * @param array 需要进行判断的数组
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(char[] array) {
            return array == null || array.length == 0;
        }
    
        /**
         * 判断数组是否为空
         * PS:
         * 数组为空的条件:
         * 1. 数组为null
         * 2. 数组中没有元素
         *
         * @param array 需要进行判断的数组
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(byte[] array) {
            return array == null || array.length == 0;
        }
    
        /**
         * 判断数组是否为空
         * PS:
         * 数组为空的条件:
         * 1. 数组为null
         * 2. 数组中没有元素
         *
         * @param array 需要进行判断的数组
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(double[] array) {
            return array == null || array.length == 0;
        }
    
        /**
         * 判断数组是否为空
         * PS:
         * 数组为空的条件:
         * 1. 数组为null
         * 2. 数组中没有元素
         *
         * @param array 需要进行判断的数组
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(float[] array) {
            return array == null || array.length == 0;
        }
    
        /**
         * 判断数组是否为空
         * PS:
         * 数组为空的条件:
         * 1. 数组为null
         * 2. 数组中没有元素
         *
         * @param array 需要进行判断的数组
         * @return 为空(true), 不为空(false)
         */
        public static boolean isEmpty(boolean[] array) {
            return array == null || array.length == 0;
        }
    }
    
  • 相关阅读:
    IE内核浏览器下中文双引号自动显示成英文引号的说明
    【转】Linq初体验——Order By 通过属性名动态排序
    【转】Windows8不联网直接安装.Net 3.5 Framework的方法
    DrawImage在绘制图片的时候,为什么会擅自改变图片的大小?
    用js判断.net版本
    【转】html 页面打印并 分页
    利用IE9下载网络歌曲新法
    LINQ访问DataTable
    20165205与20165233结对感想以及创意照
    20165233 Java第一章学习总结
  • 原文地址:https://www.cnblogs.com/freelancy/p/7923024.html
Copyright © 2011-2022 走看看