判空工具類——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;
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM