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

com.github.hetianyi.common.util.AssertUtil Maven / Gradle / Ivy

package com.github.hetianyi.common.util;


import java.io.File;
import java.io.FileNotFoundException;

/**
 * 断言工具类
 *
 * @author Jason He
 * @version 1.0.0
 * @date 2019-12-27
 */
public final class AssertUtil {

    /**
     *
     * @param o
     * @param msg
     */
    public static final void notNull(Object o, String msg) {
        if (null == o) {
            throw new NullPointerException(msg);
        }
    }

    /**
     * 检查string非空
     * @param o
     */
    public static final void notNullOrEmpty(String o, String msg) {
        if (null == o || "".equals(o.trim())) {
            throw new IllegalStateException(msg);
        }
    }

    /**
     * 检查给定int值是否在限定范围内
     * @param min 最小值
     * @param max 最大值
     * @param value 要检查的值
     * @param msg 自定义异常信息
     */
    public static final void withinRange(int min, int max, int value, String msg) {
        if (value < min || value > max) {
            throw new IllegalArgumentException(msg);
        }
    }

    /**
     * 检查给定int值是否在限定范围内
     * @param min 最小值
     * @param max 最大值
     * @param value 要检查的值
     * @param msg 自定义异常信息
     */
    public static final void withinRange(long min, long max, long value, String msg) {
        if (value < min || value > max) {
            throw new IllegalArgumentException(msg);
        }
    }

    /**
     * 检查文件是否存在
     * @param file
     * @throws FileNotFoundException
     */
    public static final void exists(File file) throws FileNotFoundException {
        notNull(file, "file cannot be null");
        if (!file.exists()) {
            throw new FileNotFoundException(file.getAbsolutePath());
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy