Spring工具類的原始碼閱讀(更新中~)
阿新 • • 發佈:2018-12-30
序:
這些工具類不僅僅只侷限於spring框架的內部使用,完全可以作為獨立的工具類在我們編寫的專案中進行使用。好處便是程式碼質量提高,編寫效率加快。今天在閱讀
spring原始碼時,發現Assert類的使用,於是便參考網路資源,在這裡做一個記錄。
工具類:
斷言Assert類
包名-org.springframework.util
public abstract class Assert {
public static void isTrue(boolean expression, String message) {
if (!expression) {
throw new IllegalArgumentException(message);
}
}
/**
* 1.根據某個表示式的boolean型別,進行斷言。
*/
public static void isTrue(boolean expression) {
isTrue(expression, "[Assertion failed] - this expression must be true");
}
public static void isNull(Object object, String message) {
if (object != null) {
throw new IllegalArgumentException(message);
}
}
/**
* 2.根據物件是否為空進行斷言。不為空產生異常。
* @param object
*/
public static void isNull(Object object) {
isNull(object, "[Assertion failed] - the object argument must be null");
}
public static void notNull(Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}
/**
* 3.根據物件是否為空進行斷言。為空產生異常。
* @param object
*/
public static void notNull(Object object) {
notNull(object, "[Assertion failed] - this argument is required; it must not be null");
}
public static void hasLength(String text, String message) {
if (!StringUtils.hasLength(text)) {
throw new IllegalArgumentException(message);
}
}
/**
* 4.根據字串內容是否為空/空值進行斷言,為空或空值則產生異常
* @param text
*/
public static void hasLength(String text) {
hasLength(text,
"[Assertion failed] - this String argument must have length; it must not be null or empty");
}
public static void hasText(String text, String message) {
if (!StringUtils.hasText(text)) {
throw new IllegalArgumentException(message);
}
}
/**
* 5.比4的斷言更嚴格,要求字串內容不能為空格字串所組成的。否則產生異常
* @param text
*/
public static void hasText(String text) {
hasText(text,
"[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
}
public static void doesNotContain(String textToSearch, String substring, String message) {
if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
textToSearch.indexOf(substring) != -1) {
throw new IllegalArgumentException(message);
}
}
/**
* 6.根據字串是否包含子字串進行斷言,不包含則產生異常。
* @param textToSearch
* @param substring
*/
public static void doesNotContain(String textToSearch, String substring) {
doesNotContain(textToSearch, substring,
"[Assertion failed] - this String argument must not contain the substring [" + substring + "]");
}
public static void notEmpty(Object[] array, String message) {
if (ObjectUtils.isEmpty(array)) {
throw new IllegalArgumentException(message);
}
}
/**
*
*
* 7.根據陣列是否為空或者陣列長度為0進行斷言,滿足前者之一則發生異常。
*/
public static void notEmpty(Object[] array) {
notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
}
public static void noNullElements(Object[] array, String message) {
if (array != null) {
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
throw new IllegalArgumentException(message);
}
}
}
}
/**
* 8.根據陣列是否包含null物件進行斷言,滿足則發生異常
* @param array
*/
public static void noNullElements(Object[] array) {
noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
}
public static void notEmpty(Collection collection, String message) {
if (CollectionUtils.isEmpty(collection)) {
throw new IllegalArgumentException(message);
}
}
/**
* 9.對集合型別是否為空或者元素為0進行斷言,滿足則發生異常
* @param collection
*/
public static void notEmpty(Collection collection) {
notEmpty(collection,
"[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
}
public static void notEmpty(Map map, String message) {
if (CollectionUtils.isEmpty(map)) {
throw new IllegalArgumentException(message);
}
}
/**
* 10.對Map型別是否為空或者元素為0進行斷言,滿足則發生異常
* @param map
*/
public static void notEmpty(Map map) {
notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
}
public static void isInstanceOf(Class clazz, Object obj) {
isInstanceOf(clazz, obj, "");
}
/**
*
*11.根據型別是否所屬於指定型別進行斷言,不是則發生異常
*/
public static void isInstanceOf(Class type, Object obj, String message) {
notNull(type, "Type to check against must not be null");
if (!type.isInstance(obj)) {
throw new IllegalArgumentException(message +
"Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
"] must be an instance of " + type);
}
}
/**
* 12.subType必須可以按型別匹配於 superType,否則將丟擲異常;
*/
public static void isAssignable(Class superType, Class subType) {
isAssignable(superType, subType, "");
}
public static void isAssignable(Class superType, Class subType, String message) {
notNull(superType, "Type to check against must not be null");
if (subType == null || !superType.isAssignableFrom(subType)) {
throw new IllegalArgumentException(message + subType + " is not assignable to " + superType);
}
}
public static void state(boolean expression, String message) {
if (!expression) {
throw new IllegalStateException(message);
}
}
/**
* 13.根據某個表示式的boolean型別,進行斷言。類似1
* @param expression
*/
public static void state(boolean expression) {
state(expression, "[Assertion failed] - this state invariant must be true");
}
}