package com.tzy.common.exception;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * @author by po'yi
 * @Classname Assert
 * @Description 自定义Assert
 * @Date 2022/3/10 10:56
 */
public class AppAssert extends Assert {

	public AppAssert() {
		super();
	}

	public static void notNull(@Nullable Object object, String message) {
		if (object == null) {
			throw new ServiceException(500,message);
		}
	}

	public static void isEmpty(@Nullable String str, String message) {
		if (StringUtils.isEmpty(str)) {
			throw new ServiceException(500,message);
		}
	}

	/**
	 * 判断集合非空
	 * @param collection
	 * @param message
	 */
	public static void notEmpty(@Nullable Collection<?> collection, String message) {
		if (CollectionUtils.isEmpty(collection)) {
			throw new ServiceException(500,message);
		}
	}

	public static void isTrue(boolean expression, String message) {
		if (!expression) {
			throw new ServiceException(500, message);
		}
	}

	public static void hasText(@Nullable String text, String message) {
		if (!StringUtils.hasText(text)) {
			throw new ServiceException(500, message);
		}
	}

}
