据我所知,在我们编写代码的过程中难免需要写if else判断语句,往往一个复杂一点的业务需要写上成堆成堆的if语句,这很影响代码的可读性和简洁性,而断言工具就是为了帮助我们简化判断语句,实现代码可读性的提高,让我们的业务代码更加的”优雅”。
就我了解,目前常用的一些断言工具,包括JDK自带的,以及spring自带的,还有lang3中的Validate都能直接使用来简化判断语句,但是难免会有一些场景导致它们自带的断言工具对你不适用,因此我写了一个简单的断言工具类,里面提供了一些简单的断言方法,后续你也可以在其中添加你所需要用得到其他断言方法;(ps:有点像仿照的spring中自带的Assert断言类hhh)
/**
* 断言工具类
*
*/
public class AssertUtils {
/**
* 断言或字符串不为空,若为空抛出IllegalArgumentException类型异常
*
* @param obj
* @param msg
*/
public static void assertNotNull(Object obj, String msg) {
if (obj == null) {
throw new IllegalArgumentException(msg);
}
}
/**
* 断言或字符串为空,若不为空抛出IllegalArgumentException类型异常
*
* @param obj
* @param msg
*/
public static void assertNull(Object obj, String msg) {
if (obj != null) {
throw new IllegalArgumentException(msg + ", But was not null");
}
}
/**
* 断言为true,若不为true抛出IllegalArgumentException类型异常
*
* @param condition
* @param msg
*/
public static void assertTrue(boolean condition, String msg) {
if (!condition) {
throw new IllegalArgumentException(msg);
}
}
/**
* 断言为false,若不为false抛出IllegalArgumentException类型异常
*
* @param condition
* @param msg
*/
public static void assertFalse(boolean condition, String msg) {
if (condition) {
throw new IllegalArgumentException(msg);
}
}
/**
* 判断某俩个字符串或对象相同,若不相同抛出IllegalArgumentException类型异常
*
* @param expected
* @param actual
* @param msg
*/
public static void assertEquals(Object expected, Object actual, String msg) {
if (!Objects.equals(expected, actual)) {
throw new IllegalArgumentException(msg + ", Expected: " + expected + ", Actual: " + actual);
}
}
/**
* 判断某俩个字符串或对象不相同,若相同抛出IllegalArgumentException类型异常
*
* @param expected
* @param actual
* @param msg
*/
public static void assertNotEquals(Object expected, Object actual, String msg) {
if (Objects.equals(expected, actual)) {
throw new IllegalArgumentException(msg + ", Expected and actual should not be equal.");
}
}
/**
* 断言俩个数组元素相同,若不相同抛出IllegalArgumentException类型异常
*
* @param expected
* @param actual
* @param msg
*/
public static void assertArrayEquals(Object[] expected, Object[] actual, String msg) {
if (!Arrays.equals(expected, actual)) {
throw new IllegalArgumentException(msg + ", Expected: " + Arrays.toString(expected) + ", Actual: " + Arrays.toString(actual));
}
}
/**
* 断言俩个数组元素不相同,若相同抛出IllegalArgumentException类型异常
*
* @param expected
* @param actual
* @param msg
*/
public static void assertArrayNotEquals(Object[] expected, Object[] actual, String msg) {
if (Arrays.equals(expected, actual)) {
throw new IllegalArgumentException(msg + ", Expected: " + Arrays.toString(expected) + ", Actual: " + Arrays.toString(actual));
}
}
/**
* 断言俩个对象相同,若不相同抛出IllegalArgumentException类型异常
*
* @param expected
* @param actual
* @param msg
*/
public static void assertSame(Object expected, Object actual, String msg) {
if (expected != actual) {
throw new IllegalArgumentException(msg + ", Expected: " + expected + ", Actual: " + actual);
}
}
/**
* 断言俩个对象不相同,若相同抛出IllegalArgumentException类型异常
*
* @param expected
* @param actual
* @param msg
*/
public static void assertNotSame(Object expected, Object actual, String msg) {
if (expected == actual) {
throw new IllegalArgumentException(msg + ", Expected and actual should not be the same.");
}
}
/**
* 断言错误,直接抛出IllegalArgumentException类型异常,打印fail信息
*
* @param msg
*/
public static void fail(String msg) {
throw new IllegalArgumentException(msg);
}
// 待添加...
}
版权声明:本文为MrBInsomnia原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。