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

com.iqiny.silly.common.util.SillyAssert Maven / Gradle / Ivy

/*
 *  Copyright  iqiny.com
 *
 *  https://gitee.com/iqiny/silly
 *
 *  project name:silly-common
 *  project description:top silly project pom.xml file
 */
package com.iqiny.silly.common.util;

import com.iqiny.silly.common.SillyConstant;
import com.iqiny.silly.common.exception.SillyCheckException;

import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;

/**
 * assert
 */
public abstract class SillyAssert {

    public static void isNull(Object o) {
        if (o != null) {
            throw SillyCheckException.newInstance("数据验证异常:应该为null");
        }
    }


    public static void isNull(Object o, String msg) {
        if (o != null) {
            throw SillyCheckException.newInstance(msg);
        }
    }

    public static void isNull(Object o, Supplier msg) {
        if (o != null) {
            throw SillyCheckException.newInstance(msg.get());
        }
    }

    public static void notNull(Object o) {
        if (o == null) {
            throw SillyCheckException.newInstance("数据验证异常:不可为null");
        }
    }

    public static void notNull(Object o, String msg) {
        if (o == null) {
            throw SillyCheckException.newInstance(msg);
        }
    }

    public static void notNull(Object o, Supplier msg) {
        if (o == null) {
            throw SillyCheckException.newInstance(msg.get());
        }
    }

    public static void notEmpty(String o, String msg) {
        if (StringUtils.isEmpty(o)) {
            throw SillyCheckException.newInstance(msg);
        }
    }

    public static void notEmpty(String o, Supplier msg) {
        if (StringUtils.isEmpty(o)) {
            throw SillyCheckException.newInstance(msg.get());
        }
    }

    public static void isEmpty(String o, String msg) {
        if (StringUtils.isNotEmpty(o)) {
            throw SillyCheckException.newInstance(msg);
        }
    }

    public static void isEmpty(String o, Supplier msg) {
        if (StringUtils.isNotEmpty(o)) {
            throw SillyCheckException.newInstance(msg.get());
        }
    }

    public static void notEmpty(Map map, String msg) {
        if (map == null || map.isEmpty()) {
            throw SillyCheckException.newInstance(msg);
        }
    }

    public static void notEmpty(Collection collection, String msg) {
        if (collection == null || collection.isEmpty()) {
            throw SillyCheckException.newInstance(msg);
        }
    }

    public static void notEmpty(Collection collection, Supplier msg) {
        if (collection == null || collection.isEmpty()) {
            throw SillyCheckException.newInstance(msg.get());
        }
    }

    public static void isTrue(boolean flag, String msg) {
        if (!flag) {
            throw SillyCheckException.newInstance(msg);
        }
    }

    public static void isTrue(boolean flag, Supplier msg) {
        if (!flag) {
            throw SillyCheckException.newInstance(msg.get());
        }
    }

    public static void isFalse(boolean flag, String msg) {
        if (flag) {
            throw SillyCheckException.newInstance(msg);
        }
    }

    public static void isFalse(boolean flag, Supplier msg) {
        if (flag) {
            throw SillyCheckException.newInstance(msg.get());
        }
    }

    public static  T isAssignableClass(Object obj, Class clazz) {
       return isAssignableClass(obj, clazz, "");
    }

    public static  T isAssignableClass(Object obj, Class clazz, String baseMessage) {
        Class oClass = SillyConstant.Null.class;
        if (obj != null) {
            oClass = obj.getClass();
            if (oClass.equals(clazz)) {
                return (T) obj;
            }
        }

        isTrue(clazz.isAssignableFrom(oClass), baseMessage + "类型不支持:" + oClass.getName() + " -> " + clazz.getName());
        return (T) obj;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy