All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yixan.tools.common.util.VerifyUtil Maven / Gradle / Ivy

There is a newer version: 3.7.1
Show newest version
package com.yixan.tools.common.util;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

/**
 * 校验工具类
 *
 * @author zhaohuihua
 * @version V1.0 2015年9月16日
 */
public abstract class VerifyUtil {

    /** 返回第一个非空的参数; 如果全都为空, 返回最后一个参数 **/
    @SuppressWarnings("unchecked")
    public static  T nvl(T... objects) {
        T last = null;
        for (T object : objects) {
            last = object;
            if (isNotBlank(object)) {
                return object;
            }
        }
        return last;
    }

    public static boolean isBlank(Object object) {
        if (object == null) {
            return true;
        }

        if (object instanceof CharSequence) {
            CharSequence string = (CharSequence) object;
            int length;
            if ((length = string.length()) == 0) {
                return true;
            }
            for (int i = 0; i < length; i++) {
                if (Character.isWhitespace(string.charAt(i)) == false) {
                    return false;
                }
            }
            return true;
        } else if (object.getClass().isArray()) {
            return Array.getLength(object) == 0;
        } else if (object instanceof Collection) {
            return ((Collection) object).isEmpty();
        } else if (object instanceof Map) {
            Map map = (Map) object;
            return map.isEmpty();
        } else if (object instanceof Iterable) {
            return !((Iterable) object).iterator().hasNext();
        } else {
            return false;
        }
    }

    public static boolean isNotBlank(Object object) {
        return !isBlank(object);
    }

    /**
     * 只有一个为空就返回true
     *
     * @param objects
     * @return
     */
    public static boolean isAnyBlank(Object... objects) {
        if (objects == null || objects.length == 0) {
            return true;
        }

        for (Object object : objects) {
            if (isBlank(object)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 全都为空就返回true
     *
     * @param objects
     * @return
     */
    public static boolean isAllBlank(Object... objects) {
        if (objects == null || objects.length == 0) {
            return true;
        }

        for (Object object : objects) {
            if (isNotBlank(object)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 全都不为空就返回true
     *
     * @param objects
     * @return
     */
    public static boolean isNoneBlank(Object... objects) {
        return !isAnyBlank(objects);
    }

    /**
     * 判断一个对象是否存在于列表中
     *
     * @param object 对象
     * @param objects 列表
     * @return 是否存在
     */
    public static boolean isExists(Object object, Object... objects) {
        if (objects == null || objects.length == 0) {
            return false;
        }
        for (Object i : objects) {
            if (i == null && object == null) {
                return true;
            } else if (i != null && i.equals(object)) {
                return true;
            } else if (object != null && object.equals(i)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断一个对象是否不存在于列表中
     *
     * @param object 对象
     * @param objects 列表
     * @return 是否不存在
     */
    public static boolean isNotExists(Object object, Object... objects) {
        return !isExists(object, objects);
    }

    /**
     * 判断两个对象是不是相等
     *
     * @author zhaohuihua
     * @param o
     * @param n
     * @return
     */
    public static boolean equals(Object o, Object n) {
        if (o == null && n == null) {
            return true;
        } else if (o == null && n != null || o != null && n == null) {
            return false;
        } else {
            return o.equals(n);
        }
    }

    /**
     * 判断两个对象是不是不相等
     *
     * @author zhaohuihua
     * @param o
     * @param n
     * @return
     */
    public static boolean notEquals(Object o, Object n) {
        return !equals(o, n);
    }

    /**
     * 判断对象是否改变, 对象不为空且与原值不符则已改变
     *
     * @author zhaohuihua
     * @param value 判断的对象
     * @param older 原值
     * @return 是否改变
     */
    public static boolean isChanged(Object value, Object older) {
        return isNotBlank(value) && notEquals(value, older);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy