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

io.github.kits.Assert Maven / Gradle / Ivy

The newest version!
package io.github.kits;

import java.util.Objects;
import java.util.function.Predicate;

/**
 * 断言工具类, 不符合条件时抛出异常
 *
 * @project: kits
 * @created: with IDEA
 * @author: kits
 * @date: 2018 11 20 2:26 PM | November. Tuesday
 */
public class Assert {

	/**
	 * if {@code !var1.equals(var2)} then throws IllegalArgumentException
	 *
	 * @param var1			value1
	 * @param var2			value2
	 * @param 			泛型约束
	 */
	public static  void equal(T var1, T var2) {
		String message = var1 + " not equals to " + var2;
		predicate(var1, arg -> !Objects.equals(var1, var2), message);
	}

	/**
	 * if {@code var == null} then throws NullPointerException as message
	 *
	 * @param var			需要判断的值
	 * @param message		不符合条件的错误信息
	 */
	public static  void isNull(T var, String message) {
		isNull(var, new NullPointerException(message));
	}

	/**
	 * if {@code var == null} then throws exception by yourself
	 *
	 * @param var			需要判断的值
	 * @param ex			不符合条件时抛出的异常
	 */
	public static  void isNull(T var, Exception ex) {
		predicate(var, Objects::nonNull, ex);
	}

	/**
	 * if {@code var != null} then throws IllegalArgumentException
	 *
	 * @param var			需要判断的值
	 * @param message		不符合条件的错误信息
	 */
	public static  void isNotNull(T var, String message) {
		isNotNull(var, new IllegalArgumentException(message));
	}

	/**
	 * if {@code var != null} then throws exception by yourself
	 *	
	 * 		Useage: Assert.isNotNull("this is value", new CustomerException("this is message"));
	 * 	
* * @param var 需要判断的值 * @param ex 不符合条件时抛出的异常 * @param 泛型约束 */ public static void isNotNull(T var, Exception ex) { predicate(var, Objects::isNull, ex); } /** * if var is null or '' then throws IllegalArgumentException * * @param var 需要判断的值 * @param message 不符合条件的错误信息 * @param 泛型约束 */ public static void isNotNullOrEmpty(T var, String message) { predicate(var, EnvKit::hasNullOrEmpty, message ); } /** * if var is null or '' then throws IllegalArgumentException * * @param var 需要判断的值 * @param ex 不符合条件时抛出的异常 * @param 泛型约束 */ public static void isNotNullOrEmpty(T var, Exception ex) { predicate(var, EnvKit::hasNullOrEmpty, ex ); } /** * if var satisfies the condition of the predicate, * then throws IllegalArgumentException * * @param var 需要判断的值 * @param predicate 断言类型 * @param message 不符合条件时的错误信息 * @param 泛型约束 */ public static void predicate(T var, Predicate predicate, String message) { predicate(var, predicate, new IllegalArgumentException(message)); } /** * If var satisfies the condition of the predicate, * the IllegalArgumentException is thrown * * @param var 需要判断的值 * @param predicate 断言类型 * @param 泛型约束 */ public static void predicate(T var, Predicate predicate) { if (!predicate.test(var)) { throw new IllegalArgumentException(var.getClass().getName()); } } /** * If var satisfies the condition of the predicate, * the corresponding exception is thrown * * @param var 需要判断的值 * @param predicate 断言类型 * @param ex 异常 * @param 泛型约束 */ public static void predicate(T var, Predicate predicate, Exception ex) { if (predicate.test(var)) { throwAsUnchecked(ex); } } /** * If var satisfies the condition of the predicates, * the IllegalArgumentException is thrown * * @param var 需要判断的值 * @param predicates 断言类型 * @param 泛型约束 */ public static void predicate(T var, Predicate... predicates) { if (predicates.length > 0) { Predicate predicate = predicates[0]; for (Predicate pred : predicates) { predicate = predicate.and(pred); } predicate(var, predicate, new IllegalArgumentException(var.getClass().getName())); } } private static void throwAsUnchecked(Exception exception) throws E { throw (E) exception; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy