一、前言
基于spring框架spring-core(4.1.4)中的org.springframework.util.Assert断言工具类,分享解读关于在代码假设或前提不成立时,使用此断言打断代码正常的执行,从下面源码中可以看出,断言实现原理其实就是抛出异常throw new IllegalArgumentException来阻断正常代码执行顺序。
二、代码分享
import java.util.Collection;@b@import java.util.Map;@b@@b@public abstract class Assert@b@{@b@ public static void isTrue(boolean expression, String message)@b@ {@b@ if (!(expression))@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ public static void isTrue(boolean expression)@b@ {@b@ isTrue(expression, "[Assertion failed] - this expression must be true");@b@ }@b@@b@ public static void isNull(Object object, String message)@b@ {@b@ if (object != null)@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ public static void isNull(Object object)@b@ {@b@ isNull(object, "[Assertion failed] - the object argument must be null");@b@ }@b@@b@ public static void notNull(Object object, String message)@b@ {@b@ if (object == null)@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ public static void notNull(Object object)@b@ {@b@ notNull(object, "[Assertion failed] - this argument is required; it must not be null");@b@ }@b@@b@ public static void hasLength(String text, String message)@b@ {@b@ if (!(StringUtils.hasLength(text)))@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ public static void hasLength(String text)@b@ {@b@ hasLength(text, @b@ "[Assertion failed] - this String argument must have length; it must not be null or empty");@b@ }@b@@b@ public static void hasText(String text, String message)@b@ {@b@ if (!(StringUtils.hasText(text)))@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ public static void hasText(String text)@b@ {@b@ hasText(text, @b@ "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");@b@ }@b@@b@ public static void doesNotContain(String textToSearch, String substring, String message)@b@ {@b@ if ((StringUtils.hasLength(textToSearch)) && (StringUtils.hasLength(substring)) && @b@ (textToSearch.indexOf(substring) != -1))@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ public static void doesNotContain(String textToSearch, String substring)@b@ {@b@ doesNotContain(textToSearch, substring, @b@ "[Assertion failed] - this String argument must not contain the substring [" + substring + "]");@b@ }@b@@b@ public static void notEmpty(Object[] array, String message)@b@ {@b@ if (ObjectUtils.isEmpty(array))@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ public static void notEmpty(Object[] array)@b@ {@b@ notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");@b@ }@b@@b@ public static void noNullElements(Object[] array, String message)@b@ {@b@ if (array != null)@b@ for (int i = 0; i < array.length; ++i)@b@ if (array[i] == null)@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ public static void noNullElements(Object[] array)@b@ {@b@ noNullElements(array, "[Assertion failed] - this array must not contain any null elements");@b@ }@b@@b@ public static void notEmpty(Collection collection, String message)@b@ {@b@ if (CollectionUtils.isEmpty(collection))@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ public static void notEmpty(Collection collection)@b@ {@b@ notEmpty(collection, @b@ "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");@b@ }@b@@b@ public static void notEmpty(Map map, String message)@b@ {@b@ if (CollectionUtils.isEmpty(map))@b@ throw new IllegalArgumentException(message);@b@ }@b@@b@ public static void notEmpty(Map map)@b@ {@b@ notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");@b@ }@b@@b@ public static void isInstanceOf(Class clazz, Object obj)@b@ {@b@ isInstanceOf(clazz, obj, "");@b@ }@b@@b@ public static void isInstanceOf(Class type, Object obj, String message)@b@ {@b@ notNull(type, "Type to check against must not be null");@b@ if (!(type.isInstance(obj)))@b@ throw new IllegalArgumentException(message + @b@ "Object of class [" + "null" + @b@ "] must be an instance of " + type);@b@ }@b@@b@ public static void isAssignable(Class superType, Class subType)@b@ {@b@ isAssignable(superType, subType, "");@b@ }@b@@b@ public static void isAssignable(Class superType, Class subType, String message)@b@ {@b@ notNull(superType, "Type to check against must not be null");@b@ if ((subType == null) || (!(superType.isAssignableFrom(subType))))@b@ throw new IllegalArgumentException(message + subType + " is not assignable to " + superType);@b@ }@b@@b@ public static void state(boolean expression, String message)@b@ {@b@ if (!(expression))@b@ throw new IllegalStateException(message);@b@ }@b@@b@ public static void state(boolean expression)@b@ {@b@ state(expression, "[Assertion failed] - this state invariant must be true");@b@ }@b@}